简单介绍
先简单说下MySQL主从复制与keepalived模式和MySQL共享存储与Keepalived模式
MySQL共享存储主备模式不同于MySQL主主复制模式,MySQL主主是利用MySQL自带的replication复制技术实现两台或多台MySQL服务器之间互为拉取二进制日志,处于BACKUP的服务器从MASTER获取二进制日志,并将日志解析成相应的SQL语句,然后在从服务器上重新执行一遍主服务器的操作,从而保证主从数据的一致性。MySQL的复制是实时的异步复制过程,能够保持多台数据库之间数据的一致性,利用MySQL复制可以实现多种方式的主从复制备份,如一主一从、一主多从、双主、双主多从,其优点是显而易见的,因此现实环境中不少的企业都使用MySQL复制技术与keepalived、heartbeat、MySQL Proxy、实现MySQL的高可用集群架构。
当然,MySQL复制技术并非百分百能够保证数据的完成一致性,同时由于开启了二进制日志功能,多少会增加一点I/O,因此在性能上会有一定损耗。简单的主主复制与keepalived模式,在正常情况下两台机器同时提供服务,并且只允许其中一台对外提供写入功能,另外一台提供读功能或只作为备机,当主down之后立即替换为主保证来服务的稳定不中断运行。(当然如果配置auto_increment_offset和auto_incremen_increment参数,可以解决主主同步写入问题)当主故障发生后,keepalived检测到之后将服务切换到从服务器上,在这个过程当中可以看到,keepalived的切换时间是非常迅速的,但此时必须及时修复故障的服务器并继续加入到主从列表中才能保证服务的相对稳定性。一旦有数据异常造成主从不同步都会影响故障的修复效率。
结合MySQL系统架构的灵活性,那么我在这里简单写一篇关于《MySQL共享存储主备模式利用Keepalived实现双机高可用》的教程,以供大家参考。
本篇的MySQL共享存储与keepalived的实现方式类似于DRBD+heartbeat模式。DRBD是一个基于软件实现的服务器存储镜像块设备复制的方案,用于实现实时的、透明的数据同步。可以提供大多数HA系统双机热备的方案。由于DRBD是基于软件的,那么其磁盘的性能必然不如物理磁盘。而共享存储在实际环境中是使用比较多的存储方案,一般是由磁盘阵列提供。如SAN、NAS存储。因此由共享存储提供的HA服务有很多,比如,WEB服务器使磁阵替代NFS存储;MySQL HA的共享存储;Oracle RAC的ASM的共享存储。MySQL HA使用共享存储与keepalived的模式并不能两台MySQL都同时提供服务,正常情况下,只有一台提供服务,另外一台完成处于备用的角色,结合keepalived灵活的脚本检测功能,当主服务器down机时,keepalived可以完成做到自动切换,保障服务的稳定运行。那么这种情况下更适用于对业务要求高稳定性的场景中。
Keepalived的脚本检测功能相当灵活,并且也相对简单。Keepalived的主备角色选举策略由state的状态决定,例如主节点配置为MASTER,备节点配置为BACKUP时,当启动keepalived集群时,处于MASTER状态的节点接管VIP和MySQL服务,而BACKUP状态的节点就处于备用的节点,只有集群发生异常时,keepalived才会根据priority值重新选举MASTER和BACKUP角色,priority值大的节点成为MASTER。在这个角色切换的过程中,如果定义了检测脚本,并且设置了脚本的weight值,当集群异常时,keepalived选举MASTER角色由priority值和weight值之和决定,因此,此时priority+weight就是新的priority值,priority值大的节点被选举为MASTER节点,并且接管集群当中的MySQL服务。
图为MySQL HA与Keepalived实现高可用的模型
那么在这个HA环境中,要注意的一个问题是,正常情况下只有MASTER角色的节点提供MySQL服务,并且也只有MASTER角色的节点挂载共享存储。在keepalived启动时,keepalived会检测各个节点的MySQL服务,只有检测到是正常状态,才被加入到keepalived集群当中(选举为MASTER和BACKUP),如果检测到异常,如MySQL并未启动,此时keepalived并不会把这个节点选举为BACKUP节点,而是被标识为FAULT状态,而当为FAULT状态时,如果MASTER节点down机时,keepalived并不会进行切换,那么此时糟糕的事情是,主备就都不能用了。为了解决这一个问题,应该在检测脚本中自定义为自己想要的状态。如在脚本中定义检测MySQL时,当这台服务器上没有挂载存储,没有VIP,也没有运行MySQL,同时满足这三个条件时,这个节点应该也被视为正常的节点,即BACKUP节点。当MASTER节点down时,才会将服务转移至BACKUP节点当中。定义了灵活的脚本之后,keepalived才能为HA服务提供高可用性。
实验环境
主机 | IP | 系统 | 角色 |
Server1 | 192.168.6.174 | CentOS6.8 | MASTER |
Server2 | 192.168.6.175 | CentOS6.8 | BACKUP |
VIP | 192.168.6.176 | ||
共享存储 | /dev/sdb1 |
实验步骤
一、MySQL安装配置
Server1和Server2安装MySQL,安装版本保持一致,包括MySQL用户UID、安装目录、数据文件目录都要完成相同。
[root@Server1 ~]# id mysql uid=498(mysql) gid=500(mysql) groups=500(mysql) [root@Server1 ~]# ls /usr/local/mysql/ bin COPYING data docs include lib man mysql-test README scripts share sql-bench support-files [root@Server2 ~]# id mysql uid=498(mysql) gid=500(mysql) groups=500(mysql) [root@Server2 ~]# ls /usr/local/mysql/ bin COPYING data docs include lib man mysql-test README scripts share sql-bench support-files
只需在主节点执行安装系统数据库的脚本$basedir/scripts/mysql_install_db
[root@Server1 ~]# mount /dev/sdb1 /data/ [root@Server1 ~]# cd /usr/local/mysql/scripts/ [root@Server1 scripts]# ./mysql_install_db --user=mysql --data=/data/mysql/mysql --basedir=/usr/local/mysql --no-defaults
二、Keepalived安装和配置
1、安装keepalived (Server1和Server2节点)
[root@Server1 ~]# yum install pcre-devel openssl-devel popt-devel libnl-devel libnfnetlink libnfnetlink-devel [root@Server1 keepalived-1.3.2]# ./configure --prefix=/usr/local/keepalived --sysconf=/etc --with-kernel-dir=/usr/src/kernels/2.6.32-642.el6.x86_64 [root@Server1 keepalived-1.3.2]# make && make install [root@Server1 keepalived-1.3.2]# ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/
2、配置keepalived.conf
Server1节点(MASTER)
! Configuration File for keepalived global_defs { notification_email { mail@huangming.org } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id MySQL_HA_DEVEL vrrp_skip_check_adv_addr } vrrp_sync_group MySQL_HA_G1 { group { MySQL_HA_1 } } vrrp_script chk_mysql { script "/etc/keepalived/scripts/check_mysql.sh" interval 6 fall 2 rise 1 weight 20 } vrrp_script chage_status { script "/etc/keepalived/scripts/chage_status.sh" interval 20 } vrrp_instance MySQL_HA_1 { state BACKUP interface eth1 virtual_router_id 176 priority 100 advert_int 2 nopreempt authentication { auth_type PASS auth_pass v262aR1dqH5jTykUbwTo } virtual_ipaddress { 192.168.6.176/24 dev eth1 } track_script { chk_mysql chage_status } track_interface { eth1 } notify_master "/etc/keepalived/scripts/notify.sh master" notify_backup "/etc/keepalived/scripts/notify.sh backup" notify_fault "/etc/keepalived/scripts/notify.sh fault" notify_stop "/etc/keepalived/scripts/notify.sh stop" }
Server2节点(BACKUP)
! Configuration File for keepalived global_defs { notification_email { mail@huangming.org } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id MySQL_HA_DEVEL vrrp_skip_check_adv_addr } vrrp_sync_group MySQL_HA_G1 { group { MySQL_HA_1 } } vrrp_script chk_mysql { script "/etc/keepalived/scripts/check_mysql.sh" interval 6 fall 2 rise 1 weight 20 } vrrp_script chage_status { script "/etc/keepalived/scripts/chage_status.sh" interval 20 } vrrp_instance MySQL_HA_1 { state BACKUP interface eth1 virtual_router_id 176 priority 90 advert_int 2 authentication { auth_type PASS auth_pass v262aR1dqH5jTykUbwTo } virtual_ipaddress { 192.168.6.176/24 dev eth1 } track_script { chk_mysql chage_status } track_interface { eth1 } notify_master "/etc/keepalived/scripts/notify.sh master" notify_backup "/etc/keepalived/scripts/notify.sh backup" notify_fault "/etc/keepalived/scripts/notify.sh fault" notify_stop "/etc/keepalived/scripts/notify.sh stop" }
3、Keepalived notify脚本
notify脚本是keepalived集群管理脚本,当keepalived角色state状态发生变化时都会执行这个脚本
notify_master:当节点进入MASTER时执行该脚本 notify_backup:当节点进入BACKUP时执行该脚本 notify_fault: 当节点进入FAULT时执行该脚本 notify_stop: 当节点进入STOP时执行该脚本
Server1和Server2的notify脚本定义如下:
#!/bin/bash # Author: hm Email: mail@huangming.org LOGDIR=/usr/local/keepalived/logs LOGFILE="/usr/local/keepalived/logs/notify_scripts.log" IPADDR=`ifconfig eth1 | awk -F"[: ]+" '/inet addr/{print $4}'` VIP="192.168.6.176" [ -d $LOGDIR ] || mkdir $LOGDIR #MASTER master() { if [ `df -h |grep "/dev/sdb1"|wc -l` -eq 0 ];then echo "`date '+%b %d %T %a'` $HOSTNAME [keepalived_notify_script]: mount /dev/sdb1 /data ..." mount /dev/sdb1 /data && sleep 1 else echo "`date '+%b %d %T %a'` $HOSTNAME [keepalived_notify_script]: /data is mounted on /data" fi killall -0 mysqld &>/dev/null if [ $? -ne 0 ];then echo "`date '+%b %d %T %a'` $HOSTNAME [keepalived_notify_script]: /etc/init.d/mysqld start..." /etc/init.d/mysqld start &>/dev/null else echo "`date '+%b %d %T %a'` $HOSTNAME [keepalived_notify_script]: Mysqld is Running..." fi exit 0 } #BACKUP backup() { killall -0 mysqld &>/dev/null if [ $? -eq 0 ];then echo "`date '+%b %d %T %a'` $HOSTNAME [keepalived_notify_script]: /etc/init.d/mysqld stop..." /etc/init.d/mysqld stop &>/dev/null else echo "`date '+%b %d %T %a'` $HOSTNAME [keepalived_notify_script]: Mysql Server is Not Running..." fi if [ `df -h |grep "/dev/sdb1"|wc -l` -ne 0 ];then echo "`date '+%b %d %T %a'` $HOSTNAME [keepalived_notify_script]: umount /data..." umount /data && sleep 1 else echo "`date '+%b %d %T %a'` $HOSTNAME [keepalived_notify_script]: /data is not mount" fi } notify_master() { echo "`date '+%b %d %T %a'` $HOSTNAME [keepalived_notify_script]: Transition to $1 STATE"; echo "`date '+%b %d %T %a'` $HOSTNAME [keepalived_notify_script]: Setup the VIP on eth1 for $VIP"; } notify_backup() { echo "`date '+%b %d %T %a'` $HOSTNAME [keepalived_notify_script]: Transition to $1 STATE"; echo "`date '+%b %d %T %a'` $HOSTNAME [keepalived_notify_script]: removing the VIP on eth1 for $VIP"; } case $1 in master) notify_master MASTER >>$LOGFILE master >>$LOGFILE exit 0 ;; backup) notify_backup BACKUP >>$LOGFILE backup >>$LOGFILE exit 0 ;; fault) notify_backup FAULT >>$LOGFILE backup >>$LOGFILE exit 0 ;; stop) notify_backup STOP >>$LOGFILE backup >>$LOGFILE #/etc/init.d/keepalived restart &>/dev/null #这一项仅在Server2节点上配置 exit 0 ;; *) echo "Usage: `basename $0` {master|backup|fault|stop}" exit 1 ;; esac
4、keepalived资源检测脚本
MySQL服务检测脚本check_mysql.sh
#!/bin/bash # 检测共享磁盘是否正常挂载,如果没有挂载,则脚本返回状态码1 if [ `df -h |grep "/dev/sdb1"|wc -l` -eq 0 ];then exit 1 #检测MySQL服务是否正常运行,如果不正常,则返回状态码1 elif ! killall -0 mysqld &>/dev/null;then exit 1 else exit 0 Fi # 如果共享磁盘和MySQL服务都正常,那么就返回0,进入keepalived集群的MASTER或BAKUP角色
MySQL更改状态脚本chage_status.sh(MASTER)
#!/bin/bash # in MASTER #改变MySQL服务的检测脚本,keepalived刚启动时,Server1节点接管MySQL服务,成为MASTER,该节 #点的检测mysql服务脚本为正常检测,即当共享磁盘和MySQL服务均正常,则正常返回状态码0; #而Server2节点,在启动keepalived时,检测脚本则改为当共享磁盘和MySQL服务均没有运行时, #同样返回状态码0,因此进入BACKUP角色。改变检测脚本的前提条件为是否存在VIP,当Server2节点 #同时满足这三个条件才会改变脚本的检测状态 CHK_VIP=`ip addr| awk -F"[ :]+" '/192.168.6.176/{print $3}'` if [ "$CHK_VIP" == "" ];then cat > /etc/keepalived/scripts/check_mysql.sh << EOF #!/bin/bash if [ \`df -h |grep "/dev/sdb1"|wc -l\` -eq 0 ];then exit 0 elif ! killall -0 mysqld &>/dev/null;then exit 0 else exit 0 fi EOF else cat > /etc/keepalived/scripts/check_mysql.sh << EOF #!/bin/bash if [ \`df -h |grep "/dev/sdb1"|wc -l\` -eq 0 ];then exit 1 elif ! killall -0 mysqld &>/dev/null;then exit 1 else exit 0 fi EOF Fi
MySQL更改状态脚本chage_status.sh(BACKUP)
#!/bin/bash # in BACKUP CHK_VIP=`ip addr| awk -F"[ :]+" '/192.168.6.176/{print $3}'` if [ "$CHK_VIP" == "" ];then cat > /etc/keepalived/scripts/check_mysql.sh << EOF #!/bin/bash if [ \`df -h |grep "/dev/sdb1"|wc -l\` -eq 0 ];then exit 0 elif ! killall -0 mysqld &>/dev/null;then exit 0 else exit 0 fi EOF else cat > /etc/keepalived/scripts/check_mysql.sh << EOF #!/bin/bash if [ \`df -h |grep "/dev/sdb1"|wc -l\` -eq 0 ];then exit 1 elif ! killall -0 mysqld &>/dev/null;then pkill keepalived && exit 1 else exit 0 fi EOF fi
5、启动并测试keepalived集群资源转换功能
启动Server1的keepalived服务,查看keepalived的日志
Jan 22 16:14:36 Server1 Keepalived[21561]: Opening file '/etc/keepalived/keepalived.conf'. Jan 22 16:14:36 Server1 Keepalived[21562]: Starting Healthcheck child process, pid=21563 Jan 22 16:14:36 Server1 Keepalived[21562]: Starting VRRP child process, pid=21564 Jan 22 16:14:36 Server1 Keepalived_healthcheckers[21563]: Netlink reflector reports IP 10.17.83.174 added Jan 22 16:14:36 Server1 Keepalived_healthcheckers[21563]: Netlink reflector reports IP 192.168.6.174 added Jan 22 16:14:36 Server1 Keepalived_healthcheckers[21563]: Netlink reflector reports IP fe80::250:56ff:feb5:725f added Jan 22 16:14:36 Server1 Keepalived_healthcheckers[21563]: Netlink reflector reports IP fe80::250:56ff:feb5:5a15 added Jan 22 16:14:36 Server1 Keepalived_healthcheckers[21563]: Registering Kernel netlink reflector Jan 22 16:14:36 Server1 Keepalived_healthcheckers[21563]: Registering Kernel netlink command channel Jan 22 16:14:36 Server1 Keepalived_healthcheckers[21563]: Opening file '/etc/keepalived/keepalived.conf'. Jan 22 16:14:36 Server1 Keepalived_vrrp[21564]: Netlink reflector reports IP 10.17.83.174 added Jan 22 16:14:36 Server1 Keepalived_vrrp[21564]: Netlink reflector reports IP 192.168.6.174 added Jan 22 16:14:36 Server1 Keepalived_vrrp[21564]: Netlink reflector reports IP fe80::250:56ff:feb5:725f added Jan 22 16:14:36 Server1 Keepalived_vrrp[21564]: Netlink reflector reports IP fe80::250:56ff:feb5:5a15 added Jan 22 16:14:36 Server1 Keepalived_vrrp[21564]: Registering Kernel netlink reflector Jan 22 16:14:36 Server1 Keepalived_vrrp[21564]: Registering Kernel netlink command channel Jan 22 16:14:36 Server1 Keepalived_vrrp[21564]: Registering gratuitous ARP shared channel Jan 22 16:14:36 Server1 Keepalived_vrrp[21564]: Opening file '/etc/keepalived/keepalived.conf'. Jan 22 16:14:36 Server1 Keepalived_vrrp[21564]: Truncating auth_pass to 8 characters Jan 22 16:14:51 Server1 Keepalived_vrrp[21564]: VRRP_Instance(MySQL_HA_1) removing protocol VIPs. Jan 22 16:14:51 Server1 Keepalived_vrrp[21564]: SECURITY VIOLATION - scripts are being executed but script_security not enabled. Jan 22 16:14:51 Server1 Keepalived_vrrp[21564]: Sync group MySQL_HA_G1 has only 1 virtual router(s) - removing Jan 22 16:14:51 Server1 Keepalived_vrrp[21564]: Using LinkWatch kernel netlink reflector... Jan 22 16:14:51 Server1 Keepalived_vrrp[21564]: VRRP_Instance(MySQL_HA_1) Entering BACKUP STATE Jan 22 16:14:51 Server1 Keepalived_vrrp[21564]: VRRP sockpool: [ifindex(3), proto(112), unicast(0), fd(10,11)] Jan 22 16:14:51 Server1 Keepalived_healthcheckers[21563]: Using LinkWatch kernel netlink reflector... Jan 22 16:14:51 Server1 Keepalived_vrrp[21564]: pid 21567 exited with status 1 Jan 22 16:14:51 Server1 Keepalived_vrrp[21564]: VRRP_Script(chage_status) succeeded Jan 22 16:14:57 Server1 Keepalived_vrrp[21564]: VRRP_Script(chk_mysql) succeeded Jan 22 16:14:58 Server1 Keepalived_vrrp[21564]: VRRP_Instance(MySQL_HA_1) Transition to MASTER STATE Jan 22 16:14:59 Server1 Keepalived_vrrp[21564]: VRRP_Instance(MySQL_HA_1) Changing effective priority from 100 to 120 Jan 22 16:15:00 Server1 Keepalived_vrrp[21564]: VRRP_Instance(MySQL_HA_1) Entering MASTER STATE Jan 22 16:15:00 Server1 Keepalived_vrrp[21564]: VRRP_Instance(MySQL_HA_1) setting protocol VIPs. Jan 22 16:15:00 Server1 Keepalived_vrrp[21564]: Sending gratuitous ARP on eth1 for 192.168.6.176 Jan 22 16:15:00 Server1 Keepalived_healthcheckers[21563]: Netlink reflector reports IP 192.168.6.176 added Jan 22 16:15:00 Server1 Keepalived_vrrp[21564]: VRRP_Instance(MySQL_HA_1) Sending/queueing gratuitous ARPs on eth1 for 192.168.6.176 Jan 22 16:15:00 Server1 Keepalived_vrrp[21564]: Sending gratuitous ARP on eth1 for 192.168.6.176
然后再启动Server2的keepalived服务,查看keepalived日志
Jan 22 16:14:57 localhost Keepalived[26882]: Opening file '/etc/keepalived/keepalived.conf'. Jan 22 16:14:57 localhost Keepalived[26882]: daemon is already running Jan 22 16:15:01 localhost Keepalived_vrrp[26875]: VRRP_Instance(MySQL_HA_1) removing protocol VIPs. Jan 22 16:15:01 localhost Keepalived_vrrp[26875]: SECURITY VIOLATION - scripts are being executed but script_security not enabled. Jan 22 16:15:01 localhost Keepalived_vrrp[26875]: Sync group MySQL_HA_G1 has only 1 virtual router(s) - removing Jan 22 16:15:01 localhost Keepalived_vrrp[26875]: Using LinkWatch kernel netlink reflector... Jan 22 16:15:01 localhost Keepalived_vrrp[26875]: VRRP_Instance(MySQL_HA_1) Entering BACKUP STATE Jan 22 16:15:01 localhost Keepalived_vrrp[26875]: VRRP sockpool: [ifindex(3), proto(112), unicast(0), fd(10,11)] Jan 22 16:15:01 localhost Keepalived_healthcheckers[26874]: Using LinkWatch kernel netlink reflector... Jan 22 16:15:01 localhost Keepalived_vrrp[26875]: VRRP_Script(chage_status) succeeded Jan 22 16:15:01 localhost Keepalived_vrrp[26875]: pid 26885 exited with status 1 Jan 22 16:15:07 localhost Keepalived_vrrp[26875]: VRRP_Script(chk_mysql) succeeded Jan 22 16:15:09 localhost Keepalived_vrrp[26875]: VRRP_Instance(MySQL_HA_1) Changing effective priority from 90 to 110
由日志可以看出,Server1正常进入了MASTER角色,同时挂载共享磁盘,启动MySQL服务,而Server2根据check_mysql.sh检测脚本返回的状态,正常进入BACKUP角色。
查看Server1的资源服务状态
# 共享存储 [root@Server1 ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 ext4 47G 2.5G 43G 6% / tmpfs tmpfs 939M 0 939M 0% /dev/shm /dev/sda1 ext4 190M 35M 145M 20% /boot /dev/sdb1 ext4 50G 263M 47G 1% /data #VIP [root@Server1 ~]# ip addr | grep eth1 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000 inet 192.168.6.174/24 brd 192.168.6.255 scope global eth1 inet 192.168.6.176/24 scope global secondary eth1 #MySQL服务 [root@Server1 ~]# ps -ef | grep mysql root 21628 1 0 16:15 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql/mysql --pid-file=/data/mysql/mysql/Server1.pid mysql 21832 21628 0 16:15 ? 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/mysql/Server1.err --pid-file=/data/mysql/mysql/Server1.pid --socket=/tmp/mysql.sock --port=3306 root 22554 25788 0 16:22 pts/1 00:00:00 grep mysql [root@Server1 ~]# ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:10050 *:* LISTEN 0 80 :::3306 :::* LISTEN 0 128 :::22 :::* LISTEN 0 128 *:22 *:* LISTEN 0 100 ::1:25 :::* LISTEN 0 100 127.0.0.1:25 *:*
查看Server2的资源和服务状态
# 是否挂载了存储 [root@Server2 ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 ext4 47G 2.5G 43G 6% / tmpfs tmpfs 939M 0 939M 0% /dev/shm /dev/sda1 ext4 190M 35M 145M 20% /boot #是否有VIP [root@Server2 ~]# ip addr | grep eth1 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000 inet 192.168.6.175/24 brd 192.168.6.255 scope global eth1 #是否有MySQL服务 [root@Server2 ~]# ps -ef | grep mysql root 27652 1807 0 16:24 pts/1 00:00:00 grep mysql [root@Server2 ~]# netstat -ntl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp 0 0 :::22 :::* LISTEN tcp 0 0 ::1:25 :::* LISTEN
三、测试keepalived自动转移MySQL服务
1、Server1上停止MySQL服务,查看Server2是否接管MySQL服务
查看Server1的keepalived日志
Jan 22 16:29:58 Server1 Keepalived_vrrp[21564]: VRRP_Script(chk_mysql) failed Jan 22 16:29:58 Server1 Keepalived_vrrp[21564]: VRRP_Instance(MySQL_HA_1) Changing effective priority from 120 to 100 Jan 22 16:29:58 Server1 Keepalived_vrrp[21564]: VRRP_Instance(MySQL_HA_1) Received advert with higher priority 110, ours 100 Jan 22 16:29:58 Server1 Keepalived_vrrp[21564]: VRRP_Instance(MySQL_HA_1) Entering BACKUP STATE Jan 22 16:29:58 Server1 Keepalived_vrrp[21564]: VRRP_Instance(MySQL_HA_1) removing protocol VIPs. Jan 22 16:29:58 Server1 Keepalived_healthcheckers[21563]: Netlink reflector reports IP 192.168.6.176 removed Jan 22 16:30:04 Server1 Keepalived_vrrp[21564]: pid 23227 exited with status 1 Jan 22 16:30:10 Server1 Keepalived_vrrp[21564]: pid 23233 exited with status 1 Jan 22 16:30:16 Server1 Keepalived_vrrp[21564]: VRRP_Script(chk_mysql) succeeded Jan 22 16:30:16 Server1 Keepalived_vrrp[21564]: VRRP_Instance(MySQL_HA_1) Changing effective priority from 100 to 120
查看Server2的keepalived日志
Jan 22 16:15:09 localhost Keepalived_vrrp[26875]: VRRP_Instance(MySQL_HA_1) Changing effective priority from 90 to 110 Jan 22 16:17:01 localhost Keepalived_vrrp[26875]: pid 27059 exited with status 126 Jan 22 16:29:57 localhost Keepalived_vrrp[26875]: VRRP_Instance(MySQL_HA_1) forcing a new MASTER election Jan 22 16:29:59 localhost Keepalived_vrrp[26875]: VRRP_Instance(MySQL_HA_1) Transition to MASTER STATE Jan 22 16:30:01 localhost Keepalived_vrrp[26875]: VRRP_Instance(MySQL_HA_1) Entering MASTER STATE Jan 22 16:30:01 localhost Keepalived_vrrp[26875]: VRRP_Instance(MySQL_HA_1) setting protocol VIPs. Jan 22 16:30:01 localhost Keepalived_vrrp[26875]: Sending gratuitous ARP on eth1 for 192.168.6.176 Jan 22 16:30:01 localhost Keepalived_healthcheckers[26874]: Netlink reflector reports IP 192.168.6.176 added Jan 22 16:30:01 localhost Keepalived_vrrp[26875]: VRRP_Instance(MySQL_HA_1) Sending/queueing gratuitous ARPs on eth1 for 192.168.6.176 Jan 22 16:30:01 localhost Keepalived_vrrp[26875]: Sending gratuitous ARP on eth1 for 192.168.6.176 Jan 22 16:30:01 localhost Keepalived_vrrp[26875]: Sending gratuitous ARP on eth1 for 192.168.6.176 Jan 22 16:30:01 localhost Keepalived_vrrp[26875]: Sending gratuitous ARP on eth1 for 192.168.6.176 Jan 22 16:30:01 localhost Keepalived_vrrp[26875]: Sending gratuitous ARP on eth1 for 192.168.6.176 Jan 22 16:30:01 localhost kernel: EXT4-fs (sdb1): warning: mounting fs with errors, running e2fsck is recommended Jan 22 16:30:01 localhost kernel: EXT4-fs (sdb1): mounted filesystem with ordered data mode. Opts: Jan 22 16:30:06 localhost Keepalived_vrrp[26875]: Sending gratuitous ARP on eth1 for 192.168.6.176 Jan 22 16:30:06 localhost Keepalived_vrrp[26875]: VRRP_Instance(MySQL_HA_1) Sending/queueing gratuitous ARPs on eth1 for 192.168.6.176 Jan 22 16:30:06 localhost Keepalived_vrrp[26875]: Sending gratuitous ARP on eth1 for 192.168.6.176
2、查看Server2是否正常接管MySQL服务
Server2
[root@Server2 ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 ext4 47G 2.5G 43G 6% / tmpfs tmpfs 939M 0 939M 0% /dev/shm /dev/sda1 ext4 190M 35M 145M 20% /boot /dev/sdb1 ext4 50G 263M 47G 1% /data [root@Server2 ~]# ip a | grep eth1 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000 inet 192.168.6.175/24 brd 192.168.6.255 scope global eth1 inet 192.168.6.176/24 scope global secondary eth1 [root@Server2 ~]# ps -ef | grep mysql root 28124 1 0 16:30 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql/mysql --pid-file=/data/mysql/mysql/Server2.pid mysql 28327 28124 0 16:30 ? 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/mysql/Server2.err --pid-file=/data/mysql/mysql/Server2.pid --socket=/tmp/mysql.sock --port=3306 root 28685 1807 0 16:33 pts/1 00:00:00 grep mysql
Server1
[root@Server1 ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 ext4 47G 2.5G 43G 6% / tmpfs tmpfs 939M 0 939M 0% /dev/shm /dev/sda1 ext4 190M 35M 145M 20% /boot [root@Server1 ~]# ip addr | grep eth1 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000 inet 192.168.6.174/24 brd 192.168.6.255 scope global eth1 [root@Server1 ~]# ps -ef | grep mysql root 23617 25788 0 16:34 pts/1 00:00:00 grep mysql
3、再测试Server2异常时,是否将服务转移至Server1上
[root@Server2 ~]# /etc/init.d/mysqld stop Shutting down MySQL.. SUCCESS!
Server1
[root@Server1 ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 ext4 47G 2.5G 43G 6% / tmpfs tmpfs 939M 0 939M 0% /dev/shm /dev/sda1 ext4 190M 35M 145M 20% /boot /dev/sdb1 ext4 50G 263M 47G 1% /data [root@Server1 ~]# ip addr | grep eth1 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000 inet 192.168.6.174/24 brd 192.168.6.255 scope global eth1 inet 192.168.6.176/24 scope global secondary eth1 [root@Server1 ~]# ps -ef | grep mysql root 23845 1 0 16:37 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql/mysql --pid-file=/data/mysql/mysql/Server1.pid mysql 24048 23845 0 16:37 ? 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/mysql/Server1.err --pid-file=/data/mysql/mysql/Server1.pid --socket=/tmp/mysql.sock --port=3306 root 24144 25788 0 16:38 pts/1 00:00:00 grep mysql
Server2
[root@Server2 ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 ext4 47G 2.5G 43G 6% / tmpfs tmpfs 939M 0 939M 0% /dev/shm /dev/sda1 ext4 190M 35M 145M 20% /boot [root@Server2 ~]# ip a | grep eth1 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000 inet 192.168.6.175/24 brd 192.168.6.255 scope global eth1 [root@Server2 ~]# ps -ef | grep mysql root 29219 1807 0 16:38 pts/1 00:00:00 grep mysql
4、测试当Server1网卡掉线时,keepalived是否自动转移
Server1,停止eth1网卡
[root@Server1 ~]# ifconfig eth1 down [root@Server1 ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:50:56:B5:72:5F inet addr:10.17.83.174 Bcast:10.17.83.255 Mask:255.255.252.0 inet6 addr: fe80::250:56ff:feb5:725f/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:12403837 errors:0 dropped:0 overruns:0 frame:0 TX packets:915130 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:918776939 (876.2 MiB) TX bytes:58055577 (55.3 MiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:14 errors:0 dropped:0 overruns:0 frame:0 TX packets:14 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:956 (956.0 b) TX bytes:956 (956.0 b)
停止网卡后,Keepalived移除Server1的资源,重新选举MASTER角色,并将资源切换到Server2节点
[root@Server1 ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 ext4 47G 2.5G 43G 6% / tmpfs tmpfs 939M 0 939M 0% /dev/shm /dev/sda1 ext4 190M 35M 145M 20% /boot [root@Server1 ~]# ip addr | grep eth1 3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc mq state DOWN qlen 1000 inet 192.168.6.174/24 brd 192.168.6.255 scope global eth1 [root@Server1 ~]# ps -ef | grep mysql root 24553 25788 0 16:42 pts/1 00:00:00 grep mysql
Server2
[root@Server2 ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 ext4 47G 2.5G 43G 6% / tmpfs tmpfs 939M 0 939M 0% /dev/shm /dev/sda1 ext4 190M 35M 145M 20% /boot /dev/sdb1 ext4 50G 263M 47G 1% /data [root@Server2 ~]# ip a | grep eth1 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000 inet 192.168.6.175/24 brd 192.168.6.255 scope global eth1 inet 192.168.6.176/24 scope global secondary eth1 [root@Server2 ~]# ps -ef | grep mysql root 29425 1 0 16:40 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql/mysql --pid-file=/data/mysql/mysql/Server2.pid mysql 29628 29425 0 16:40 ? 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/mysql/Server2.err --pid-file=/data/mysql/mysql/Server2.pid --socket=/tmp/mysql.sock --port=3306 root 29839 1807 0 16:43 pts/1 00:00:00 grep mysql
5、同样可以测试keepalived服务自动down掉时是否发生转移
Server2
[root@Server2 ~]# /etc/init.d/keepalived stop Stopping keepalived: [ OK ] [root@Server2 ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 ext4 47G 2.5G 43G 6% / tmpfs tmpfs 939M 0 939M 0% /dev/shm /dev/sda1 ext4 190M 35M 145M 20% /boot [root@Server2 ~]# ip a | grep eth1 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000 inet 192.168.6.175/24 brd 192.168.6.255 scope global eth1
Server1
[root@Server1 ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 ext4 47G 2.5G 43G 6% / tmpfs tmpfs 939M 0 939M 0% /dev/shm /dev/sda1 ext4 190M 35M 145M 20% /boot /dev/sdb1 ext4 50G 263M 47G 1% /data [root@Server1 ~]# ip addr | grep eth1 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000 inet 192.168.6.174/24 brd 192.168.6.255 scope global eth1 inet 192.168.6.176/24 scope global secondary eth1 [root@Server1 ~]# ps -ef | grep mysql root 24912 1 0 16:46 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql/mysql --pid-file=/data/mysql/mysql/Server1.pid mysql 25115 24912 0 16:46 ? 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/mysql/Server1.err --pid-file=/data/mysql/mysql/Server1.pid --socket=/tmp/mysql.sock --port=3306 root 25204 25788 0 16:46 pts/1 00:00:00 grep mysql
总结
MySQL共享存储与Keepalived模式、MySQL复制与Keepalived模式,这两种方式各有各自的优缺点。对于简单的HA集群,都能实现其高可用。本篇只是简单介绍前者的实现方式,并且结合keeplived灵活的脚本定制,可以实现MySQL HA全自动切换,提高MySQL HA高可用的稳定性。那么使用共享存储的缺点也显而易见,就是存储单点了,如果共享存储数据的被损坏,那么整个HA也就都不可用了。因此,要提高安全性,最好定期备份数据库,避免数据丢失。