之前我们介绍了Keepalived+Nginx双机主备实现Nginx高可用,这种架构有个很大的缺陷,就是只有一台Nginx服务器提供服务,另外一台会一直被闲置,造成资源浪费。
通过对Keepalived+Nginx双机主备进行优化,使用两个虚拟IP,两台Nginx服务器互为主备
Keepalived+Nginx双机主备的架构图:
Keepalived+Nginx双主热备的架构图:
其中,虚拟IP 192.168.1.161的Master节点为服务器192.168.1.171,Backup节点为服务器192.168.1.172;虚拟IP 192.168.1.162的Master节点为服务器192.168.1.172,Backup节点为服务器192.168.1.171
DNS可以实现一个域名对应多个IP
Keepalived+Nginx双主热备配置
第一台服务器配置:
global_defs { router_id KEEPALIVED_NODE_2 } vrrp_script check_nginx_alive { script "/etc/keepalived/check_nginx_alive.sh" interval 2 # 每隔两秒运行上一行脚本 weight 10 # 如果脚本运行成功,则升级权重+10 # weight -10 # 如果脚本运行失败,则升级权重-10 } vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 50 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { check_nginx_alive # 追踪 nginx 脚本 } virtual_ipaddress { 192.168.1.171 } } vrrp_instance VI_2 { state BACKUP interface ens33 virtual_router_id 51 priority 80 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { check_nginx_alive # 追踪 nginx 脚本 } virtual_ipaddress { 192.168.1.172 } }
第二台服务器配置:
global_defs { router_id KEEPALIVED_NODE_1 } vrrp_script check_nginx_alive { script "/etc/keepalived/check_nginx_alive.sh" interval 2 # 每隔两秒运行上一行脚本 weight 10 # 如果脚本运行成功,则升级权重+10 # weight -10 # 如果脚本运行失败,则升级权重-10 } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 50 priority 80 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { check_nginx_alive # 追踪 nginx 脚本 } virtual_ipaddress { 192.168.1.171 } } vrrp_instance VI_2 { state MASTER interface ens33 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { check_nginx_alive # 追踪 nginx 脚本 } virtual_ipaddress { 192.168.1.172 } }