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

#配置文件中需要注意主备节点的priority与nopreempt配置不同
 
[root@Mysql-248 ~]# cd /etc/keepalived/
[root@Mysql-248 keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
  notification_email {
    acassen@firewall.loc
    failover@firewall.loc
    sysadmin@firewall.loc
  }
  notification_email_from Alexandre.Cassen@firewall.loc
  smtp_server 192.168.200.1
  smtp_connect_timeout 30
  router_id MySQLHA_DEVEL
}
vrrp_script check_mysqld {
    script "/etc/keepalived/mysqlcheck/check_slave.sh" #定义检测mysql复制状态的脚本
    interval 2
    }
vrrp_instance HA_1 {
    state BACKUP                #两台mysql服务器均配置为BACKUP
    interface eth0
    virtual_router_id 80
    priority 100  #备用节点设置为90
    advert_int 2
    nopreempt  #不抢占模式,只在优先级高的机器上设置即可,备用节点不加此参数
    authentication {
        auth_type PASS
        auth_pass qweasdzxc
    }
   
    track_script {
        check_mysqld #调用mysql检测脚本
    }
    virtual_ipaddress {
        192.168.175.254/24 dev eth0    #mysql的对外服务IP,即VIP
    }
}
[root@Mysql-248 keepalived]#

3.配置Mysql检测脚本

#检测脚本主备节点都一样
 
[root@Mysql-248 keepalived]# cat /etc/keepalived/mysqlcheck/check_slave.sh
#!/bin/bash
#This scripts is check for Mysql Slave status
 
Mysqlbin=/usr/local/mysql-5.6.30/bin/mysql
user=root
pw='qwe123``'
port=3306
host=127.0.0.1
sbm=120
 
#Check for $Mysqlbin
if [ ! -f $Mysqlbin ];then
        echo 'Mysqlbin not found,check the variable Mysqlbin'
        exit 99
fi
 
#Get Mysql Slave Status
IOThread=`$Mysqlbin -h $host -P $port -u$user -p$pw -e 'show slave status\G'  2>/dev/null|grep 'Slave_IO_Running:'|awk '{print $NF}'`
SQLThread=`$Mysqlbin -h $host -P $port -u$user -p$pw -e 'show slave status\G' 2>/dev/null|grep 'Slave_SQL_Running:'|awk '{print $NF}'`
SBM=`$Mysqlbin -h $host -P $port -u$user -p$pw -e 'show slave status\G' 2>/dev/null|grep 'Seconds_Behind_Master:'|awk '{print $NF}'`
 
#Check if the mysql run
if [[ -z "$IOThread" ]];then
        exit 1
fi
 
#Check if the thread run
if [[ "$IOThread" = "No" || "$SQLThread" = "No" ]];then
        exit 1
        elif [[ $SBM -ge $sbm ]];then
                exit 1
        else
                exit 0
fi
[root@Mysql-248 keepalived]#

4.启动keepalived

#分别在Mysql248、Mysql249上启动keepalived
#需要注意由于设置了nopreempt非强占模式,所以先启动的节点会成为主节点,后启动的成为备节点。
 
[root@Mysql-248 keepalived]# /etc/init.d/keepalived  start
Starting keepalived: [  OK  ]
[root@Mysql-249 keepalived]# /etc/init.d/keepalived start
Starting keepalived: [  OK  ]

四、检验Mysql双主互备+keepalived高可用架构

1.启动后检查message日志信息

#在248上查看message日志,发现启动时节点进入MASTER状态,并且绑定了192.168.175.254的VIP
[root@Mysql-248 ~]# tail -f /var/log/messages
Sep  1 08:16:41 Mysql-248 Keepalived_vrrp[71143]: VRRP_Instance(HA_1) Transition to MASTER STATE
Sep  1 08:16:43 Mysql-248 Keepalived_vrrp[71143]: VRRP_Instance(HA_1) Entering MASTER STATE
Sep  1 08:16:43 Mysql-248 Keepalived_vrrp[71143]: VRRP_Instance(HA_1) setting protocol VIPs.
Sep  1 08:16:43 Mysql-248 Keepalived_vrrp[71143]: VRRP_Instance(HA_1) Sending gratuitous ARPs on eth0 for 192.168.175.254
Sep  1 08:16:43 Mysql-248 Keepalived_healthcheckers[71142]: Netlink reflector reports IP 192.168.175.254 added
Sep  1 08:16:48 Mysql-248 Keepalived_vrrp[71143]: VRRP_Instance(HA_1) Sending gratuitous ARPs on eth0 for 192.168.175.254
 
#在249上查看message日志,发现启动时节点进入BACKUP状态
[root@Mysql-249 ~]# tail -f /var/log/messages
Sep  1 08:19:15 Mysql-249 Keepalived_vrrp[54929]: Truncating auth_pass to 8 characters
Sep  1 08:19:15 Mysql-249 Keepalived_vrrp[54929]: Configuration is using : 65520 Bytes
Sep  1 08:19:15 Mysql-249 Keepalived_healthcheckers[54928]: Configuration is using : 7520 Bytes
Sep  1 08:19:15 Mysql-249 Keepalived_vrrp[54929]: Using LinkWatch kernel netlink reflector...
Sep  1 08:19:15 Mysql-249 Keepalived_vrrp[54929]: VRRP_Instance(HA_1) Entering BACKUP STATE
Sep  1 08:19:15 Mysql-249 Keepalived_healthcheckers[54928]: Using LinkWatch kernel netlink reflector...
Sep  1 08:19:15 Mysql-249 Keepalived_vrrp[54929]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]

若两台服务器启动后都进入MASTER状态,都绑定了VIP,则需要检查防火墙是否放行了VRRP。

防火墙放行VRRP后,会有一台机器进入BACKUP状态。

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

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