[[email protected] ~]# yum -y install keepalived
[[email protected] ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_MASTER-A
}
vrrp_script mysql {
script "/opt/mysql.sh"
interval 2
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state BACKUP
interface ens32
virtual_router_id 51
priority 100
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
mysql
}
virtual_ipaddress {
192.168.200.254
}
}
[[email protected] ~]# cat /opt/mysql.sh
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
systemctl stop keepalived
fi
[[email protected] ~]# chmod +x /opt/mysql.sh
[[email protected] ~]# systemctl start keepalived
[[email protected] ~]# ip a | grep ens32
2: ens32: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 192.168.200.111/24 brd 192.168.200.255 scope global ens32
inet 192.168.200.254/32 scope global ens32
[[email protected] ~]# tail -f /var/log/messages
Feb 12 17:23:23 localhost Keepalived_vrrp[70087]: VRRP_Instance(VI_1) Transition to MASTER STATE
Feb 12 17:23:24 localhost Keepalived_vrrp[70087]: VRRP_Instance(VI_1) Entering MASTER STATE
Feb 12 17:23:24 localhost Keepalived_vrrp[70087]: VRRP_Instance(VI_1) setting protocol iptable drop rule
Feb 12 17:23:24 localhost Keepalived_vrrp[70087]: VRRP_Instance(VI_1) setting protocol VIPs.
Feb 12 17:23:24 localhost Keepalived_vrrp[70087]: Sending gratuitous ARP on ens32 for 192.168.200.254
Feb 12 17:23:24 localhost Keepalived_vrrp[70087]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens32 for 192.168.200.254
Feb 12 17:23:24 localhost Keepalived_vrrp[70087]: Sending gratuitous ARP on ens32 for 192.168.200.254