MySQL双主互备+Keepalived高可用架构介绍(3)

#分别在两台服务器上放行vrrp协议(CentOS5/6配置方法):
[root@Mysql-248 keepalived]#iptables -I INPUT -p vrrp -d 224.0.0.18/32 -j ACCEPT
#保存放行规则。若是通过开机脚本控制防火墙规则,请注意修改对应脚本。
[root@Mysql-248 keepalived]#/etc/init.d/iptables

2.检查两台服务器的IP,并在客户机通过VIP连接数据库

#在248上查看,发现此时vip在此机器
[root@Mysql-248 keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
      valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:47:a4:ea brd ff:ff:ff:ff:ff:ff
    inet 192.168.175.248/24 brd 192.168.175.255 scope global eth0
    inet 192.168.175.254/24 scope global secondary eth0
    inet6 fe80::20c:29ff:fe47:a4ea/64 scope link
      valid_lft forever preferred_lft forever
       
       
#在249上查看,并无VIP
[root@Mysql-249 keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
      valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:e9:01:e0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.175.249/24 brd 192.168.175.255 scope global eth0
    inet6 fe80::20c:29ff:fee9:1e0/64 scope link
      valid_lft forever preferred_lft forever

MySQL双主互备+Keepalived高可用架构介绍

客户端通过VIP连接数据库

3.在客户机通过VIP连接Mysql,同时测试不间断ping vip254,并停止Mysql248上的Mysql服务器。停止后新建一个数据库测试主节点故障后的写操作是否正常。

[root@Mysql-248 keepalived]# /etc/init.d/mysqld stop
Shutting down MySQL............ SUCCESS!
[root@Mysql-248 keepalived]#

MySQL双主互备+Keepalived高可用架构介绍

不间断ping测试故障切换时间

MySQL双主互备+Keepalived高可用架构介绍

故障切换后测试写入数据

在模拟故障的过程中,VIP切换时网络会出现一下中断,几次测试发现VIP切换的时间在几百毫秒延迟到一个丢包之间,切换时间非常短。但是测试写入数据提示连接中断,重试后发现可以正常写入数据。所以此架构需要程序端有数据库断开时重连的机制。在此情况下,主节点故障对业务的影响时间就降低到了秒级,无需人为干预才能恢复业务。

一台节点故障后,仅剩下一台节点能够正常工作,只需要配合监控发现故障后,及时修复故障节点,就可以将恢复后的节点重新成为keepalived的备用节点,以保证架构的持续高可用。

4.故障切换过程

#在故障节点248上查看日志,发现VRRP_Script检测到Mysql状态faild,节点进入FAULT状态并移除vip
[root@Mysql-248 ~]# tail -f /var/log/messages
Sep  1 08:32:51 Mysql-248 Keepalived_vrrp[71143]: VRRP_Script(check_mysqld) failed
Sep  1 08:32:52 Mysql-248 Keepalived_vrrp[71143]: VRRP_Instance(HA_1) Entering FAULT STATE
Sep  1 08:32:52 Mysql-248 Keepalived_vrrp[71143]: VRRP_Instance(HA_1) removing protocol VIPs.
Sep  1 08:32:52 Mysql-248 Keepalived_vrrp[71143]: VRRP_Instance(HA_1) Now in FAULT state
Sep  1 08:32:52 Mysql-248 Keepalived_healthcheckers[71142]: Netlink reflector reports IP 192.168.175.254 removed
 
#249服务器进入MASTER状态,并绑定VIP
[root@Mysql-249 ~]# tail -f /var/log/messages
Sep  1 08:32:54 Mysql-249 Keepalived_vrrp[54929]: VRRP_Instance(HA_1) Transition to MASTER STATE
Sep  1 08:32:56 Mysql-249 Keepalived_vrrp[54929]: VRRP_Instance(HA_1) Entering MASTER STATE
Sep  1 08:32:56 Mysql-249 Keepalived_vrrp[54929]: VRRP_Instance(HA_1) setting protocol VIPs.
Sep  1 08:32:56 Mysql-249 Keepalived_vrrp[54929]: VRRP_Instance(HA_1) Sending gratuitous ARPs on eth0 for 192.168.175.254
Sep  1 08:32:56 Mysql-249 Keepalived_healthcheckers[54928]: Netlink reflector reports IP 192.168.175.254 added
Sep  1 08:33:01 Mysql-249 Keepalived_vrrp[54929]: VRRP_Instance(HA_1) Sending gratuitous ARPs on eth0 for 192.168.175.254

5.故障节点恢复

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/902301259b314448ff4e8570030fab2c.html