Keepalived配置nginx自动重启
1. 增加nginx 检查脚本
vim /etc/keepalived/check_nginx_alive_or_not. sh
#!/bin/bash
A=`ps -C nginx --no-header | wc -l`
# 判断nginx是否宕机,如果宕机尝试重新启动
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
# 等待3秒检查nginx 如果没有启动成功 则停止keepalived 启动备用机
sleep 3
if [`ps -C nginx --no-header I wc -l` -eq 0 ];then
killall keepalived
fi
fi
# 增加运行权限
chmod +x /etc/keepalived/check_nginx_alive_or_not.sh
2. 配置keepalived监听nginx脚本
vrrp_script check_nginx_alived {
script "/etc/keepalived/check_nginx_alive_or_not.sh"
interval 2 #每隔2秒运行一次
weight 10 # 运行成功 权重加10
}
3. 在vrrp_ instance 中新增监控脚本
track_script {
check_nginx_alive # 添加nginx脚本
}
4. 重启keepalived使配置文件生效
systemctl restart keepalived