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