【需求】
根据需求需要搭建mysql主从架构数据库及加keepalived进行自动切换VIP(没有使用脚本进行检查对端数据库及互换主从)
【环境介绍】
系统环境:CentOS release 6.4 (Final) + Server version: 5.7.25-log MySQL Community Server (GPL) + Keepalived v1.2.7
系统 |
IP |
主机名 |
备注 |
版本 |
xx系统 |
192.168.8.100 |
mysqldb1 |
主库 |
Server version: 5.7.25-log MySQL Community Server (GPL) |
192.168.8.101 |
mysqldb2 |
备库 |
||
192.168.8.105 |
mysqldb1 |
主库(VIP) |
【搭建步骤:mysql软件部署】
可参考之前mysql主从搭建步骤
【搭建步骤:keepalived软件部署】
安装步骤:
使用系统自带的keepalived版本或者下载其他版本rpm安装。yum -y install keepalived*
节点一(主库)
[root@mysqldb1 keepalived]# rpm -qa |grep keepalived
keepalived-1.2.7-3.el6.x86_64
[root@mysqldb1 keepalived]#
节点二(从库)
[root@mysqldb2 ~]# rpm -qa |grep keepalived
keepalived-1.2.7-3.el6.x86_64
[root@mysqldb2 ~]#
keepalived配置文件:
! Configuration File for keepalived
#smtp_server smtp.163.com
smtp_connect_timeout 30
router_id LVS_DEVEL >>>可以自定义名字,跟从库一致
}
state MASTER >>>表明为master节点
interface eth1 >>>指定主机IP挂载的接口与从库一致
virtual_router_id 100 >>>指定ID标示,与从库一致
garp_master_delay 1
priority 100 >>>指定等级这里为100从库为99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
192.168.8.105/24 dev eth1 >>>VIP
}
[root@mysqldb1 keepalived]#
监控mysql数据库状态,输出check_mysql.log日志
#!/bin/bash
mysql=/var/lib/mysql/bin/mysql
mysql_host=localhost
mysql_user=root
mysql_password=mysql
check_time=3
mysql_ok=1
function check_mysql_health(){
$mysql -h $mysql_host -u$mysql_user -p$mysql_password -e 'show status;' >/dev/null 2>&1
if [ $? = 0 ] ;then
mysql_ok=1
echo 'mysql_ok'
else
mysql_ok=0
echo 'mysql_failed'
fi
return $mysql_ok
}
do
let "check_time -= 1"
check_mysql_health
if [ $mysql_ok = 1 ] ;then
check_time=0
exit 0
fi
then
pkill keepalived
fi
sleep 1
done
[root@mysqldb1 keepalived]#
定时任务
#check_mysql
*/1 * * * * bash /etc/keepalived/check_mysql.sh >> /etc/keepalived/check_mysql.log
节点二配置:
! Configuration File for keepalived
#smtp_server smtp.163.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
state MASTER
interface eth1
virtual_router_id 100
garp_master_delay 1
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
192.168.8.105/24 dev eth1
}
[root@mysqldb2 keepalived]# cat check_mysql.sh
#!/bin/bash
mysql=/var/lib/mysql/bin/mysql
mysql_host=localhost
mysql_user=root
mysql_password=mysql
check_time=3
mysql_ok=1
function check_mysql_health(){
$mysql -h $mysql_host -u$mysql_user -p$mysql_password -e 'show status;' >/dev/null 2>&1
if [ $? = 0 ] ;then
mysql_ok=1
echo 'mysql_ok'
else
mysql_ok=0
echo 'mysql_failed'
fi
return $mysql_ok
}
do
let "check_time -= 1"
check_mysql_health
if [ $mysql_ok = 1 ] ;then
check_time=0
exit 0
fi
then
pkill keepalived
fi
sleep 1
done
[root@mysqldb2 keepalived]#
启动keepalived服务:
主库启动keepalived,VIP已经挂载,查看日志为MASTER STATE模式
[root@mysqldb1 keepalived]# service keepalived start
Starting keepalived: [ OK ]
[root@mysqldb1 keepalived]# ip a
1: lo:
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: eth1:
link/ether 08:00:27:62:61:3a brd ff:ff:ff:ff:ff:ff
inet 192.168.8.100/24 brd 192.168.8.255 scope global eth1
inet 192.168.8.105/24 scope global secondary eth1
inet6 fe80::a00:27ff:fe62:613a/64 scope link
valid_lft forever preferred_lft forever
[root@mysqldb1 keepalived]#
查看keepalived日志:
tail -f messages
Mar 6 02:33:09 mysqldb1 Keepalived[12767]: Stopping Keepalived v1.2.7 (02/21,2013)
Mar 6 02:33:09 mysqldb1 Keepalived_vrrp[12770]: VRRP_Instance(VI_1) sending 0 priority
Mar 6 02:33:12 mysqldb1 Keepalived[12814]: Starting Keepalived v1.2.7 (02/21,2013)
Mar 6 02:33:12 mysqldb1 Keepalived[12815]: Starting Healthcheck child process, pid=12817
Mar 6 02:33:12 mysqldb1 Keepalived[12815]: Starting VRRP child process, pid=12818
Mar 6 02:33:12 mysqldb1 Keepalived_vrrp[12818]: Interface queue is empty
Mar 6 02:33:12 mysqldb1 Keepalived_vrrp[12818]: Netlink reflector reports IP 192.168.8.100 added
Mar 6 02:33:12 mysqldb1 Keepalived_vrrp[12818]: Netlink reflector reports IP fe80::a00:27ff:fe62:613a added
Mar 6 02:33:12 mysqldb1 Keepalived_vrrp[12818]: Registering Kernel netlink reflector
Mar 6 02:33:12 mysqldb1 Keepalived_vrrp[12818]: Registering Kernel netlink command channel
Mar 6 02:33:12 mysqldb1 Keepalived_healthcheckers[12817]: Interface queue is empty
Mar 6 02:33:12 mysqldb1 Keepalived_healthcheckers[12817]: Netlink reflector reports IP 192.168.8.100 added
Mar 6 02:33:12 mysqldb1 Keepalived_healthcheckers[12817]: Netlink reflector reports IP fe80::a00:27ff:fe62:613a added
Mar 6 02:33:12 mysqldb1 Keepalived_healthcheckers[12817]: Registering Kernel netlink reflector
Mar 6 02:33:12 mysqldb1 Keepalived_vrrp[12818]: Registering gratuitous ARP shared channel
Mar 6 02:33:12 mysqldb1 Keepalived_healthcheckers[12817]: Registering Kernel netlink command channel
Mar 6 02:33:12 mysqldb1 Keepalived_vrrp[12818]: Opening file '/etc/keepalived/keepalived.conf'.
Mar 6 02:33:12 mysqldb1 Keepalived_healthcheckers[12817]: Opening file '/etc/keepalived/keepalived.conf'.
Mar 6 02:33:12 mysqldb1 Keepalived_healthcheckers[12817]: Configuration is using : 5627 Bytes
Mar 6 02:33:12 mysqldb1 Keepalived_vrrp[12818]: Configuration is using : 61320 Bytes
Mar 6 02:33:12 mysqldb1 Keepalived_vrrp[12818]: Using LinkWatch kernel netlink reflector...
Mar 6 02:33:12 mysqldb1 Keepalived_vrrp[12818]: VRRP sockpool: [ifindex(2), proto(112), fd(10,11)]
Mar 6 02:33:12 mysqldb1 Keepalived_healthcheckers[12817]: Using LinkWatch kernel netlink reflector...
Mar 6 02:33:13 mysqldb1 Keepalived_vrrp[12818]: VRRP_Instance(VI_1) Transition to MASTER STATE
Mar 6 02:33:14 mysqldb1 Keepalived_vrrp[12818]: VRRP_Instance(VI_1) Entering MASTER STATE
Mar 6 02:33:14 mysqldb1 Keepalived_vrrp[12818]: VRRP_Instance(VI_1) setting protocol VIPs.
Mar 6 02:33:14 mysqldb1 Keepalived_vrrp[12818]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.8.105
Mar 6 02:33:14 mysqldb1 Keepalived_healthcheckers[12817]: Netlink reflector reports IP 192.168.8.105 added
Mar 6 02:33:15 mysqldb1 Keepalived_vrrp[12818]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.8.105
Mar 6 02:34:04 mysqldb1 Keepalived_vrrp[12818]: VRRP_Instance(VI_1) Received lower prio advert, forcing new election
Mar 6 02:34:04 mysqldb1 Keepalived_vrrp[12818]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.8.105
查看mysql数据库监控日志
[root@mysqldb1 keepalived]# tail -f check_mysql.log
mysql_ok
mysql_ok
mysql_ok
mysql_ok
mysql_ok
mysql_ok
mysql_ok
mysql_ok
mysql_ok
mysql_ok
从库启动keepalived,查看日志为BACKUP STATE模式
[root@mysqldb2 keepalived]# service keepalived start
Starting keepalived: [ OK ]
[root@mysqldb2 keepalived]#
tail -f messages
Mar 6 02:34:42 mysqldb2 Keepalived[12288]: Starting Keepalived v1.2.7 (02/21,2013)
Mar 6 02:34:42 mysqldb2 Keepalived[12289]: Starting Healthcheck child process, pid=12291
Mar 6 02:34:42 mysqldb2 Keepalived[12289]: Starting VRRP child process, pid=12292
Mar 6 02:34:42 mysqldb2 Keepalived_vrrp[12292]: Interface queue is empty
Mar 6 02:34:42 mysqldb2 Keepalived_healthcheckers[12291]: Interface queue is empty
Mar 6 02:34:42 mysqldb2 Keepalived_healthcheckers[12291]: Netlink reflector reports IP 192.168.8.101 added
Mar 6 02:34:42 mysqldb2 Keepalived_healthcheckers[12291]: Netlink reflector reports IP fe80::a00:27ff:fe53:eeca added
Mar 6 02:34:42 mysqldb2 Keepalived_healthcheckers[12291]: Registering Kernel netlink reflector
Mar 6 02:34:42 mysqldb2 Keepalived_healthcheckers[12291]: Registering Kernel netlink command channel
Mar 6 02:34:42 mysqldb2 Keepalived_vrrp[12292]: Netlink reflector reports IP 192.168.8.101 added
Mar 6 02:34:42 mysqldb2 Keepalived_vrrp[12292]: Netlink reflector reports IP fe80::a00:27ff:fe53:eeca added
Mar 6 02:34:42 mysqldb2 Keepalived_vrrp[12292]: Registering Kernel netlink reflector
Mar 6 02:34:42 mysqldb2 Keepalived_vrrp[12292]: Registering Kernel netlink command channel
Mar 6 02:34:42 mysqldb2 Keepalived_vrrp[12292]: Registering gratuitous ARP shared channel
Mar 6 02:34:42 mysqldb2 Keepalived_healthcheckers[12291]: Opening file '/etc/keepalived/keepalived.conf'.
Mar 6 02:34:42 mysqldb2 Keepalived_vrrp[12292]: Opening file '/etc/keepalived/keepalived.conf'.
Mar 6 02:34:42 mysqldb2 Keepalived_healthcheckers[12291]: Configuration is using : 5610 Bytes
Mar 6 02:34:42 mysqldb2 Keepalived_vrrp[12292]: Configuration is using : 61303 Bytes
Mar 6 02:34:42 mysqldb2 Keepalived_vrrp[12292]: Using LinkWatch kernel netlink reflector...
Mar 6 02:34:42 mysqldb2 Keepalived_vrrp[12292]: VRRP sockpool: [ifindex(2), proto(112), fd(10,11)]
Mar 6 02:34:42 mysqldb2 Keepalived_healthcheckers[12291]: Using LinkWatch kernel netlink reflector...
Mar 6 02:34:43 mysqldb2 Keepalived_vrrp[12292]: VRRP_Instance(VI_1) Transition to MASTER STATE
Mar 6 02:34:43 mysqldb2 Keepalived_vrrp[12292]: VRRP_Instance(VI_1) Received higher prio advert
Mar 6 02:34:43 mysqldb2 Keepalived_vrrp[12292]: VRRP_Instance(VI_1) Entering BACKUP STATE
查看数据库监控日志
[root@mysqldb2 keepalived]# tail -f check_mysql.log
mysql_ok
mysql_ok
mysql_ok
mysql_ok
mysql_ok
mysql_ok
mysql_ok
mysql_ok
mysql_ok
mysql_ok
【进行远程连接】
连接为VIP,端口为3306,schema为test
查看连接是为节点一主机
至此keepalived搭建完成
【进行keepalived模拟自动切换】
停止节点一mysql主数据库
[root@mysqldb1 keepalived]# mysqladmin --defaults-file=/etc/my.cnf -P3306 shutdown -uroot -pmysql
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[1]+ Done mysqld --defaults-file=/etc/my.cnf --user=mysql
[root@mysqldb1 keepalived]#
查看节点一mysql进程监控日志
[root@mysqldb1 keepalived]# tail -f check_mysql.log
mysql_ok
mysql_ok
mysql_ok
mysql_ok
mysql_failed
mysql_failed
mysql_failed
查看节点一keepalived日志信息
Mar 6 02:33:12 mysqldb1 Keepalived_healthcheckers[12817]: Using LinkWatch kernel netlink reflector...
Mar 6 02:33:13 mysqldb1 Keepalived_vrrp[12818]: VRRP_Instance(VI_1) Transition to MASTER STATE
Mar 6 02:33:14 mysqldb1 Keepalived_vrrp[12818]: VRRP_Instance(VI_1) Entering MASTER STATE
Mar 6 02:33:14 mysqldb1 Keepalived_vrrp[12818]: VRRP_Instance(VI_1) setting protocol VIPs.
Mar 6 02:33:14 mysqldb1 Keepalived_vrrp[12818]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.8.105
Mar 6 02:33:14 mysqldb1 Keepalived_healthcheckers[12817]: Netlink reflector reports IP 192.168.8.105 added
Mar 6 02:33:15 mysqldb1 Keepalived_vrrp[12818]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.8.105
Mar 6 02:34:04 mysqldb1 Keepalived_vrrp[12818]: VRRP_Instance(VI_1) Received lower prio advert, forcing new election
Mar 6 02:34:04 mysqldb1 Keepalived_vrrp[12818]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.8.105
Mar 6 02:34:42 mysqldb1 Keepalived_vrrp[12818]: VRRP_Instance(VI_1) Received lower prio advert, forcing new election
Mar 6 02:34:42 mysqldb1 Keepalived_vrrp[12818]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.8.105
Mar 6 02:54:03 mysqldb1 Keepalived[12815]: Stopping Keepalived v1.2.7 (02/21,2013) 》》》keepalived进程已经停止
查看节点一keepalived进程状态
[root@mysqldb1 keepalived]# service keepalived status
keepalived dead but subsys locked
[root@mysqldb1 keepalived]#
查看节点二备库keepalived日志信息,VIP已经漂移过来
Mar 6 02:34:42 mysqldb2 Keepalived_healthcheckers[12291]: Using LinkWatch kernel netlink reflector...
Mar 6 02:34:43 mysqldb2 Keepalived_vrrp[12292]: VRRP_Instance(VI_1) Transition to MASTER STATE
Mar 6 02:34:43 mysqldb2 Keepalived_vrrp[12292]: VRRP_Instance(VI_1) Received higher prio advert
Mar 6 02:34:43 mysqldb2 Keepalived_vrrp[12292]: VRRP_Instance(VI_1) Entering BACKUP STATE
Mar 6 02:54:06 mysqldb2 Keepalived_vrrp[12292]: VRRP_Instance(VI_1) Transition to MASTER STATE
Mar 6 02:54:07 mysqldb2 Keepalived_vrrp[12292]: VRRP_Instance(VI_1) Entering MASTER STATE
Mar 6 02:54:07 mysqldb2 Keepalived_vrrp[12292]: VRRP_Instance(VI_1) setting protocol VIPs.
Mar 6 02:54:07 mysqldb2 Keepalived_vrrp[12292]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.8.105
Mar 6 02:54:07 mysqldb2 Keepalived_healthcheckers[12291]: Netlink reflector reports IP 192.168.8.105 added
Mar 6 02:54:08 mysqldb2 Keepalived_vrrp[12292]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.8.105
测试远程连接VIP则连接到节点二数据库
【回切mysql主节点keepalived服务】
启动节点一主数据库
[root@mysqldb1 keepalived]# mysqld --defaults-file=/etc/my.cnf --user=mysql &
[1] 13039
[root@mysqldb1 keepalived]# ps -ef |grep mysql
root 12836 4599 0 02:35 pts/2 00:00:00 tail -f check_mysql.log
mysql 13039 1999 8 02:55 pts/0 00:00:00 mysqld --defaults-file=/etc/my.cnf --user=mysql
root 13073 1999 0 02:55 pts/0 00:00:00 grep mysql
[root@mysqldb1 keepalived]#
查看节点一keepalived的mysql数据库监控日志,已经为正常
[root@mysqldb1 keepalived]# tail -f check_mysql.log
mysql_ok
mysql_ok
mysql_ok
mysql_failed
mysql_failed
mysql_failed
mysql_failed
mysql_failed
mysql_failed
mysql_ok
mysql_ok
mysql_ok
mysql_ok
手动启动节点一keepalived进程,VIP已经回切
[root@mysqldb1 keepalived]# service keepalived start
Starting keepalived: [ OK ]
[root@mysqldb1 keepalived]# ip a
1: lo:
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: eth1:
link/ether 08:00:27:62:61:3a brd ff:ff:ff:ff:ff:ff
inet 192.168.8.100/24 brd 192.168.8.255 scope global eth1
inet 192.168.8.105/24 scope global secondary eth1
inet6 fe80::a00:27ff:fe62:613a/64 scope link
valid_lft forever preferred_lft forever
[root@mysqldb1 keepalived]# service keepalived status
keepalived (pid 13139) is running...
[root@mysqldb1 keepalived]#
查看节点一的keepalived日志,VIIP已经回切
Mar 6 02:54:03 mysqldb1 Keepalived[12815]: Stopping Keepalived v1.2.7 (02/21,2013)
Mar 6 02:57:56 mysqldb1 Keepalived[13138]: Starting Keepalived v1.2.7 (02/21,2013)
Mar 6 02:57:56 mysqldb1 Keepalived[13139]: Starting Healthcheck child process, pid=13141
Mar 6 02:57:56 mysqldb1 Keepalived[13139]: Starting VRRP child process, pid=13142
Mar 6 02:57:56 mysqldb1 Keepalived_vrrp[13142]: Interface queue is empty
Mar 6 02:57:56 mysqldb1 Keepalived_vrrp[13142]: Netlink reflector reports IP 192.168.8.100 added
Mar 6 02:57:56 mysqldb1 Keepalived_vrrp[13142]: Netlink reflector reports IP 192.168.8.105 added
Mar 6 02:57:56 mysqldb1 Keepalived_vrrp[13142]: Netlink reflector reports IP fe80::a00:27ff:fe62:613a added
Mar 6 02:57:56 mysqldb1 Keepalived_vrrp[13142]: Registering Kernel netlink reflector
Mar 6 02:57:56 mysqldb1 Keepalived_vrrp[13142]: Registering Kernel netlink command channel
Mar 6 02:57:56 mysqldb1 Keepalived_vrrp[13142]: Registering gratuitous ARP shared channel
Mar 6 02:57:56 mysqldb1 Keepalived_healthcheckers[13141]: Interface queue is empty
Mar 6 02:57:56 mysqldb1 Keepalived_healthcheckers[13141]: Netlink reflector reports IP 192.168.8.100 added
Mar 6 02:57:56 mysqldb1 Keepalived_vrrp[13142]: Opening file '/etc/keepalived/keepalived.conf'.
Mar 6 02:57:56 mysqldb1 Keepalived_healthcheckers[13141]: Netlink reflector reports IP 192.168.8.105 added
Mar 6 02:57:56 mysqldb1 Keepalived_healthcheckers[13141]: Netlink reflector reports IP fe80::a00:27ff:fe62:613a added
Mar 6 02:57:56 mysqldb1 Keepalived_healthcheckers[13141]: Registering Kernel netlink reflector
Mar 6 02:57:56 mysqldb1 Keepalived_healthcheckers[13141]: Registering Kernel netlink command channel
Mar 6 02:57:56 mysqldb1 Keepalived_vrrp[13142]: Configuration is using : 61320 Bytes
Mar 6 02:57:56 mysqldb1 Keepalived_vrrp[13142]: Using LinkWatch kernel netlink reflector...
Mar 6 02:57:56 mysqldb1 Keepalived_healthcheckers[13141]: Opening file '/etc/keepalived/keepalived.conf'.
Mar 6 02:57:56 mysqldb1 Keepalived_healthcheckers[13141]: Configuration is using : 5627 Bytes
Mar 6 02:57:56 mysqldb1 Keepalived_vrrp[13142]: VRRP sockpool: [ifindex(2), proto(112), fd(10,11)]
Mar 6 02:57:56 mysqldb1 Keepalived_healthcheckers[13141]: Using LinkWatch kernel netlink reflector...
Mar 6 02:57:56 mysqldb1 Keepalived_vrrp[13142]: VRRP_Instance(VI_1) Transition to MASTER STATE
Mar 6 02:57:57 mysqldb1 Keepalived_vrrp[13142]: VRRP_Instance(VI_1) Entering MASTER STATE
Mar 6 02:57:57 mysqldb1 Keepalived_vrrp[13142]: VRRP_Instance(VI_1) setting protocol VIPs.
Mar 6 02:57:57 mysqldb1 Keepalived_vrrp[13142]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.8.105
Mar 6 02:57:58 mysqldb1 Keepalived_vrrp[13142]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.8.105
查看节点二keepalived日志,keepalived为BACKUP STATE,VIP已经移除
Mar 6 02:54:07 mysqldb2 Keepalived_vrrp[12292]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.8.105
Mar 6 02:54:07 mysqldb2 Keepalived_healthcheckers[12291]: Netlink reflector reports IP 192.168.8.105 added
Mar 6 02:54:08 mysqldb2 Keepalived_vrrp[12292]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.8.105
Mar 6 02:57:57 mysqldb2 Keepalived_vrrp[12292]: VRRP_Instance(VI_1) Received higher prio advert
Mar 6 02:57:57 mysqldb2 Keepalived_vrrp[12292]: VRRP_Instance(VI_1) Entering BACKUP STATE
Mar 6 02:57:57 mysqldb2 Keepalived_vrrp[12292]: VRRP_Instance(VI_1) removing protocol VIPs.
Mar 6 02:57:57 mysqldb2 Keepalived_healthcheckers[12291]: Netlink reflector reports IP 192.168.8.105 removed
测试远程VIP连接数据库为节点一