Keepalived使用小结
编译安装
1、安装环境
CentOS release 6.4 Based on Linux 2.6.32,安装1.2.9,没问题
在Red Hat Enterprise Linux Server release 5.3 (Tikanga) Based on 2.6.18 安装1.2.9,有问题,1.2.8没问题
2、获取源码
http://www.keepalived.org/software/keepalived-1.2.9.tar.gz
http://www.keepalived.org/software/keepalived-1.2.8.tar.gz
3、编译
tar -xvf keepalived-1.2.x.tar.gz
mkdir /opt/keepalived
cd keepalived-1.2.x
./configure --prefix=/opt/keepalived
make
make install
4、配置
假设有两台机器A,B
A的keepalived.conf配置:
global_defs { router_id LVS_DEVEL_TEST } vrrp_script Monitor_Haroxy { # script "/home/plat/ott_release/script/haproxy_keepalived.sh" script "killall -0 haproxy" interval 3 weight -20 } vrrp_instance VI_HA { state BACKUP interface em1 #网卡接口 virtual_router_id 53 priority 90 advert_int 1 nopreempt authentication { auth_type PASS auth_pass 1111 } #notify_master /home/plat/ ott_release/script/redis_master.sh #notify_backup/home/plat/ott_release/script/redis_backup.sh track_script { Monitor_Haproxy } virtual_ipaddress { 10.0.63.237 } }
B的keepalived.conf配置:
global_defs { router_id LVS_DEVEL_TEST } vrrp_script Monitor_Haroxy { script "/home/plat/ott_release/script/haproxy_keepalived.sh" #script "killall -0 haproxy" interval 3 weight -20 } vrrp_instance VI_HA { state BACKUP interface br-ex virtual_router_id 53 priority 80 advert_int 1 nopreempt authentication { auth_type PASS auth_pass 1111 } notify_master /home/plat/ ott_release/script/redis_master.sh notify_backup/home/plat/ott_release/script/redis_backup.sh track_script { Monitor_Haproxy } virtual_ipaddress { 10.0.63.237 } }
以下配置,A、B是相同的
/home/plat/ott_release/script/haproxy_keepalived.sh示例如下:
-
#!/bin/bash
count = `ps aux | grep -v grep | grep haproxy | wc -l`
if [ $count > 0 ]; then
exit 0
else
exit 1
fi
注:
keepalived会定时执行脚本并对脚本执行的结果进行分析,动态调整vrrp_instance的优先级。
-
如果脚本执行结果为0,并且weight配置的值大于0,则优先级相应的增加
-
如果脚本执行结果非0,并且weight配置的值小于0,则优先级相应的减少
-
其他情况,维持原本配置的优先级,即配置文件中priority对应的值。
说明几点:
1)优先级不会不断的提高或者降低
2)可以编写多个检测脚本并为每个检测脚本设置不同的weight
3)不管提高优先级还是降低优先级,最终优先级的范围是在[1,254],不会出现优先级小于等于0或者优先级大于等于255的情况
说明一种情况,以上配置的两台机器,启动后A weight高,所以变成Master,如果Master检测到进程出问题,将自身weight减少20即60,小于备机,所以备机切换为Master;当原Master即A恢复,由于其weight为80,会抢占B成为Master。
启动时,在安装目录下运行:./sbin/keepalived -f conf/keepalived.conf -c run/checker.pid -r run/vrrp.pid -p run/pid.pid
运行两个keepalived进程时,例如:./sbin/keepalived -f conf/keepalived1.conf -c run/checker1.pid -r run/vrrp1.pid -p run/pid1.pid