1、准备

操作系统版本:CentOS6.5 64位
MySQL版本:5.6.21
Keepalived版本:v1.2.23 1.2.24
节点A:192.168.43.134
节点B:192.168.43.135
VIP(虚拟IP):192.168.43.130

下文中说到
VIP即指192.168.43.130
节点A或134即指192.168.43.134
节点B或135即指192.168.43.135

MySQL高可用主要是通过Keepalived这款软件来实现多个节点的服务可用性检测和故障转移,对外提供VIP地址供客户端连接,由keepalived根据服务的状态,负责将vip漂移到真实可用的机器上,从而实现服务的高可用。阅读本文前,需要先安装MySQL、Keepalived以及MySQL主主同步配置,可参考我之前写的文章自行安装与配置:

Linux下安装Lnmp环境之准备篇(一)
Linux下安装Lnmp环境之Mysql(二)
MySQL主主互备配置过程详解
Linux下keepalived安装与配置

本文配置keepalived为非抢占模式

节点A配置

vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
    ### 标识本节点的字条串,通常为 功能+IP
    router_id db_134
}
vrrp_instance VI_1 {
    #两个节点都为BACKUP状态,根据优先级大小判断谁为MASTER
    state BACKUP
    interface eth0
    #虚拟路由的ID号,每个节点设置必须一样
    virtual_router_id 51
    #节点优先级 MASTER高于BACKUP
    priority 100
    advert_int 1
    nopreempt #非抢占模式
    authentication {
        auth_type PASS
        auth_pass 1111
    }
     # 虚拟IP池
    virtual_ipaddress {
        192.168.43.130
    }
}

virtual_server 192.168.43.130 3306{
    #健康检查时间间隔,单位:秒 
    delay_loop 2
    lb_algo wrr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.43.134 3306 {
        weight 3
        notify_down /etc/keepalived/mysql.sh # 当mysql服务down了之后,执行的脚本
        TCP_CHECK {
             connect_timeout 10   # mysql连接超时时长(秒)
             nb_get_retry 3       # mysql服务连接失败,重试次数
             delay_before_retry 3 #每隔3秒检测一次mysql服务是否可用
             connect_port 3306
         }
    }
}

/etc/keepalived/mysql.sh配置:

#!/bin/sh
pkill keepalived

当mysql服务停止时,会执行/etc/keepalived/mysql.sh脚本,将当前节点的keepalived服务停止,这样vip就会切换到另外一个节点上,从而实现了服务的高可用。

节点B配置

vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id db_135
}

vrrp_instance VI_1 {
    #两个节点都为BACKUP状态,根据优先级大小判断谁为MASTER
    state BACKUP
    interface eth0
    virtual_router_id 51
    #注意:比MASTER小
    priority 90
    advert_int 1
    nopreempt #非抢占模式
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    #虚拟IP池
    virtual_ipaddress {
        192.168.43.130
    }
}

virtual_server 192.168.43.130 3306 {
    delay_loop 2
    lb_algo wrr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.43.135 3306 {
        weight 3
        notify_down /etc/keepalived/mysql.sh  # 当mysql服务down了之后,执行
的脚本
TCP_CHECK {
             connect_timeout 10   # mysql连接超时时长(秒)
             nb_get_retry 3       # mysql服务连接失败,重试次数
             delay_before_retry 3 #每隔3秒检测一次mysql服务是否可用
             connect_port 3306
         }
    }
}

/etc/keepalived/mysql.sh配置同节点A

#!/bin/sh
pkill keepalived

启动MySQL和Keepalived服务

service mysqld start
service keepalived start

注意:先启动mysql,再启动keepalived。因为keepalived启动之后会去连接mysql,检测服务是否可用,如果3次都没连接成功,则会将keepalived进程杀死。

连接测试

注意:连接到VIP地址,而非直接连接到真实的MySQL服务器。

先看看vip漂在哪台服务器上:

ip add

mysql vip属性 mysql vip节点_keepalived


从上图得知,此时VIP漂移在节点1(192.168.43.134)上。

注:两个节点 在指定ip连接到mysql时,需要授权配置远程连接的帐号、密码和ip。如:

grant all privileges on *.* to 'root'@'%' identified by 'password' with grant option;

从虚拟IP 192.168.43.130 登录Mysql

mysql -u root -proot -h 192.168.43.130

mysql vip属性 mysql vip节点_myqsl_02

故障转移测试

从上面连接测试得知,此时vip漂移在192.168.43.134节点上,所以通过vip(192.168.43.130)连接到的真实服务器是192.168.43.134。将134的mysql服务停止,观察vip是否会切换到135上。

#通过/var/log/message文件可以看到keepalived故障的日志
tail -f /var/log/message
节点A的日志:
Sep 25 16:00:32 edu-mysql-01 Keepalived_healthcheckers[3517]: TCP connection to [192.168.43.134]:3306 failed.
Sep 25 16:00:35 edu-mysql-01 Keepalived_healthcheckers[3517]: TCP connection to [192.168.43.134]:3306 failed.
Sep 25 16:00:35 edu-mysql-01 Keepalived_healthcheckers[3517]: Check on service [192.168.43.134]:3306 failed after 1 retry.
Sep 25 16:00:35 edu-mysql-01 Keepalived_healthcheckers[3517]: Removing service [192.168.43.134]:3306 from VS [192.168.43.130]:3306
Sep 25 16:00:35 edu-mysql-01 Keepalived_healthcheckers[3517]: Executing [/etc/keepalived/mysql.sh] for service [192.168.43.134]:3306 in VS [192.168.43.130]:3306
Sep 25 16:00:35 edu-mysql-01 Keepalived_healthcheckers[3517]: Lost quorum 1-0=1 > 0 for VS [192.168.43.130]:3306
Sep 25 16:00:35 edu-mysql-01 kernel: IPVS: __ip_vs_del_service: enter
Sep 25 16:00:35 edu-mysql-01 Keepalived[3515]: Stopping
Sep 25 16:00:35 edu-mysql-01 Keepalived_healthcheckers[3517]: Stopped
Sep 25 16:00:35 edu-mysql-01 Keepalived_vrrp[3518]: VRRP_Instance(VI_1) sent 0 priority
Sep 25 16:00:35 edu-mysql-01 Keepalived_vrrp[3518]: VRRP_Instance(VI_1) removing protocol VIPs.
Sep 25 16:00:36 edu-mysql-01 Keepalived_vrrp[3518]: Stopped
Sep 25 16:00:36 edu-mysql-01 Keepalived[3515]: Stopped Keepalived v1.2.23 (09/12,2016)

前三行是keepalived心跳检测,每秒检测一次mysql服务是否可用,当第3次连接失败时,将该服务从lvs真实服务器列表中移除(第4行),并执行/etc/keepalived/mysql.sh脚本(第5行)停止keepalived服务,转让vip使用权。

节点B的日志:
Sep 25 16:00:36 edu-mysql-02 Keepalived_vrrp[3457]: VRRP_Instance(VI_1) Transition to MASTER STATE
Sep 25 16:00:37 edu-mysql-02 Keepalived_vrrp[3457]: VRRP_Instance(VI_1) Entering MASTER STATE
Sep 25 16:00:37 edu-mysql-02 Keepalived_vrrp[3457]: VRRP_Instance(VI_1) setting protocol VIPs.
Sep 25 16:00:37 edu-mysql-02 Keepalived_vrrp[3457]: Sending gratuitous ARP on enp0s3 for 192.168.43.130
Sep 25 16:00:37 edu-mysql-02 Keepalived_vrrp[3457]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on enp0s3 for 192.168.43.130
Sep 25 16:00:37 edu-mysql-02 Keepalived_vrrp[3457]: Sending gratuitous ARP on enp0s3 for 192.168.43.130
Sep 25 16:00:37 edu-mysql-02 Keepalived_vrrp[3457]: Sending gratuitous ARP on enp0s3 for 192.168.43.130
Sep 25 16:00:37 edu-mysql-02 Keepalived_vrrp[3457]: Sending gratuitous ARP on enp0s3 for 192.168.43.130
Sep 25 16:00:37 edu-mysql-02 Keepalived_vrrp[3457]: Sending gratuitous ARP on enp0s3 for 192.168.43.130
Sep 25 16:00:37 edu-mysql-02 Keepalived_healthcheckers[3456]: Netlink reflector reports IP 192.168.43.130 added
Sep 25 16:00:42 edu-mysql-02 Keepalived_vrrp[3457]: Sending gratuitous ARP on enp0s3 for 192.168.43.130
Sep 25 16:00:42 edu-mysql-02 Keepalived_vrrp[3457]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on enp0s3 for 192.168.43.130

节点2当前是BACKUP状态,当收到节点1挂掉的通知后,将自己的状态转换为master状态(第1行),接着将vip(192.168.43.130)绑定到enp0s3网卡上(第3到第5行)。

查看故障转移后的vip

mysql vip属性 mysql vip节点_keepalived_03

从上图可知,vip成功从134转移到了135上。

此时在之前已经和vip建立了连接的mysql客户端当中,再执行sql时,第一次会执行失败,因为之前的连接已经断开,第二次执行时,会尝试重新连接到vip对应的新的mysql真实服务器上。