安装keepalived并将其配置成系统服务。master1和master2两台机器上同样进行如下操作:
yum install -y openssl-devel keepalived
在db01配置keepalived
cd /etc/keepalived/
mv keepalived.conf keepalived.conf.bak #重命名
vi keepalived.conf
global_defs {
router_id db01
}
vrrp_script chk_mysql_port {
script "/opt/chk_mysql.sh"
interval 2
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
mcast_src_ip 192.168.40.216
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.40.218
}
track_script {
chk_mysql_port
}
}
编写切换脚本,KeepAlived做心跳检测,如果db01的MySQL服务3306端口不可用,slave接管VIP
vi /opt/chk_mysql.sh
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
systemctl stop keepalived
fi
chmod +x /opt/chk_mysql.sh
systemctl restart keepalived.service
systemctl enable keepalived.service
在db02配置keepalived
cd /etc/keepalived/
mv keepalived.conf keepalived.conf.bak #重命名
vi keepalived.conf
global_defs {
router_id db02
}
vrrp_script chk_mysql_port {
script "/opt/chk_mysql.sh"
interval 2
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
mcast_src_ip 192.168.40.217
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.40.218
}
track_script {
chk_mysql_port
}
}
编写切换脚本,KeepAlived做心跳检测,如果db01的MySQL服务3306端口不可用,slave接管VIP
vi /opt/chk_mysql.sh
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
systemctl stop keepalived
fi
chmod +x /opt/chk_mysql.sh
systemctl restart keepalived.service
systemctl enable keepalived.service
本文摘自 :https://blog.51cto.com/u