检查nginx 是否正常
#!/bin/sh
nginxpid=$(ps -C nginx --no-header|wc -l)
#1.判断Nginx是否存活,如果不存活则尝试启动Nginx
if [ $nginxpid -eq 0 ];then
systemctl start nginx
sleep 3
#2.等待3秒后再次获取一次Nginx状态
nginxpid=$(ps -C nginx --no-header|wc -l)
#3.再次进行判断, 如Nginx还不存活则停止Keepalived,让地址进行漂移,并退出脚本
if [ $nginxpid -eq 0 ];then
systemctl stop keepalived
fi
fi
#给脚本增加执行权限
keepalived 中添加:
script "/etc/keepalived/check_split_brain.sh"
[root@LB01 ~]# cat /etc/keepalived/keepalived.conf
global_defs {
router_id LB01
}
vrrp_script check_nginx {
script "/etc/keepalived/check_split_brain.sh"
interval 5
}
vrrp_instance VI_1 {
state MASTER
priority 150
interface eth0
virtual_router_id 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3
}
}
#调用并运行脚本
track_script {
check_nginx
}
检查脑裂:
[root@LB01 ~]# cat /etc/keepalived/check_split_brain.sh
LB01_VIP_Number=`ip add | grep 10.0.0.3 | wc -l`
LB02_VIP_Number=`ssh '10.0.0.6' ip add | grep 10.0.0.3 | wc -l`
if [ $LB01_VIP_Number -eq 1 -a $LB02_VIP_Number -eq 1 ]
then
systemctl stop keepalived
fi
欢迎来撩 :shell 编程从0到1