环境
操作系统: CentOS7.9
keepalived: 1.35
master配置
cat > /etc/keepalived/keepalived.conf<<'EOF'
global_defs {router_id Nginx1
}
vrrp_script chk_nginx {script "/etc/keepalived/check_nginx.sh"interval 3weight -3
}
vrrp_instance VI_1 {state MASTERinterface enp0s8 virtual_router_id 101priority 120advert_int 2nopreemptauthentication {auth_type PASSauth_pass 12333}virtual_ipaddress {192.168.240.6}track_script {chk_nginx }
}EOF
cat > /etc/keepalived/check_nginx.sh<<'EOF'
B=$(pgrep nginx | wc -l)
if [[ $B -eq 0 ]]; then/usr/bin/systemctl stop keepalivedexit 1
fi
exit 0EOF
backup配置
cat > /etc/keepalived/keepalived.conf<<'EOF'
global_defs {router_id Nginx2
}
vrrp_script chk_nginx {script "/etc/keepalived/check_nginx.sh"interval 3weight -3
}
vrrp_instance VI_1 {state BACKUPinterface enp0s8 virtual_router_id 101priority 119advert_int 2nopreemptauthentication {auth_type PASSauth_pass 12333}virtual_ipaddress {192.168.240.6}track_script {chk_nginx}
}EOF
cat > /etc/keepalived/check_nginx.sh<<'EOF'
B=$(pgrep nginx | wc -l)
if [[ $B -eq 0 ]]; then/usr/bin/systemctl stop keepalivedexit 1
fi
exit 0EOF
说明
坑点
- 检测脚本
/etc/keepalived/check_nginx.sh
第一行一定不要写#!/bin/bash
- 检测脚本
/etc/keepalived/check_nginx.sh
的退出状态码不起作用,所以只能/usr/bin/systemctl stop keepalived
排错参考文档
【Linux】详解shell中source、sh、bash、./执行脚本的区别