一、实验环境
操作系统: CentOS7.5 Minimal
serverA: 192.18.1.101
serverB: 192.168.1.102
vip: 192.168.1.110
一、关闭selinux
# setenforce 0
# sed -i 's/^SELINUX=.*/SELINUX=permissive/g' /etc/selinux/config
二、 设置 防火墙
firewalld和iptables二选一

三、配置serverA
# vim /etc/keepalived/keepalived.conf
###################################
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_script check {
script "/etc/keepalived/check_nginx.sh"
interval 2
fail 2
rise 2
}
vrrp_instance VI_1 {
state BACKUP
interface eno16777736
virtual_router_id 125
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check
}
virtual_ipaddress {
192.168.1.110/24 dev eno16777736
}
}
#######################################
四、配置serverB
# vim /etc/keepalived/keepalived.conf
#######################################
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_script check {
script "/etc/keepalived/check_nginx.sh"
interval 2
fail 2
rise 2
}
vrrp_instance VI_1 {
state BACKUP
interface eno16777736
virtual_router_id 125
priority 90
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check
}
virtual_ipaddress {
192.168.1.110/24 dev eno16777736
}
}
########################################
# chmod 750 /etc/keepalived/check_nginx.sh
# cat /etc/keepalived/check_nginx.sh
########################################
#!/bin/bash
nginx_count1=$(ps -C nginx --no-heading|wc -l)
if [ $nginx_count1 -eq 0 ];then
systemctl start nginx
nginx_count2=$(ps -C nginx --no-heading|wc -l)
if [ $nginx_count2 -gt 0 ];then
exit 0
else
exit 1
fi
fi
exit 0
##############################################
五、参考
Keepalived Check and Notify Scripts
https://tobru.ch/keepalived-check-and-notify-scripts
https://serverfault.com/questions/718132/keepalived-vrrp-script-not-failing-over
https://e-mc2.net/keepalived-documentation-nightmare
https://docs.oracle.com/cd/E37670_01/E41138/html/section_hxz_zdw_pr.html