Chinaunix首页 | 论坛 | 博客
  • 博客访问: 630326
  • 博文数量: 95
  • 博客积分: 2091
  • 博客等级: 大尉
  • 技术积分: 982
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-05 19:34
文章分类

全部博文(95)

文章存档

2017年(1)

2016年(20)

2015年(10)

2014年(6)

2013年(4)

2012年(1)

2011年(9)

2010年(6)

2009年(20)

2008年(17)

2007年(1)

我的朋友

分类: 系统运维

2015-12-29 21:42:33

keepalived 安装请参考另一篇“Keepalived安装配置范例”

1 Keepalived Master机器配置文件

点击(此处)折叠或打开

  1. ! Configuration File for keepalived

  2. global_defs {
  3.    notification_email {
  4.      acassen
  5.    }
  6.    notification_email_from xxxxxx@qq.com
  7.    smtp_server smtp.qq.com
  8.    smtp_connect_timeout 30
  9.    router_id LVS_DEVEL
  10. }

  11. vrrp_script nginx_check {
  12.     script "/root/nginx_check.sh"
  13.         interval 1
  14.   weight -60
  15. }

  16. vrrp_instance VI_1 {
  17.     state MASTER
  18.     interface eth0
  19.     virtual_router_id 51
  20.     priority 100
  21.     advert_int 1
  22.     authentication {
  23.         auth_type PASS
  24.         auth_pass 1111
  25.     }
  26.     virtual_ipaddress {
  27.         192.168.10.60/24 dev eth0 label eth0:1
  28.     }

  29.     track_script
  30.     {
  31.         nginx_check
  32.     }
  33. }
2  Keepalived Backup机器配置文件

点击(此处)折叠或打开

  1. ! Configuration File for keepalived

  2. global_defs {
  3.    notification_email {
  4.      acassen
  5.    }
  6.    notification_email_from xxxxxx@qq.com
  7.    smtp_server smtp.qq.com
  8.    smtp_connect_timeout 30
  9.    router_id LVS_DEVEL
  10. }

  11. vrrp_script nginx_check
  12. {
  13.     script "/root/nginx_check.sh"
  14.     interval 1 
  15.     weigh -60
  16. }

  17. vrrp_instance VI_1 {
  18.     state BACKUP
  19.     interface eth0
  20.     virtual_router_id 51
  21.     priority 80
  22.     advert_int 1
  23.     authentication {
  24.         auth_type PASS
  25.         auth_pass 1111
  26.     }
  27.     virtual_ipaddress {
  28.         192.168.10.60/24 dev eth0 label eth0:1
  29.     }

  30.     track_script
  31.     {
  32.         nginx_check
  33.     }

  34. }


点击(此处)折叠或打开

  1. ! Configuration File for keepalived

  2. global_defs {
  3.    notification_email {
  4.      acassen
  5.    }
  6.    notification_email_from xxxxxxxxx@qq.com
  7.    smtp_server smtp.qq.com
  8.    smtp_connect_timeout 30
  9.    router_id LVS_DEVEL
  10. }

  11. vrrp_script nginx_check
  12. {
  13.     script "/root/nginx_check.sh"
  14.     interval 1 ###检测时间间隔 1s###
  15.     weigh -60 ###如果条件成立,权重-60###
  16. }

  17. vrrp_instance VI_1 {
  18.     state BACKUP
  19.     interface eth0
  20.     virtual_router_id 51
  21.     priority 80
  22.     advert_int 1
  23.     authentication {
  24.         auth_type PASS
  25.         auth_pass 1111
  26.     }
  27.     virtual_ipaddress {
  28.         192.168.10.60/24 dev eth0 label eth0:1
  29.     }

  30.     track_script
  31.     {
  32.         nginx_check
  33.     }

  34. }
3  Nginx 检查脚本,并放到master与backup机器的/root目录下

点击(此处)折叠或打开

  1. [root@localhost ~]# cat /root/nginx_check.sh
  2. #!/bin/bash
  3. alive=`netstat -pant|awk '/0.0.0.0:80/&&/LISTEN/'|wc -l`
  4. if [ $alive -eq 1 ]; then
  5.     exit 0
  6. else
  7.     exit 1
  8. fi

4  测试,停止Master上的nginx,隔一会再启动nginx
  从日志中可以看出,
     1   当Nginx停止,Keepalived检测到"VRRP_Script(nginx_check) failed", 开始切换到backup机器,并将VIP从master中remove。
     2   当Nginx恢复,Keepalived检测到"VRRP_Script(nginx_check) succeeded, 开始切换到master机器,并添加VIP。

点击(此处)折叠或打开

  1. Dec 30 05:36:15 localhost Keepalived_vrrp[13636]: Configuration is using : 68820 Bytes
  2. Dec 30 05:36:15 localhost Keepalived_vrrp[13636]: Using LinkWatch kernel netlink reflector...
  3. Dec 30 05:36:15 localhost Keepalived_vrrp[13636]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
  4. Dec 30 05:36:15 localhost Keepalived_healthcheckers[13635]: Netlink reflector reports IP 192.168.10.46 added
  5. Dec 30 05:36:15 localhost Keepalived_healthcheckers[13635]: Netlink reflector reports IP fe80::5054:ff:feef:323d added
  6. Dec 30 05:36:15 localhost Keepalived_healthcheckers[13635]: Registering Kernel netlink reflector
  7. Dec 30 05:36:15 localhost Keepalived_healthcheckers[13635]: Registering Kernel netlink command channel
  8. Dec 30 05:36:15 localhost Keepalived_healthcheckers[13635]: Opening file '/etc/keepalived/keepalived.conf'.
  9. Dec 30 05:36:15 localhost Keepalived_healthcheckers[13635]: Configuration is using : 11386 Bytes
  10. Dec 30 05:36:15 localhost Keepalived_healthcheckers[13635]: Using LinkWatch kernel netlink reflector...
  11. Dec 30 05:36:15 localhost Keepalived_vrrp[13636]: VRRP_Script(nginx_check) succeeded
  12. Dec 30 05:36:16 localhost Keepalived_vrrp[13636]: VRRP_Instance(VI_1) Transition to MASTER STATE
  13. Dec 30 05:36:16 localhost Keepalived_vrrp[13636]: VRRP_Instance(VI_1) Received lower prio advert, forcing new election
  14. Dec 30 05:36:17 localhost Keepalived_vrrp[13636]: VRRP_Instance(VI_1) Entering MASTER STATE
  15. Dec 30 05:36:17 localhost Keepalived_vrrp[13636]: VRRP_Instance(VI_1) setting protocol VIPs.
  16. Dec 30 05:36:17 localhost Keepalived_vrrp[13636]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.10.60
  17. Dec 30 05:36:17 localhost Keepalived_healthcheckers[13635]: Netlink reflector reports IP 192.168.10.60 added
  18. Dec 30 05:36:22 localhost Keepalived_vrrp[13636]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.10.60
  19. Dec 30 05:38:34 localhost Keepalived_vrrp[13636]: VRRP_Script(nginx_check) failed
  20. Dec 30 05:38:36 localhost Keepalived_vrrp[13636]: VRRP_Instance(VI_1) Received higher prio advert
  21. Dec 30 05:38:36 localhost Keepalived_vrrp[13636]: VRRP_Instance(VI_1) Entering BACKUP STATE
  22. Dec 30 05:38:36 localhost Keepalived_vrrp[13636]: VRRP_Instance(VI_1) removing protocol VIPs.
  23. Dec 30 05:38:36 localhost Keepalived_healthcheckers[13635]: Netlink reflector reports IP 192.168.10.60 removed
  24. Dec 30 05:38:54 localhost Keepalived_vrrp[13636]: VRRP_Script(nginx_check) succeeded
  25. Dec 30 05:38:56 localhost Keepalived_vrrp[13636]: VRRP_Instance(VI_1) forcing a new MASTER election
  26. Dec 30 05:38:56 localhost Keepalived_vrrp[13636]: VRRP_Instance(VI_1) forcing a new MASTER election
  27. Dec 30 05:38:57 localhost Keepalived_vrrp[13636]: VRRP_Instance(VI_1) Transition to MASTER STATE
  28. Dec 30 05:38:58 localhost Keepalived_vrrp[13636]: VRRP_Instance(VI_1) Entering MASTER STATE
  29. Dec 30 05:38:58 localhost Keepalived_vrrp[13636]: VRRP_Instance(VI_1) setting protocol VIPs.
  30. Dec 30 05:38:58 localhost Keepalived_vrrp[13636]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.10.60
  31. Dec 30 05:38:58 localhost Keepalived_healthcheckers[13635]: Netlink reflector reports IP 192.168.10.60 added
  32. Dec 30 05:39:03 localhost Keepalived_vrrp[13636]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.10.60


  
参考:


阅读(1392) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~