它进行的判断就是,如果系统中有命令包含 haproxy.cfg 的进程(假定这种情况就代表haproxy正在运行),就反馈 0 , 否则反馈 1
代码语言:javascript复制[root@h101 script]# ps faux | grep -v grep | grep haproxy
haproxy 23083 0.0 0.0 14260 1408 ? Ss 22:43 0:00 /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg
[root@h101 script]# /usr/local/keepalived/script/chk_haproxy.bash
[root@h101 script]# echo $?
0
[root@h101 script]# kill 23083
[root@h101 script]# ps faux | grep -v grep | grep haproxy
[root@h101 script]# /usr/local/keepalived/script/chk_haproxy.bash
[root@h101 script]# echo $?
1
[root@h101 script]#
配置keepalived
代码语言:javascript复制[root@h101 script]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_101
}
vrrp_script checkhaproxy {
script "/usr/local/keepalived/script/chk_haproxy.bash"
weight -20
interval 3
}
vrrp_instance VI_222 {
state BACKUP
interface eth2
virtual_router_id 222
priority 108
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
checkhaproxy
}
virtual_ipaddress {
192.168.100.222/24
}
}
[root@h101 script]#
----------
[root@h102 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_102
}
vrrp_script checkhaproxy {
script "/usr/local/keepalived/script/chk_haproxy.bash"
weight -20
interval 3
}
vrrp_instance VI_222 {
state BACKUP
interface eth2
virtual_router_id 222
priority 115
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
checkhaproxy
}
virtual_ipaddress {
192.168.100.222/24
}
}
[root@h102 ~]#