keepalived+MySQL雙主搭建

keepalived+MySQL雙主搭建過程
首先要簡單瞭解一下keepalived:
Keepalived是Linux下一個輕量級別的高可用解決方案。高可用(High Avalilability,HA),其實兩種不一樣的含義:廣義來說,是指整個系統的高可用行,狹義的來說就是之主機的冗餘和接管,
它與HeartBeat RoseHA 實現相同相似的功能,均可以實現服務或者網絡的高可用,可是又有差異,HeartBeat是一個專業的、功能完善的高可用軟件,它提供了HA 軟件所需的基本功能,好比:心跳檢測、資源接管,檢測集羣中的服務,在集羣節點轉移共享IP地址的全部者等等。HeartBeat功能強大,可是部署和使用相對比較麻煩,
與HeartBeat相比,Keepalived主要是經過虛擬路由冗餘來實現高可用功能,雖然它沒有HeartBeat功能強大,可是Keepalived部署和使用很是的簡單,全部配置只須要一個配置文件便可以完成。
keepalived的工做原理以下:
Keepalived工做在TCP/IP 參考模型的 三層、四層、五層,也就是分別爲:網絡層,
傳輸層和應用層,根據TCP、IP參數模型隔層所能實現的功能,Keepalived運行機制以下:
在網絡層:咱們知道運行這4個重要的協議,互聯網絡IP協議,互聯網絡可控制報文協議ICMP、地址轉換協議ARP、反向地址轉換協議RARP,在網絡層Keepalived在網絡層採用最多見的工做方式是經過ICMP協議向服務器集羣中的每個節點發送一個ICMP數據包(有點相似與Ping的功能),若是某個節點沒有返回響應數據包,那麼認爲該節點發生了故障,Keepalived將報告這個節點失效,並從服務器集羣中剔除故障節點。
在傳輸層:提供了兩個主要的協議:傳輸控制協議TCP和用戶數據協議UDP,傳輸控制協議TCP能夠提供可靠的數據輸出服務、IP地址和端口,表明TCP的一個鏈接端,要得到TCP服務,須要在發送機的一個端口和接收機的一個端口上創建鏈接,而Keepalived在傳輸層裏利用了TCP協議的端口鏈接和掃描技術來判斷集羣節點的端口是否正常,好比對於常見的WEB服務器80端口。或者SSH服務22端口,Keepalived一旦在傳輸層探測到這些端口號沒有數據響應和數據返回,就認爲這些端口發生異常,而後強制將這些端口所對應的節點從服務器集羣中剔除掉。
在應用層:能夠運行FTP,TELNET,SMTP,DNS等各類不一樣類型的高層協議,Keepalived的運行方式也更加全面化和複雜化,用戶能夠經過自定義Keepalived工做方式,例如:能夠經過編寫程序或者腳原本運行Keepalived,而Keepalived將根據用戶的設定參數檢測各類程序或者服務是否容許正常,若是Keepalived的檢測結果和用戶設定的不一致時,Keepalived將把對應的服務器從服務器集羣中剔除
看一下基本環境
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

咱們假設MySQL雙主已經搭建成功了,若是還不會的話,能夠看個人之前博客,MySQL二進制安裝雙主結構: http://www.cnblogs.com/shengdimaya/p/6839542.html
下面開始搭建咱們的環境
1:安裝以及簡單配置keepalived
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
}
}

 

此配置文件就是最簡單的配置虛擬IP的文件了,接下來咱們在172.16.16.34上啓動keepalived
/etc/init.d/keepalived start

 

查看機器IP:
[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

 

能夠看到,VIP已經添加到了server1上,接下來咱們在server2安裝啓動keepalived,啓動,全部步驟都同樣,只不過server2的keepalived做爲備機,並不會持有VIP。
咱們在server1上執行
/etc/init.d/keepalived stop

 

執行之後在ip addr看,VIP如今已是server2持有了。因此最最基本的功能是已經完成了,可是離咱們雙主自動切換仍是有很大的差距的,下面咱們繼續去對keepalived的文件進行改造,直到達到咱們滿意爲止:
下面咱們修改配置文件,達到咱們須要的目的(下面以server2的kp文件爲例,server1的和server2同樣,只要替換掉相應IP爲34就能夠了):
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
}
}

 

看一下checkmysql.sh這個腳本:
 
  

#!/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
中間VIP的切換是不會影響到客戶端的操做的,可是在切換過程是有那麼一段時間是不能訪問的。
  
相關文章
相關標籤/搜索