安裝keepalived并將其配置成系統(tǒng)服務(wù)。master1和master2兩臺(tái)機(jī)器上同樣進(jìn)行如下操作:
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做心跳檢測(cè),如果db01的MySQL服務(wù)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做心跳檢測(cè),如果db01的MySQL服務(wù)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