keepalived mysql双主架构图_MySQL双主互备+Keepalived高可用架构介绍

一、MySQL双主互备+keepalived高可用架构介绍

MySQL主从复制架构可以在很大程度保证MySQL的高可用,在一主多从的架构中还可以利用读写分离将读操作分配到从库中,减轻主库压力。但是在这种架构中,主库出现故障时需要手动将一台从库提升为主库。在对写操作要求较高的环境中,主库故障在主从架构中会成为单点故障。因此需要主主互备架构,避免主节点故障造成写操作失效。

在双主互备的架构中,每台MySQL都充当主服务器,同时充当对方的从服务器。在任意一台服务器上的写操作都会被复制到另一台服务器上,从而保证了数据的可靠性。

在双主互备的基础上加上keepalived,在其中一台机器上绑定虚拟ip(VIP)。利用vip统一对外服务,可以避免在两个节点同时写数据造成冲突。同时当keepalived主节点发生故障时,keeplived会自动将VIP切换到备节点上,从而实现主服务器的高可用。

MySQL双主互备+keepalived高可用架构图

二、MySQL双主互备架构部署

在上一篇MySQL主从复制操作中已经完成了MySQL主从架构的部署。在此基础上,需要在原Master上开启relay-log,在原Slave服务器上开启log-bin,同时在MySQL248上指定MySQL249为自己的主服务器并开启slave即可。

1.开启原Master上的relay-log,指定不复制的库

[root@Mysql-248 mysql-5.6.30]# grep -A8 'mysqld' my.cnf

[mysqld]

port = 3306

server_id = 1

log-bin=mysql-bin

#以下部分为在原基础上新增的内容

relay-log = mysql-relay-bin

replicate-wild-ignore-table=mysql.%

replicate-wild-ignore-table=test.%

replicate-wild-ignore-table=information_schema.%

2.开启原Slave上的log-bin,指定不复制的库

[root@Mysql-249 mysql-5.6.30]# grep -A8 'mysqld' my.cnf

[mysqld]

port = 3306

server_id = 2

relay-log = mysql-relay-bin

#以下部分为在原基础上新增的内容

log-bin=mysql-bin

replicate-wild-ignore-table=mysql.%

replicate-wild-ignore-table=test.%

replicate-wild-ignore-table=information_schema.%

3.在Mysql248服务器上指定Mysql249为自己的主服务器并开启slave

####  ① 在Mysql249上查看当前master信息,并建立复制用户

mysql> show master status;

+------------------+----------+--------------+------------------+-------------------+

| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000001 |      493 |              |                  |                  |

+------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

mysql> grant replication slave on *.* to 'repl_user'@'192.168.175.%' identified by '123456';

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

#### ② 在Mysql248上指定Mysql249为自己的主服务器,开启slave

mysql> change master to \

-> master_host='192.168.175.249',

-> master_user='repl_user',

-> master_password='123456',

-> master_log_file='mysql-bin.000001',

-> master_log_pos=493;

Query OK, 0 rows affected, 2 warnings (0.02 sec)

mysql> start slave;

Query OK, 0 rows affected (0.03 sec)

4.检验双主互备

①通过分别在两台服务器上使用show slave status\G,查询主库信息以及IO进程、SQL进程工作状态。若两台服务器的查询结果都为Slave_IO_Running: Yes,Slave_SQL_Running: Yes;则表示当前双主互备状态正常。

②在Mysql248数据库上建库建表,检查Mysql249上是否同步正常;然后在Mysql249上建库建表,检查Mysql248上是否同步正常。

三、keepalived高可用部署

以下操作在两台机器上基本相同,不同之处请留意备注标识

1.keepalived编译安装

#编译操作两台机器一样

[root@Mysql-248 src]# tar -zxf keepalived-1.2.12.tar.gz

[root@Mysql-248 src]# cd keepalived-1.2.12

[root@Mysql-248 keepalived-1.2.12]# ./configure  --sysconf=/etc --with-kernel-dir=/usr/src/kernels/2.6.32-573.el6.x86_64/

[root@Mysql-248 keepalived-1.2.12]# make

[root@Mysql-248 keepalived-1.2.12]# make install

[root@Mysql-248 keepalived-1.2.12]# ln -s /usr/local/sbin/keepalived  /sbin/

[root@Mysql-248 keepalived-1.2.12]# chkconfig  --add keepalived

[root@Mysql-248 keepalived-1.2.12]# chkconfig  --level 35 keepalived on

2.keepalived配置

#配置文件中需要注意主备节点的priority与nopreempt配置不同

[root@Mysql-248 ~]# cd /etc/keepalived/

[root@Mysql-248 keepalived]# cat keepalived.conf

! Configuration File for keepalived

global_defs {

notification_email {

[email protected]

[email protected]

[email protected]

}

notification_email_from [email protected]

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状态。

#分别在两台服务器上放行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: 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: 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: 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: 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

客户端通过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]#

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

故障切换后测试写入数据

在模拟故障的过程中,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.故障节点恢复

#将测试时关闭的数据库打开,查看日志发现节点进入BACKUP状态

#查看数据库发现故障时候创建的数据库已经同步过来了

#此时又恢复Mysql双主复制的高可用状态了

[root@Mysql-248 keepalived]# /etc/init.d/mysqld start

Starting MySQL.. SUCCESS!

[root@Mysql-248 keepalived]# tail -5 /var/log/messages

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

Sep  1 08:51:42 Mysql-248 Keepalived_vrrp[71143]: VRRP_Script(check_mysqld) succeeded

Sep  1 08:51:44 Mysql-248 Keepalived_vrrp[71143]: VRRP_Instance(HA_1) Entering BACKUP STATE

[root@Mysql-248 keepalived]#

[root@Mysql-248 mysql-5.6.30]# mysql -uroot -p'qwe123``' -e 'show databases;'

Warning: Using a password on the command line interface can be insecure.

+--------------------+

| Database          |

+--------------------+

| information_schema |

| cubix              |

| db1                |

| mysql              |

| performance_schema |

+--------------------+

你可能感兴趣的:(keepalived,mysql双主架构图)