重新整理了下,发现网上有大佬写的更好,就直接上大佬的了;
大佬的传送门是:
https://www.cnblogs.com/mmdln/p/8988977.html
以下为正文:
任务需求:
默认每隔3秒钟执行一次检测脚本,检查nginx服务是否启动,如果没启动就把nginx服务启动起来,如果启动不成功,就把keepalived服务down掉,让漂浮到备keepalived上
脚本内容:
#!/bin/bash
run=`ps -C nginx --no-header | wc -l`
if [ $run -eq 0 ];then
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx
sleep 3
if [ `ps -C nginx --no-header | wc -l` ];then
killall keepalived
fi
fi
keepalived配置
vrrp_script check_ng {
script " /etc/keepalived/check_nginx.sh "
interval 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100/24
}
track_script {
check_ng
}
}
注意事项:
1.检测脚本要写在vrrp_instance的前面且花括号一定要有空格;
2.脚本调用命令trace_script要写在vip的后面;