1.在linux机器上边下载安装keepalived
yum install -y keepalived
2.进行keepalived主备配置文件配置
机器IP就VIP如下
真实IP 192.168.10.101,192.168.10.102 VIP 192.168.10.111,192.168.10.112 读写分离
主节点 192.168.10.101
global_defs {
router_id mysql_ha_54
}
##
vrrp_script check_run {
script "/etc/keepalived/check_mysql.sh"
interval 30
}
#主备节点的virtual_router_id 必须相同
vrrp_instance VI_1 {
state BACKUP
interface bond0
virtual_router_id 202
priority 100
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 234202
}
virtual_ipaddress {
192.168.10.111
192.168.10.112
}
track_script {
check_run
}
unicast_src_ip 192.168.10.101
unicast_peer {
192.168.10.102
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
notify_stop "/etc/keepalived/notify.sh stop"
}
virtual_server 192.168.10.111 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.10.101 3306 {
weight 3
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
connect_port 3306
}
}
}
virtual_server 192.168.10.112 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.10.101 3306 {
weight 3
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
connect_port 3311
}
}
}
备节点 192.168.10.102
global_defs {
router_id mysql_ha_55
}
vrrp_script check_run {
script "/etc/keepalived/check_mysql.sh"
interval 30
}
vrrp_instance VI_1 {
state BACKUP
interface bond0
virtual_router_id 202
priority 90
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 234202
}
virtual_ipaddress {
192.168.10.111
192.168.10.112
}
track_script {
check_run
}
unicast_src_ip 192.168.10.102
unicast_peer {
192.168.10.101
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
notify_stop "/etc/keepalived/notify.sh stop"
}
virtual_server 192.168.10.111 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.10.102 3306 {
weight 3
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
connect_port 3306
}
}
}
virtual_server 192.168.10.112 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.10.102 3306 {
weight 3
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
connect_port 3311
}
}
}
检查keepalived所监控mysql服务的脚本/etc/keepalived/check_mysql.sh
#!/bin/bash
count=1
while true
do
mysql -uroot -S /data/mysqldata3306/sock/mysql.sock -p"**********" -e "show status;" > /dev/null 2>&1
i=$?
ps aux | grep mysqld | grep "group-suffix=@3311" | grep -v grep > /dev/null 2>&1
j=$?
if [ $i = 0 ] && [ $j = 0 ]
then
exit 0
else
if [ $i = 1 ] && [ $j = 0 ]
then
exit 0
else
if [ $count -gt 5 ]
then
break
fi
let count++
continue
fi
fi
done
#/etc/init.d/keepalived stop
systemctl stop keepalived
连接异常主从节点切换监控脚本notify.sh
#!/bin/bash
vip=192.168.10.111
notify() {
warningbody="vip${vip}ChangeTo$1"
curl "http://10.27.11.76:8082/mcoms/clogin.do?method=sendMsgDailyAsForm\&receivers=151*****097\&msgContent=$warningbody"
}
case "$1" in
master)
notify master
exit 0
;;
backup)
notify backup
exit 0
;;
fault)
notify fault
exit 0
;;
*)
echo 'Usage: `basename $0` {master|backup|fault}'
exit 1
;;
esac