server1:MySQL5.7.14+keepalived1.2+172.16.16.34 server2:MySQL5.7.14+keepalived1.2+172.16.16.35 VIP:172.16.16.20
咱们两台机器是搭建的MySQL双主,咱们平时只会经过VIP对MySQL进行读写,咱们要实现的是,当VIP所在的主机的MySQLDOWN掉之后,VIP可以切换到另一台机器上而且继续提供服务。html
yum install -y keepalived
[root@localhost maxiangqian]# rpm -ql keepalived
[root@localhost maxiangqian]# vi /etc/keepalived/keepalived.conf vrrp_instance VI_20 { state BACKUP nopreempt interface eth0 virtual_router_id 20 priority 100 advert_int 5 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.16.16.20 } }
/etc/init.d/keepalived start
[root@localhost maxiangqian]# ip addr |grep 172.16 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000 inet 172.16.16.34/24 brd 172.16.16.255 scope global eth0 inet 172.16.16.20/32 scope global eth0
/etc/init.d/keepalived stop
vrrp_script checkmysql { script "/etc/keepalived/checkmysql.sh" interval 10 #监控脚本,每十秒运行一次 } vrrp_instance VI_20 { state BACKUP #状态只有MASTER和BACKUP两种,而且要大写,MASTER为工做状态,BACKUP是备用状态 nopreempt #非抢占模式 interface eth0 virtual_router_id 20 priority 100 #权重,同一个vrrp_instance的MASTER优先级必须比BACKUP高。咱们使用非抢占模式,设置相同便可 advert_int 5 #MASTER 与BACKUP 负载均衡器之间同步检查的时间间隔,单位为秒 authentication { auth_type PASS #验证authentication。包含验证类型和验证密码。类型主要有PASS、AH 两种,一般使用的类型为PASS auth_pass 1111 } track_script { #执行定义的监控脚本 checkmysql } virtual_ipaddress { 172.16.16.20/24 } }
#!/bin/sh
#isok=$(sed -n '2p' /etc/keepalived/result.txt)
isok=$(/usr/local/mysql/bin/mysql -uroot -p123456 -e 'select 1' |sed -n '2p')
function error_query(){
service keepalived stop
echo "172.16.16.34 mysql down, keepalived 切换" | mail -s "34MySQL+keepalived通知" ma.xiangqian@sf-express.com
}
echo "$isok"
if [ "$isok" != "1" ]
then
#echo 'diaoyong error'
error_query
fimysql
如今咱们执行如下语句,重新load如下keepalived的配置文件:sql
/etc/init.d/keepalived reload server1和server2都要执行从新load一下新的配置文件,下面咱们测试一下当server1 MySQL DOWN掉的话会发生什么: server1:shutdown MySQL server1和server2:tail -f /var/log/messages
server1信息:mongodb
May 15 15:35:34 localhost Keepalived_healthcheckers[22987]: TCP connection to [172.16.16.34]:3306 failed !!! May 15 15:35:34 localhost Keepalived_healthcheckers[22987]: Removing service [172.16.16.34]:3306 from VS [172.16.16.20]:3306 May 15 15:35:34 localhost Keepalived_healthcheckers[22987]: IPVS : Virtual service [172.16.16.20]:3306 illegal timeout. May 15 15:35:34 localhost Keepalived_healthcheckers[22987]: Executing [/etc/keepalived/shutdown.sh #检测到服务down后执行的脚本] for service [172.16.16.34]:3306 in VS [172.16.16.20]:3306 May 15 15:35:34 localhost Keepalived_healthcheckers[22987]: Lost quorum 1-0=1 > 0 for VS [172.16.16.20]:3306 May 15 15:36:04 localhost Keepalived_vrrp[22988]: VRRP_Script(checkmysql) failed May 15 15:36:06 localhost Keepalived_vrrp[22988]: VRRP_Instance(VI_20) Entering FAULT STATE May 15 15:36:06 localhost Keepalived_vrrp[22988]: VRRP_Instance(VI_20) removing protocol VIPs. May 15 15:36:06 localhost Keepalived_vrrp[22988]: VRRP_Instance(VI_20) Now in FAULT state May 15 15:36:06 localhost Keepalived_healthcheckers[22987]: Netlink reflector reports IP 172.16.16.20 removed
server2信息:express
May 15 15:24:58 mxqmongodb2 Keepalived_healthcheckers[3093]: IPVS : Virtual service [172.16.16.20]:3306 illegal timeout. May 15 15:24:58 mxqmongodb2 Keepalived_healthcheckers[3093]: Using LinkWatch kernel netlink reflector... May 15 15:24:58 mxqmongodb2 Keepalived_healthcheckers[3093]: Activating healthchecker for service [172.16.16.35]:3306 May 15 15:24:58 mxqmongodb2 Keepalived_vrrp[3094]: VRRP_Script(checkmysql) succeeded May 15 15:36:04 mxqmongodb2 Keepalived_vrrp[3094]: VRRP_Instance(VI_20) Transition to MASTER STATE May 15 15:36:09 mxqmongodb2 Keepalived_vrrp[3094]: VRRP_Instance(VI_20) Entering MASTER STATE May 15 15:36:09 mxqmongodb2 Keepalived_vrrp[3094]: VRRP_Instance(VI_20) setting protocol VIPs. May 15 15:36:09 mxqmongodb2 Keepalived_vrrp[3094]: VRRP_Instance(VI_20) Sending gratuitous ARPs on eth0 for 172.16.16.20 May 15 15:36:09 mxqmongodb2 Keepalived_healthcheckers[3093]: Netlink reflector reports IP 172.16.16.20 added May 15 15:36:14 mxqmongodb2 Keepalived_vrrp[3094]: VRRP_Instance(VI_20) Sending gratuitous ARPs on eth0 for 172.16.16.20
咱们能够看到虚IP连接已经切换了,咱们从客户端两个时间点执行MySQL的操做也能够很明显看到切换:服务器
mysql> select @@server_id; +-------------+ | @@server_id | +-------------+ | 343306 | +-------------+ 1 row in set mysql> select @@server_id; +-------------+ | @@server_id | +-------------+ | 353306 | +-------------+ 1 row in set