3. 测试

根据上面的配置,初始化状态:172.29.88.224 (itoatest1,MASTER,101),172.29.88.222itoatest2,BACKUP,100),nginx和keepalived都启动,虚拟IP 172.29.88.222 在 itoatest1 上:

  1. # 使用ip命令配置的地址,ifconfig查看不了
  2. [root@itoatest1 nginx-1.6]# ip a|grep eth0
  3. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  4. inet 172.29.88.224/24 brd 172.29.88.255 scope global eth0
  5. inet 172.29.88.222/32 scope global eth0

浏览器访问 172.29.88.222 或域名,OK。

直接关闭 itoatest1 上的nginx:/usr/local/nginx-1.6/sbin/nginx -s stop

  1. [root@localhost keepalived]# ip a|grep eth0
  2. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  3. inet 172.29.88.224/24 brd 172.29.88.255 scope global eth0

vip消失,漂移到 itoatest2:

nginx-keepalived-vip.png

同时可以看到两台服务器上 /var/log/messages

  1. ## itoatest1
  2. Jun 5 16:44:01 itoatest1 Keepalived_vrrp[44875]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.29.88.222
  3. Jun 5 16:44:06 itoatest1 Keepalived_vrrp[44875]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.29.88.222
  4. Jun 5 16:44:46 itoatest1 Keepalived_vrrp[44875]: VRRP_Script(chk_nginx) failed
  5. Jun 5 16:44:48 itoatest1 Keepalived_vrrp[44875]: VRRP_Instance(VI_1) Received higher prio advert
  6. Jun 5 16:44:48 itoatest1 Keepalived_vrrp[44875]: VRRP_Instance(VI_1) Entering BACKUP STATE
  7. Jun 5 16:44:48 itoatest1 Keepalived_vrrp[44875]: VRRP_Instance(VI_1) removing protocol VIPs.
  8. Jun 5 16:44:48 itoatest1 Keepalived_healthcheckers[44874]: Netlink reflector reports IP 172.29.88.222 removed
  9. ## itoatest2
  10. Jun 5 16:44:00 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) Transition to MASTER STATE
  11. Jun 5 16:44:00 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) Received higher prio advert
  12. Jun 5 16:44:00 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) Entering BACKUP STATE
  13. Jun 5 16:44:48 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) forcing a new MASTER election
  14. Jun 5 16:44:48 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) forcing a new MASTER election
  15. Jun 5 16:44:49 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) Transition to MASTER STATE
  16. Jun 5 16:44:50 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) Entering MASTER STATE
  17. Jun 5 16:44:50 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) setting protocol VIPs.
  18. Jun 5 16:44:50 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.29.88.222
  19. Jun 5 16:44:50 itoatest2 Keepalived_healthcheckers[35554]: Netlink reflector reports IP 172.29.88.222 added
  20. Jun 5 16:44:55 itoatest2 Keepalived_vrrp[35555]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.29.88.222

你也可以通过在两台服务器上抓包来查看 优先级priority 的变化:

  1. ## itoatest1 上
  2. ## 直接输出,或后加 -w itoatest-kl.cap存入文件用wireshark查看
  3. # tcpdump -vvv -n -i eth0 dst 224.0.0.18 and src 172.29.88.224

nginx-keepalived-prio.png

参考

  • 使用Keepalived实现Nginx高可用性
  • High Availability Support Based on keepalived
  • nginx+keepalived实现双机热备的高可用
  • LVS原理详解及部署之五:LVS+keepalived实现负载均衡&高可用
  • Keepalived双主模型中vrrp_script中权重改变故障排查
  • 虚拟路由器冗余协议原理篇】VRRP详解
  • Keepalived原理与实战精讲

来源:http://seanlook.com/2015/05/18/nginx-keepalived-ha/




相关内容