共计 10960 个字符,预计需要花费 28 分钟才能阅读完成。
一、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 {
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 状态。
# 分别在两台服务器上放行 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
客户端通过 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+Haproxy 负载均衡 http://www.linuxidc.com/Linux/2016-12/138917.htm
Keepalived 安装与配置 http://www.linuxidc.com/Linux/2017-02/140421.htm
《Keepalived 权威指南》下载见 http://www.linuxidc.com/Linux/2012-05/60951.htm
Nginx+Keepalived 实现站点高可用 http://www.linuxidc.com/Linux/2016-12/137883.htm
Nginx+Keepalived 实现站点高可用 (负载均衡) http://www.linuxidc.com/Linux/2016-12/138221.htm
构建高可用集群 Keepalived+Haproxy 负载均衡 http://www.linuxidc.com/Linux/2016-12/138917.htm
CentOS6.5 下 Keepalived 高可用服务单实例配置 http://www.linuxidc.com/Linux/2016-12/138110.htm
Keepalived+Nginx 实现高可用(HA)http://www.linuxidc.com/Linux/2017-02/140422.htm
本文永久更新链接地址 :http://www.linuxidc.com/Linux/2017-02/141173.htm