基础架构如下
准备干净的实验环境
[root@mysql1 ~]# systemctl stop firewalld
[root@mysql1 ~]# cat /etc/sysconfig/selinux |grep "SELINUX=disabled"
SELINUX=disabled
[root@mysql1 ~]# setenforce 0
setenforce: SELinux is disabled
[root@mysql1 ~]# getenforce
Disabled
首先,搭建221和222机器mysql互为主从
这里可以参考我之前的帖子mysql主从复制
mysql双主双从
我们本次实验只是两台机器互为主从,即互为master和slave
搭建完成后登录mysql 可以查看状态:
mysql1机器:
mysql2:
可以自行验证修改数据,两边数据库是否同步
随后
开始安装配置 keepalived (mysql1& mysql2)
mysql1:
[root@mysql1 ~]# yum -y install keepalived
[root@mysql1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id mysql1
script_user root
enable_script_security
user root
}
vrrp_script check_run {
script "/etc/keepalived/keepalived_check_mysql.sh"
interval 5
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 88
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass yryr
}
track_script {
check_run
}
virtual_ipaddress {
192.168.29.100/24 dev ens33 label ens33:1
}
}
mysql2:
[root@mysql2 ~]# yum -y install keepalived
[root@mysql2 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id mysql2
script_user root
enable_script_security
}
vrrp_script check_run {
script "/etc/keepalived/keepalived_check_mysql.sh"
interval 5
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 88
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass yryr
}
track_script {
check_run
}
virtual_ipaddress {
192.168.29.100/24 dev ens33 label ens33:1
}
}
两台机器上keepalived都配置了一个mysql的检查脚本 ,现在我们来加上:
[root@mysql2 ~]# vim /etc/keepalived/keepalived_check_mysql.sh
#!/bin/bash
MYSQL=/usr/bin/mysql
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=123456
CHECK_TIME=3
MYSQL_OK=1
check_mysql_helth (){
echo "Checking MySQL at $(date)" >> /var/log/keepalived_mysql_check.log
$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -e "show status" >> /var/log/keepalived_mysql_check.log 2>&1
if [ $? -eq 0 ]; then
MYSQL_OK=1
else
MYSQL_OK=0
fi
return $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]; do
check_mysql_helth
if [ $MYSQL_OK -eq 1 ]; then
exit 0
fi
let CHECK_TIME--
sleep 1
done
echo "MySQL health check failed at $(date)" >> /var/log/keepalived_mysql_check.log
/usr/bin/systemctl stop keepalived
exit 1
授权:
[root@mysql1 ~]# chmod +x /etc/keepalived/keepalived_check_mysql.sh
重启服务:
[root@mysql1 local]# systemctl restart keepalived
[root@mysql2 local]# systemctl restart keepalived
查看:
可以看到我们的vip 100 已经飘到 master机器上
[root@mysql1 local]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:5f:08:ee brd ff:ff:ff:ff:ff:ff
inet 192.168.29.221/24 brd 192.168.29.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.29.100/24 scope global secondary ens33:1
valid_lft forever preferred_lft forever
inet6 fe80::b09a:b99e:bc92:62ee/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::9073:a63f:e689:8462/64 scope link noprefixroute
valid_lft forever preferred_lft forever
后续可以停止mysql 服务来验证vip状态~