92.168.222.250 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.222.137 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@backup keepalived]# systemctl enable --now keepalived.service
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.
编写脚本
master
[root@master ~]# mkdir /scripts
[root@master ~]# cd /scripts/
[root@master scripts]# vim check_haproxy.sh
[root@master scripts]# cat check_haproxy.sh
#!/bin/bash
haproxy_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bhaproxy\b'|wc -l)
if [ $haproxy_status -lt 1 ];then
systemctl stop keepalived
fi
[root@master scripts]# chmod +x check_haproxy.sh
[root@master scripts]# ll
total 4
-rwxr-xr-x. 1 root root 149 Oct 10 02:50 check_haproxy.sh
[root@master scripts]# vim notify.sh
[root@master scripts]# cat notify.sh
#!/bin/bash
VIP=$2
case "$1" in
master)
haproxy_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bhaproxy\b'|wc -l)
if [ $haproxy_status -lt 1 ];then
systemctl start haproxy
fi
;;
backup)
haproxy_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bhaproxy\b'|wc -l)
if [ $haproxy_status -gt 0 ];then
systemctl stop haproxy
fi
;;
*)
echo "Usage:$0 master|backup VIP"
;;
esac
[root@master scripts]# chmod +x notify.sh
[root@master scripts]# ll
total 8
-rwxr-xr-x. 1 root root 149 Oct 10 02:50 check_haproxy.sh
-rwxr-xr-x. 1 root root 413 Oct 10 02:56 notify.sh
backup
[root@backup keepalived]# cd
[root@backup ~]# mkdir /scripts
[root@backup ~]# cd /scripts/
[root@backup scripts]# scp root@192.168.222.250:/scripts/notify.sh .
The authenticity of host '192.168.222.250 (192.168.222.250)' can't be established.
ECDSA key fingerprint is SHA256:y11UDaNXs3AnvVUnZQfAim2VHAplF09YOvQp2NemHyk.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.222.250' (ECDSA) to the list of known hosts.
root@192.168.222.250's password:
notify.sh 100% 413 429.2KB/s 00:00
[root@backup scripts]# ll
total 4
-rwxr-xr-x. 1 root root 413 Oct 10 02:58 notify.sh
配置keepalived加入监控脚本的配置
master
[root@master scripts]# cd
[root@master ~]# vim /etc/keepalived/keepalived.conf
[root@master ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_script haproxy_check { //添加
script "/scripts/check_haproxy.sh"
interval 1
weight -20
}
vrrp_instance VI_1 { //这里主备节点需要一致
state BACKUP
interface ens33 //网卡
virtual_router_id 51
priority 100 //这里比备节点的高
advert_int 5
authentication {
auth_type PASS
auth_pass tushanbu //密码(可以随机生成)
}
virtual_ipaddress {
192.168.222.133 //高可用虚拟IP(VIP)地址
}
track_script { //添加
haproxy_check //添加
} //添加
notify_master "/scripts/notify.sh master 192.168.222.133" //添加
}
virtual_server 192.168.222.133 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.222.250 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.222.137 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry