MySQL高可用架构之Keepalived+主从架构部署(4)

if [ ${io_status} -ne 1 ] || [ ${sql_status} -ne 1 ] || [ ${sync_status} -ne 0 ]
then
    if [ ${vip_status} -eq 0 ] && [ ${keepalived_status} -ne 0 ];then
        service keepalived stop
    fi
elif [ ${io_status} -eq 1 ] && [ ${sql_status} -eq 1 ] && [ ${sync_status} -eq 0 ]
then
    if [ ${sync_time} -ge 120 ];then
        if [ ${vip_status} -eq 0 ] && [ ${keepalived_status} -ne 0 ];then
          service keepalived stop
        fi
    elif [ ${sync_time} -eq 0 ];then
        if [ ${vip_status} -eq 0 ] && [ ${keepalived_status} -eq 0 ];then
          service keepalived start
        fi
    fi
fi

3、防火墙端口开放

VRRP是用IP多播的方式(默认多播地址224.0.0.18)实现高可用对之间通信的,如果开启防火墙记得开放这个vrrp协议对应的策略

iptables -A INPUT -p vrrp -d 224.0.0.18  -j ACCEPT

iptables -A INPUT -p vrrp  -j ACCEPT

六、keepalived故障转移测试

1、主数据库实例宕机

当前主数据库主机状态:

[root@node1 keepalived]# service mysqld status
MySQL (Percona Server) running (22983)                    [  OK  ]
[root@node1 keepalived]# service keepalived status
keepalived (pid  14092) is running...
[root@node1 keepalived]# ip add |grep inet
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
    inet 192.168.1.23/24 brd 192.168.1.255 scope global eth1
    inet 192.168.1.27/32 scope global eth1:1
    inet6 fe80::250:56ff:fe8d:496/64 scope link

当前从数据库主机状态:

[root@node2 keepalived]# service mysqld status
MySQL (Percona Server) running (14146)                    [  OK  ]
[root@node2 keepalived]# service keepalived status
keepalived (pid  12207) is running...
[root@node2 keepalived]# ip add |grep inet
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
    inet 192.168.1.24/24 brd 192.168.1.255 scope global eth1
    inet6 fe80::250:56ff:fe8d:3d1e/64 scope link

主库执行关闭mysql实例命令,数据库关闭后发现VIP已漂移

[root@node1 keepalived]# service mysqld stop
Shutting down MySQL (Percona Server)...............        [  OK  ]
[root@node1 keepalived]# service mysqld status   
MySQL (Percona Server) is not running                      [FAILED]
[root@node1 keepalived]# service keepalived status
keepalived is stopped
[root@node1 keepalived]# ip add |grep inet
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
    inet 192.168.1.23/24 brd 192.168.1.255 scope global eth1
    inet6 fe80::250:56ff:fe8d:496/64 scope link

此时检查从库相关状态

[root@node2 keepalived]# service mysqld status
MySQL (Percona Server) running (14146)                    [  OK  ]
[root@node2 keepalived]# service keepalived status
keepalived (pid  12207) is running...
[root@node2 keepalived]# ip add |grep inet
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
    inet 192.168.1.24/24 brd 192.168.1.255 scope global eth1
    inet 192.168.1.27/32 scope global eth1:1
    inet6 fe80::250:56ff:fe8d:3d1e/64 scope link
[root@node2 keepalived]# mysql -uroot -e "show slave status \G"|egrep  "Slave.*Running|Seconds_Behind_Master|Last_Errno"|grep -v "Slave_SQL_Running_State"
            Slave_IO_Running: Connecting
            Slave_SQL_Running: Yes
                  Last_Errno: 0
        Seconds_Behind_Master: NULL

2、主数据库服务器宕机

同上,实现检查主备状态后,在主库执行shutdown -h now的命令后,检查从库状态

3、主keepalived服务停止

同上

4、主服务器网卡故障

此时keepalived服务正常,mysqld服务正常,VRRP通信无法完成的情况下,主库会执行关闭keepalived服务并完成切换

5、模拟VRRP通信故障(脑裂)

在备库开启防火墙,并不开通vrrp允许,此时VIP会存在于两边的服务器上

[root@node2 keepalived]# service iptables start
iptables: Applying firewall rules:                        [  OK  ]

主库服务器状态检查

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

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