一、实验环境
serverA:192.168.1.110
serverB:192.168.1.120
vip: 192.168.1.130
二、配置
serverA
$ cat /etc/keepalived/keepalived.conf
##################################################################
! Configuration File for keepalived
global_defs {
router_id openresty_kp
}
#Insertcheckscript
#InsertvrrpInstance
vrrp_script chk_openresty {
script "/etc/keepalived/check_openresty.sh"
interval 2
}
vrrp_instance openresty {
state BACKUP
interface eth0
virtual_router_id 55
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 111111
}
unicast_src_ip 192.168.1.110
unicast_peer {
192.168.1.120
}
virtual_ipaddress {
192.168.1.130/24
}
track_script {
chk_openresty
}
}
##################################################################
serverB
$ cat /etc/keepalived/keepalived.conf
####################################################################
! Configuration File for keepalived
global_defs {
router_id openresty_kp
}
#Insertcheckscript
#InsertvrrpInstance
vrrp_script chk_openresty {
script "/etc/keepalived/check_openresty.sh"
interval 2
}
vrrp_instance openresty {
state BACKUP
interface eth0
virtual_router_id 55
priority 80
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 111111
}
unicast_src_ip 192.168.1.120
unicast_peer {
192.168.1.110
}
virtual_ipaddress {
192.168.1.130/24
}
track_script {
chk_openresty
}
}
#####################################################################
检测脚本
# cat /etc/keepalived/check_openresty.sh
#####################################################################
#!/bin/bash
if ! ps aux | grep -w "nginx" | grep -v "grep" | grep -w "master process" > /dev/null 2>&1; then
/usr/local/openresty/nginx/sbin/nginx
if ! ps aux | grep -w "nginx" | grep -v "grep" | grep -w "master process" > /dev/null 2>&1; then
exit 1
fi
fi
######################################################################
三、配置说明
1. keepalived采用单播模式,避免局域网内 有其他virtual_router_id相同的keepalived实例,导致keepalived异常
2. keepalived采用非抢占模式, state 均设置为BACKUP ,根据脚本检测退出码决定优先级高低,而不是priority 值大小