1. Keepalived的安装

1.1. yum install方式

  1. yum install -y keepalived

1.2. 安装包编译方式

更多安装包参考:http://www.keepalived.org/download.html

  1. wget http://www.keepalived.org/software/keepalived-2.0.7.tar.gz
  2. tar zxvf keepalived-2.0.7.tar.gz
  3. cd keepalived-2.0.7
  4. ./configure --bindir=/usr/bin --sbindir=/usr/sbin --sysconfdir=/etc --mandir=/usr/share
  5. make && make install

2. 常用配置

keepalived配置文件路径:/etc/keepalived/keepalived

2.1. MASTER(主机配置)

  1. global_defs {
  2. router_id proxy-keepalived
  3. }
  4. vrrp_script check_nginx {
  5. script "/etc/keepalived/scripts/check_nginx.sh"
  6. interval 3
  7. weight 2
  8. }
  9. vrrp_instance VI_1 {
  10. state BACKUP
  11. interface eth2
  12. virtual_router_id 15
  13. priority 100
  14. advert_int 1
  15. authentication {
  16. auth_type PASS
  17. auth_pass xxx
  18. }
  19. track_script {
  20. check_nginx
  21. }
  22. virtual_ipaddress {
  23. 180.101.115.139
  24. 218.98.38.29
  25. }
  26. nopreempt
  27. notify_master "/etc/keepalived/keepalived_notify.sh master"
  28. notify_backup "/etc/keepalived/keepalived_notify.sh backup"
  29. notify_fault "/etc/keepalived/keepalived_notify.sh fault"
  30. notify_stop "/etc/keepalived/keepalived_notify.sh stop"
  31. }

2.2. BACKUP(备机配置)

  1. global_defs {
  2. router_id proxy-keepalived
  3. }
  4. vrrp_script check_nginx {
  5. script "/etc/keepalived/scripts/check_nginx.sh"
  6. interval 3
  7. weight 2
  8. }
  9. vrrp_instance VI_1 {
  10. state BACKUP
  11. interface eth2
  12. virtual_router_id 15
  13. priority 99
  14. advert_int 1
  15. authentication {
  16. auth_type PASS
  17. auth_pass xxx
  18. }
  19. track_script {
  20. check_nginx
  21. }
  22. virtual_ipaddress {
  23. 180.101.115.139
  24. 218.98.38.29
  25. }
  26. nopreempt
  27. notify_master "/etc/keepalived/keepalived_notify.sh master"
  28. notify_backup "/etc/keepalived/keepalived_notify.sh backup"
  29. notify_fault "/etc/keepalived/keepalived_notify.sh fault"
  30. notify_stop "/etc/keepalived/keepalived_notify.sh stop"
  31. }

3. 注意事项

1、指定Nginx健康检测脚本:/etc/keepalived/scripts/check_nginx.sh

2、主备配置差别主要为(建议这么配置):

以下两种方式的配置,当其中一台机器keepalived挂掉后会自动VIP切到另一台机器,当挂掉机器keepalived恢复后不会抢占VIP,该方式可以避免机器恢复再次切VIP所带来的影响。

  • 主机:(state BACKUP;priority 100)
  • 备机:(state BACKUP;priority 99)
  • 非抢占:nopreempt

    或者:

  • 主机:(state MASTER;priority 100)

  • 备机:(state BACKUP;priority 100)
  • 默认抢占

3、指定VIP

  1. virtual_ipaddress {
  2. 180.101.115.139
  3. 218.98.38.29
  4. }

4、可以指定为非抢占:nopreempt,即priority高不会抢占已经绑定VIP的机器。

5、制定绑定IP的网卡: interface eth2

6、可以指定keepalived状态变化通知

  1. notify_master "/etc/keepalived/keepalived_notify.sh master"
  2. notify_backup "/etc/keepalived/keepalived_notify.sh backup"
  3. notify_fault "/etc/keepalived/keepalived_notify.sh fault"
  4. notify_stop "/etc/keepalived/keepalived_notify.sh stop"

7、virtual_router_id 15值,主备值一致,但建议不应与集群中其他Nginx机器上的相同,如果同一个网段配置的virtual_router_id 重复则会报错,选择一个不重复的0~255之间的值,可以用以下命令查看已存在的vrid。

  1. tcpdump -nn -i any net 224.0.0.0/8

4. 常用脚本

4.1. Nginx健康检测脚本

在Nginx配置目录下(/etc/nginx/conf.d/)增加health.conf的配置文件,该配置文件用于配置Nginx health的接口。

  1. server {
  2. listen 80 default_server;
  3. server_name localhost;
  4. default_type text/html;
  5. return 200 'Health';
  6. }

Nginx健康检测脚本:/etc/keepalived/scripts/check_nginx.sh

4.1.1. 检查接口调用是否为200

  1. #!/bin/sh
  2. set -x
  3. timeout=30 #指定默认30秒没返回200则为非健康,该值可根据实际调整
  4. if [ -n ${timeout} ];then
  5. httpcode=`curl -sL -w %{http_code} -m ${timeout} http://localhost -o /dev/null`
  6. else
  7. httpcode=`curl -sL -w %{http_code} http://localhost -o /dev/null`
  8. fi
  9. if [ ${httpcode} -ne 200 ];then
  10. echo `date`': nginx is not healthy, return http_code is '${httpcode} >> /etc/keeperalived/keepalived.log
  11. killall keepalived
  12. exit 1
  13. else
  14. exit 0
  15. fi

4.1.2. 检查Nginx进程是否运行

  1. #!/bin/sh
  2. if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
  3. echo "$(date) nginx pid not found">>/etc/keepalived/keepalived.log
  4. killall keepalived
  5. fi

4.2. Keepalived状态通知脚本

  1. #!/bin/bash
  2. set -x
  3. warn_receiver=$1
  4. ip=$(ifconfig bond0|grep inet |awk '{print $2}')
  5. warningInfo="${ip}_keepalived_changed_status_to_$1"
  6. warn-report --user admin --key=xxxx --target=${warn_receiver} ${warningInfo}
  7. echo $(date) $1 >> /etc/keepalived/status

说明:

  1. ip获取本机IP,本例中IP获取是bond0的IP,不同机器网卡名称不同需要修改为对应网卡名称。
  2. 告警工具根据自己指定。