实验:实现单主模式的Nginx反向代理的高可用 keepalived+nginx
一、前期规则设置
CIP:192.168.80.50 gateway:192.168.10.2
KA1:DIP:192.168.80.100 gateway:192.168.80.2 VIP: 192.168.80.222/24
KA2: RIP:192.168.80.110 gateway:192.168.80.2 VIP: 192.168.80.222/24
RS1: RIP:192.168.80.120 gateway:192.168.80.2
RS2: RIP:192.168.80.130 gateway:192.168.80.2
KA服务器安装 yum install keepalived nginx RS服务器安装 yum install apache/nginx 一、 KA两个节点都配置nginx反向代理 http { upstream websrvs { server 192.168.80.120:80; server 192.168.80.130:80; } server { listen 80; location /{ proxy_pass http://websrvs/; } } } 二、KA脚本两个,要执行权限 cat /etc/keepalived/check_nginx.sh #!/bin/bash killall -0 nginx &> /dev/null || exit 1 三、KA在两个节点都配置实现nginx反向代理高可用 cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { 348987564@qq.com } notification_email_from root@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id localhost7A vrrp_iptables #vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 vrrp_mcast_group4 224.0.100.100 } vrrp_script chk_nginx { #检查nginx是否运行,不运行权重就减15.也就是变成了backup. script "/etc/keepalived/check_nginx.sh" interval 1 weight -25 fall 3 rise 2 timeout 2 } vrrp_instance zzhz { state MASTER interface eth0 virtual_router_id 88 priority 88 advert_int 2 authentication { auth_type PASS auth_pass centos } virtual_ipaddress { 192.168.80.222/24 dev eth0 label eth0:1 } track_script { chk_nginx } } 脚本路径 [root@localhost7A ~]# cat /etc/keepalived/check_nginx.sh #!/bin/bash /usr/bin/killall -0 nginx [root@localhost7A ~]# chmod +x /etc/keepalived/check_nginx.sh 四:另一台设置变化部分 router_id ka1 state BACKUP: priority 77 要小于MASTER。 五 测试: ka中的nginx 19:03:11.680037 IP 192.168.80.100 > 224.100.100.43: VRRPv2, Advertisement, vrid 51, prio 95, authtype simple, intvl 2s, length 20 19:03:13.681014 IP 192.168.80.100 > 224.100.100.43: VRRPv2, Advertisement, vrid 51, prio 75, authtype simple, intvl 2s, length 20 #nginx关 19:03:13.681889 IP 192.168.80.110 > 224.100.100.43: VRRPv2, Advertisement, vrid 51, prio 85, authtype simple, intvl 2s, length 20 19:04:03.744446 IP 192.168.80.110 > 224.100.100.43: VRRPv2, Advertisement, vrid 51, prio 85, authtype simple, intvl 2s, length 20 19:04:03.745267 IP 192.168.80.100 > 224.100.100.43: VRRPv2, Advertisement, vrid 51, prio 95, authtype simple, intvl 2s, length 20 #nginx开 [root@localhost7A ~]# tailf /var/log/messages Aug 4 19:05:42 localhost7A Keepalived_vrrp[63914]: /etc/keepalived/check_nginx.sh exited with status 1 Aug 4 19:05:43 localhost7A Keepalived_vrrp[63914]: /etc/keepalived/check_nginx.sh exited with status 1 Aug 4 19:05:43 localhost7A Keepalived_vrrp[63914]: VRRP_Script(chk_nginx) failed Aug 4 19:05:44 localhost7A Keepalived_vrrp[63914]: /etc/keepalived/check_nginx.sh exited with status 1 Aug 4 19:05:45 localhost7A Keepalived_vrrp[63914]: VRRP_Instance(zzhz) Changing effective priority from 95 to 75 Aug 4 19:05:45 localhost7A Keepalived_vrrp[63914]: /etc/keepalived/check_nginx.sh exited with status 1 Aug 4 19:05:45 localhost7A Keepalived_vrrp[63914]: VRRP_Instance(zzhz) Received advert with higher priority 85, ours 75 #nginx关 Aug 4 19:05:45 localhost7A Keepalived_vrrp[63914]: VRRP_Instance(zzhz) Entering BACKUP STATE Aug 4 19:05:45 localhost7A Keepalived_vrrp[63914]: VRRP_Instance(zzhz) removing protocol VIPs. Aug 4 19:05:45 localhost7A avahi-daemon[934]: Withdrawing address record for 192.168.80.222 on eth0. Aug 4 19:05:46 localhost7A Keepalived_vrrp[63914]: /etc/keepalived/check_nginx.sh exited with status 1 Aug 4 19:05:47 localhost7A Keepalived_vrrp[63914]: /etc/keepalived/check_nginx.sh exited with status 1 Aug 4 19:06:23 localhost7A Keepalived_vrrp[63914]: VRRP_Instance(zzhz) Changing effective priority from 75 to 95 #nginx开 Aug 4 19:06:23 localhost7A Keepalived_vrrp[63914]: VRRP_Instance(zzhz) forcing a new MASTER election Aug 4 19:06:23 localhost7A avahi-daemon[934]: Received response from host 192.168.80.1 with invalid source port 49695 on interface 'eth0.0' Aug 4 19:06:25 localhost7A Keepalived_vrrp[63914]: VRRP_Instance(zzhz) Transition to MASTER STATE