ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

Keepalived健康检查

2026/10/8 9:57:56 拓冰建站 浏览量
Keepalived健康检查

方法一:killall -0 nginx实现

修改keepalived配置文件
! Configuration File for keepalivedglobal_defs {router_id LVS_141
}
vrrp_script chk_ngx {script "killall -0 nginx"
}
vrrp_instance nginx {state MASTERinterface ens160virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.233.50}track_script {chk_ngx}
}
systemctl start keepalived
systemctl start nginx

方法二:脚本实现

创建脚本:

vim /etc/keepalived/check.sh

脚本: 

#!/bin/bashsystemctl status nginx | grep "active(running)" > /dev/nullif [ $? -ne 0 ]; thensystemctl restart nginx &> /dev/nullsleep 1systemctl status nginx | grep "active(running)" > /dev/nullif [ $? -ne 0 ]; thensystemctl stop keepalivedfi
fi

修改keepalived配置文件:


! Configuration File for keepalivedglobal_defs {router_id LVS_141
}
vrrp_script chk_ngx {script "/etc/keepalived/check.sh"
}
vrrp_instance nginx {state MASTERinterface ens160virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.233.50}track_script {chk_ngx}
}
systemctl start keepalived
systemctl start nginx