Keepalived安装部署
安装keepalived
yum 安装keepalived
yum install keepalived
keepalived.conf配置文件进行调整,其中state设置主备状态,interface设置对应网络接口类型,priority设置优先级,备节点数字小于master节点,数字越大优先级越高。virtual_ipaddress设置虚拟IP地址,track_script设置读取检查脚本的方法,在方法中配置对应脚本位置。
主节点cdp01修改配置文件keepalived.conf为
! Configuration File for keepalived global_defs { } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 60 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { check_haproxy } virtual_ipaddress { 10.60.1.21/32 } } vrrp_script check_haproxy { script "/etc/keepalived/haproxy_check.sh" interval 2 }
备节点cdp02修改配置文件keepalived.conf为
! Configuration File for keepalived global_defs { } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 60 priority 50 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { check_haproxy } virtual_ipaddress { 10.60.1.21/32 } } vrrp_script check_haproxy { script "/etc/keepalived/haproxy_check.sh" interval 2 }
设置haproxy检查脚本haproxy_check.sh,赋予可执行权限
#!/bin/bash HAPROXY_STATUS=`ps -C haproxy --no-header | wc -l` if [[ $HAPROXY_STATUS -eq 0 ]];then date >> /etc/keepalived/haproxy_check.log echo "haproxy is not running" >> /etc/keepalived/haproxy_check.log exit 1 fi
keepalived设置开机自启
systemctl enable keepalived systemctl start keepalived systemctl status keepalived
最终keepalived配置文件为
主节点
! Configuration File for keepalived global_defs { script_user root } vrrp_script chk_haproxy { script "/etc/keepalived/haproxy_check.sh" interval 2 user root } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 60 priority 100 # nopreempt #不抢占 ,back节点配置 advert_int 1 unicast_src_ip 10.60.1.1 unicast_peer { 10.168.54.100 } authentication { auth_type PASS auth_pass 1111 } track_script { chk_haproxy } virtual_ipaddress { 10.60.1.21/32 dev eth0 label eth0:1 #10.168.54.105/32 dev eth0 label eth0:2 } }
备节点
! Configuration File for keepalived global_defs { script_user root } vrrp_script chk_haproxy { script "/etc/keepalived/haproxy_check.sh" interval 2 user root } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 60 priority 50 nopreempt advert_int 1 unicast_src_ip 10.168.54.101 unicast_peer { 10.60.1.1 } authentication { auth_type PASS auth_pass 1111 } track_script { chk_haproxy36,546% } virtual_ipaddress { 10.60.1.21/32 } }
Faq:
Q1:keepalived两个节点出现脑裂现象(双vip)
A:在配置文件中添加如下配置后,重启keepalived服务
unicast_src_ip 10.11.4.187 ##source ip unicast_peer { 10.11.4.186 ##dest ip }
Q2:keepalived在haproxy挂掉之后无法进行VIP的切换
cdp01中的haproxy挂掉之后,cdp01中查看IP地址,VIP还存在于cdp01中。
A:修改配置文件为
因为原先用的默认用户keepalived_script,这个主机中没有该用户。替换成root,把脚本方法提上来,VIP可以正常切换。