实验环境:
本次实验基于上一个实验的环境
上一个实验为 “CentOS-7.5 配置 MySQL-5.7 双主复制”
上一个实验链接:https://blog.51cto.com/14783377/2485673

1、安装并配置keepalived

master-a端:

1)安装keepalived
[root@CentOS ~]#
[root@CentOS ~]# yum install keepalived 安装keepalived
[root@CentOS ~]#
[root@CentOS ~]# rpm -qa keepalived
keepalived-1.3.5-16.el7.x86_64
[root@CentOS ~]#

2)修改 keepalived配置文件
[root@CentOS ~]#
[root@CentOS ~]# > /etc/keepalived/keepalived.conf #清空原来的配置文件
[root@CentOS ~]#
[root@CentOS ~]# vim /etc/keepalived/keepalived.conf #自定义配置文件
! Configuration File for keepalived

global_defs {
notification_email {br/>[email protected]
}
smtp_server 192.168.23.1
smtp_connect_timeout 30
router_id MySQL-HA
}

vrrp_script chk_mysql {
script "/etc/keepalived/chk_mysql.sh"
interval 2
weight -5
fall 3
}

vrrp_instance VI_1 {
state BACKUP
interface ens32
mcast_src_ip 192.168.18.103
virtual_router_id 55
priority 100
advert_int 1
nopreempt

authentication {
auth_type PASS
auth_pass 123456
}

virtual_ipaddress {
192.168.18.110/24
}

track_script {
chk_mysql
}
}

[root@CentOS ~]#
[root@CentOS ~]# cat /etc/keepalived/keepalived.conf #查看配置
mysql双主+keepalived实现高可用集群_第1张图片

3)编写mysql状态检测脚本
[root@CentOS ~]#
[root@CentOS ~]# vim /etc/keepalived/chk_mysql.sh

#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
systemctl stop keepalived
fi

[root@CentOS ~]#
[root@CentOS ~]# cat /etc/keepalived/chk_mysql.sh #查看脚本内容
mysql双主+keepalived实现高可用集群_第2张图片
[root@CentOS ~]#
[root@CentOS ~]# chmod +x /etc/keepalived/chk_mysql.sh #赋予脚本可执行权限

4)启动keepalived
[root@CentOS ~]#
[root@CentOS ~]# systemctl start keepalived
[root@CentOS ~]# systemctl enable keepalived

master-b端:
1)安装keepalived
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# yum install keepalived

2)修改keepalived配置文件
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# > /etc/keepalived/keepalived.conf #清空原配置
[root@CentOS-2 ~]# vim /etc/keepalived/keepalived.conf #自定义配置文件

! Configuration File for keepalived

global_defs {
notification_email {br/>[email protected]
}
smtp_server 192.168.23.1
smtp_connect_timeout 30
router_id MySQL-HA
}

vrrp_script chk_mysql {
script "/etc/keepalived/chk_mysql.sh"
interval 2
weight -5
fall 3
}

vrrp_instance VI_1 {
state BACKUP
interface ens32
mcast_src_ip 192.168.18.104
virtual_router_id 55
priority 95
advert_int 1

authentication {
auth_type PASS
auth_pass 123456
}

virtual_ipaddress {
192.168.18.110/24
}

track_script {
chk_mysql
}
}

[root@CentOS-2 ~]#
[root@CentOS-2 ~]# cat /etc/keepalived/keepalived.conf
mysql双主+keepalived实现高可用集群_第3张图片

3)编写mysql状态检测脚本
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# vim /etc/keepalived/chk_mysql.sh

#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
systemctl stop keepalived
fi

[root@CentOS-2 ~]#
[root@CentOS-2 ~]# cat /etc/keepalived/chk_mysql.sh
mysql双主+keepalived实现高可用集群_第4张图片
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# chmod +x /etc/keepalived/chk_mysql.sh #赋予脚本可执行权限

4)启动keepalived
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# systemctl start keepalived
[root@CentOS-2 ~]# systemctl enable keepalived

2、测试keepalived+MySQL双主可用性

1)查看mysql状态

master-a端:
[root@CentOS ~]#
[root@CentOS ~]# systemctl status mysqld #查看mysql是否在运行
mysql双主+keepalived实现高可用集群_第5张图片

master-b端:
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# systemctl status mysqld
mysql双主+keepalived实现高可用集群_第6张图片

2)查看VIP在哪个服务器上

master-a端:
[root@CentOS ~]#
[root@CentOS ~]# ip a #发现VIP此时位于master-a端
mysql双主+keepalived实现高可用集群_第7张图片

master-b端:
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# ip a
mysql双主+keepalived实现高可用集群_第8张图片

3)停掉master-a端的mysql服务,查看VIP在哪台服务器上

master-a端:
[root@CentOS ~]#
[root@CentOS ~]# systemctl stop mysqld
[root@CentOS ~]# ip a
mysql双主+keepalived实现高可用集群_第9张图片

master-b端:
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# ip a #去master-b端查看
mysql双主+keepalived实现高可用集群_第10张图片

4)启动master-a端的mysql服务以及keepalived服务,停止master-b端的mysql服务,此时在查看VIP在哪台服务器上
[root@CentOS ~]#
[root@CentOS ~]# systemctl start mysqld #启动master-a端的mysql
[root@CentOS ~]# systemctl start keepalived #启动master-a端的keepalived
[root@CentOS ~]#

[root@CentOS-2 ~]#
[root@CentOS-2 ~]# systemctl stop mysqld #停止master-b端的mysql
[root@CentOS-2 ~]#

[root@CentOS ~]#
[root@CentOS ~]# ip a #查看master-a端的网卡情况
mysql双主+keepalived实现高可用集群_第11张图片

[root@CentOS-2 ~]#
[root@CentOS-2 ~]# ip a #查看master-b端的网卡情况
mysql双主+keepalived实现高可用集群_第12张图片

3、实验结论

通过验证对比发现,MySQL双主集群通过keepalived成功实现了高可用