centos7 keepalived+mysql互为主从高可用集群

1. mysql互为主从的架构搭建

1.1 实验环境

1.1.1 两台服务器系统均为centos7

1.1.2 服务器ip分配

MySQL Server1 : 192.168.153.135
MySQL Server2 : 192.168.153.136

1.1.3 实验之前服务器均关闭防火墙和selinux

关闭防火墙命令:

systemctl stop firewalld

关闭selinux命令:

setenforce 0

1.1.4 具体配置:centos7的和centos6相同

centos7安装数据库客户端名称为:mariadb,服务端名称为:mariadb-server

 yum -y install mariadb-server mariadb

参考链接>> https://blog.csdn.net/m0_46674735/article/details/109597636

2. keepalived抢占模式配置

2.1 在两台服务器上均安装keepalived软件

yum -y install keepalived

2.2 修改Server1上keepalived的配置文件

2.2.1 修改命令

vim /etc/keepalived/keepalived.conf 

2.2.2 修改内容如下:

1)将14行的vrrp_strict注释掉,注释符为!

vrrp_strict参数的含义为:
严格遵守VRRP协议。下列情况将会阻止启动Keepalived:1. 没有VIP地址。2. 单播邻居。3. 在VRRP版本2中有IPv6地址。这里需要注释掉
centos7 keepalived+mysql互为主从高可用集群

2)配置 vrrp_instance

centos7 keepalived+mysql互为主从高可用集群

3)配置 virtual_server

centos7 keepalived+mysql互为主从高可用集群

2.2.3 修改完成之后保存退出并开启keepalived服务

systemctl start keepalived

2.3 修改Server1上keepalived的配置文件

2.3.1 修改命令

vim /etc/keepalived/keepalived.conf 

2.3.2 修改内容如下:

1)将14行的vrrp_strict注释掉,注释符为!

vrrp_strict参数的含义为:
严格遵守VRRP协议。下列情况将会阻止启动Keepalived:1. 没有VIP地址。2. 单播邻居。3. 在VRRP版本2中有IPv6地址。这里需要注释掉
centos7 keepalived+mysql互为主从高可用集群

2)配置 vrrp_instance

centos7 keepalived+mysql互为主从高可用集群

3)配置 virtual_server

centos7 keepalived+mysql互为主从高可用集群

2.3.3 修改完成之后保存退出并开启keepalived服务

systemctl start keepalived

3. 验证配置

3.1 在server1上VIP是否配置成功

命令:ip add
centos7 keepalived+mysql互为主从高可用集群

3.2 验证VIP漂移

3.2.1 关闭server1上的mysql服务,

[root@host-135 ~]# systemctl stop mariadb

3.2.2 在server2上查看VIP漂移成功

centos7 keepalived+mysql互为主从高可用集群

3.3 抢占模式验证

3.3.1 恢复server1上的mysql服务和keepalived服务

[root@host-135 ~]# systemctl start mariadb
[root@host-135 ~]# systemctl start keepalived

3.3.2 查看VIP抢占成功

centos7 keepalived+mysql互为主从高可用集群

3.4 详细信息可追踪日志查看

命令:tailf /var/log/messages

3.4.1 server1进入master状态

centos7 keepalived+mysql互为主从高可用集群

3.4.2 server2进入backup状态

centos7 keepalived+mysql互为主从高可用集群

3.4.3 server1上的MySQL服务停掉之后,server2抢占为master

centos7 keepalived+mysql互为主从高可用集群

3.4.3 server1上的MySQL服务和keepalived服务恢复之后,server1重新抢占为master

centos7 keepalived+mysql互为主从高可用集群
而server2则恢复到backup状态
centos7 keepalived+mysql互为主从高可用集群

4. keepalived非抢占模式配置

4.1 修改Server1上keepalived的配置文件

4.2.1 修改命令

vim /etc/keepalived/keepalived.conf 

4.2.2 修改内容如下:

将server1的状态也修改为BACKUP,再添加一行参数nopreempt
centos7 keepalived+mysql互为主从高可用集群
修改完成之后保存退出

4.2.3 重启keepalived服务

systemctl restart keepalived

4.2 修改Server2上keepalived的配置文件

4.2.1 修改命令

vim /etc/keepalived/keepalived.conf 

4.2.2 修改内容如下:

只需再添加一行参数nopreempt
centos7 keepalived+mysql互为主从高可用集群
修改完成之后保存退出

4.2.3 重启keepalived服务

systemctl restart keepalived

4.3 验证

4.3.1 验证135主机上VIP设置成功

centos7 keepalived+mysql互为主从高可用集群

4.3.2 验证VIP漂移

停掉135主机上的mysql服务

[root@host-135 ~]# systemctl stop mariadb

在136主机上查看漂移成功

centos7 keepalived+mysql互为主从高可用集群

4.3.3 非抢占模式验证

1)恢复135主机上的mysql服务和keepalived服务

[root@host-135 ~]# systemctl start mariadb keepalived

2)在135主机上查看VIP不会漂移回来

centos7 keepalived+mysql互为主从高可用集群

3)停掉136主机上的mysql服务VIP发生漂移

[root@host-136 ~]# systemctl stop mariadb

4)在135主机上查看VIP又会漂移回来

centos7 keepalived+mysql互为主从高可用集群

上一篇:journactl日志查看命令-渐入佳境


下一篇:CentOS7安装redis稳定版