大體分兩層結構:用戶空間 user space和內核空間 kernel spacehtml
Keepalived裏面全部對LVS的相關操做並不直接使用ipvsadm這樣的用戶端程序,而是直接使用IPVS提供的函數進程操做,該代碼都在check/ipwrapper.c中mysql
1)先實施Master->Slave的主主同步。主主是數據雙向同步,主從是數據單向同步。通常狀況下,主庫宕機後,須要手動將鏈接切換到從庫上。(可是用keepalived就能夠自動切換)
2)再結合Keepalived的使用,經過VIP實現Mysql雙主對外鏈接的統一接口。即客戶端經過Vip鏈接數據庫;當其中一臺宕機後,VIP會漂移到另外一臺上,這個過程對於客戶端的數據鏈接來講幾乎無感受,從而實現高可用。
MySQL主從與雙主搭建請參考:http://www.cnblogs.com/wade-lt/p/9008058.html
環境描述:
mysql的安裝能夠參考:http:
//www
.cnblogs.com
/kevingrace/p/6109679
.html
Centos7.4版本
Master:192.168.0.103 安裝mysql和keepalived
Slave : 192.168.0.104 安裝mysql和keepalived
VIP:192.168.0.102
要實現主主同步,能夠先實現主從同步,即master1->master2的主從同步,而後master2->master1的主從同步.
這樣,雙方就完成了主主同步。
1)安裝keepalived並將其配置成系統服務。master和salve兩臺機器上一樣進行以下操做:
[root@master ~]# yum install -y openssl-devel #安裝依賴
[root@master ~]# cd /tmp
[root@master src]# tar -zvxf keepalived-1.4.3.tar.gz #官網下載並上傳至服務器:http://www.keepalived.org/software/(官網)
[root@master src]# cd keepalived-1.4.3
[root@master keepalived-1.4.3]# ./configure --prefix=/usr/local/keepalived
[root@master keepalived-1.4.3]# make && make install
[root@master keepalived-1.4.3]# cp /usr/local/src/keepalived-1.4.3/keepalived/etc/init.d/keepalived /etc/rc.d/init.d/
[root@master keepalived-1.4.3]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@master keepalived-1.4.3]# mkdir /etc/keepalived/
[root@master keepalived-1.4.3]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@master keepalived-1.4.3]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@master keepalived-1.4.3]# echo "/etc/init.d/keepalived start" >> /etc/rc.local #centos 7 /etc/rc.local ---> /etc/rc.d/rc.local 未能開啓自啓,修改文件
2)配置master機器上的keepalived.conf配置。(下面配置中沒有使用lvs的負載均衡功能,因此不須要配置虛擬服務器virtual server)
注:keepalive.conf文件不能添加多餘的字符如「#」註釋等,「{}」與標識之間要空一個字符,keepalive.conf詳解參考:https://www.cnblogs.com/along1226/p/5027838.html
[root@master ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[root@master ~]# vim /etc/keepalived/keepalived.conf #清空默認內容,直接採用下面配置:
! Configuration File for keepalived
global_defs {
notification_email { #指定keepalived在發生切換時須要發送email到的對象,一行一個
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc #指定發件人
smtp_server 127.0.0.1 #指定smtp服務器地址
smtp_connect_timeout 30 #指定smtp鏈接超時時間
router_id MASTER-HA #運行keepalived機器的一個標識
}
vrrp_script chk_mysql_port { #檢測mysql服務是否在運行。有不少方式,好比進程,用腳本檢測等等
script "/opt/chk_mysql.sh" #這裏經過腳本監測
interval 2 #腳本執行間隔,每2s檢測一次
weight -5 #腳本結果致使的優先級變動,檢測失敗(腳本返回非0)則優先級 -5
fall 2 #檢測連續2次失敗纔算肯定是真失敗。會用weight減小優先級(1-255之間
rise 1 #檢測1次成功就算成功。但不修改優先級
}
vrrp_instance VI_1 {
state MASTER #指定那個爲master,那個爲backup,若是設置了nopreempt這個值不起做用,主備由priority決定
interface ens33 #設置實例綁定的網卡
mcast_src_ip 192.168.0.103 #發送多播包的地址,若是不設置默認使用綁定網卡的primary ip
virtual_router_id 51 #路由器標識,MASTER和BACKUP必須是一致的
priority 101 #定義優先級,數字越大,優先級越高,在同一個vrrp_instance下,MASTER的優先級必須大於BACKUP的優先級。這樣MASTER故障恢復後,就能夠將VIP資源再次搶回來
advert_int 1 #檢查間隔,默認1秒
authentication {#設置認證
auth_type PASS #認證方式
auth_pass 1111 #認證密碼
}
virtual_ipaddress {#設置vip
192.168.0.102/24 #與綁定的網卡實ip掩碼相同
}
track_script { #mysql腳本檢測
chk_mysql_port
}
}
3)編寫切換腳本。KeepAlived作心跳檢測,若是Master的MySQL服務掛了(3306端口掛了),那麼它就會選擇自殺。Slave的KeepAlived經過心跳檢測發現這個狀況,就會將VIP的請求接管
[root@master ~]# vim /opt/chk_mysql.sh
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
/etc/init.d/keepalived stop
fi
[root@master ~]# chmod 755 /opt/chk_mysql.sh
啓動keepalived服務
[root@master ~]# /etc/init.d/keepalived start
正在啓動 keepalived: [肯定]
linux
4)slave機器上的keepalived配置。slave機器上的keepalived.conf文件只修改priority爲9九、nopreempt不設置、real_server設置本地IP。sql
[root@slave ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[root@slave ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MASTER-HA
}
vrrp_script chk_mysql_port {
script "/opt/chk_mysql.sh"
interval 2
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
mcast_src_ip 192.168.0.104
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.102/24
}
track_script {
chk_mysql_port
}
}
[root@slave ~]# cat /opt/chk_mysql.sh
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
/etc/init.d/keepalived stop
fi
[root@slave ~]# chmod 755 /opt/chk_mysql.sh
[root@slave ~]# /etc/init.d/keepalived start
正在啓動 keepalived: [肯定]
注:若有防火牆,請開放相關權限,如:兩臺主機之間的通訊,vrrp通訊以及mysql的3306端口,關閉selinux /etc/selinux/conf ,修改後並使之生效
Mysql+keepalived故障轉移的高可用測試
1)經過Mysql客戶端經過VIP鏈接,看是否鏈接成功。
好比,在遠程一臺測試機上鍊接,經過vip地址能夠正常鏈接(下面的鏈接權限要是在服務端提早受權的)
[root@dev-new-test ~]# mysql -h192.168.0.103 -uroot -p123456
2)默認狀況下,vip是在master上的。使用"ip a"命令查看vip切換狀況
[root@master ~]# ip a
中止master機器上的mysql服務,根據配置中的腳本,mysql服務停了,keepalived也會停,從而vip資源將會切換到slave機器上。(mysql服務沒有起來的時候,keepalived服務也沒法順利啓動!)
[root@master ~]# /etc/init.d/mysql stop
Shutting down MySQL.. SUCCESS!
[root@master ~]# ps -ef|grep mysql
root 25812 21588 0 17:30 pts/0 00:00:00 grep mysql
[root@master ~]# ps -ef|grep keepalived
root 25814 21588 0 17:30 pts/0 00:00:00 grep keepalived
[root@master ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:3c:25:42 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.103/24 brd 192.168.0.255 scope global ens33
inet6 fe80::5054:ff:fe3c:2542/64 scope link
valid_lft forever preferred_lft forever
如上結果,發現vip沒有了,說明此時vip資源已不在master機器上了
查看下master的系統日誌,以下,會發現vip資源已經切換走了
[root@master ~]# tail -f /var/log/messages 數據庫
再到slave機器上,發現vip資源的確切換過來了
[root@slave ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:95:1f:6d brd ff:ff:ff:ff:ff:ff
inet 192.168.0.104/24 brd 192.168.0.255 scope global ens33
inet 192.168.0.102/24 scope global ens33
inet6 fe80::5054:ff:fe95:1f6d/64 scope link
valid_lft forever preferred_lft forever
查看slave的系統日誌
[root@slave ~]# tail -f /var/log/messages
3)再次啓動master的mysql和keepalived服務。(注意:若是restart重啓mysql,那麼還要啓動下keepalived,由於mysql重啓,根據腳本會形成keepalived關閉)
注意:必定要先啓動mysql服務,而後再啓動keepalived服務。若是先啓動keepalived服務,按照上面的配置,mysql沒有起來,就會自動關閉keepalived。
[root@master ~]# /etc/init.d/mysql start
Starting MySQL.. SUCCESS!
[root@master ~]# /etc/init.d/keepalived start
正在啓動 keepalived: [肯定]
啓動這兩個服務器後,稍微等過一下子,注意觀察會發現vip資源再次從slave機器上切換回來了。
[root@master ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:3c:25:42 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.103/24 brd 192.168.0.255 scope global ens33
inet 192.168.0.102/24 scope global ens33
inet6 fe80::5054:ff:fe3c:2542/64 scope link
valid_lft forever preferred_lft forever
[root@master ~]# tail -f /var/log/messages
再看看slave機器,發現vip資源又被恢復後的master搶過去了
[root@slave ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:95:1f:6d brd ff:ff:ff:ff:ff:ff
inet 192.168.0.104/24 brd 192.168.0.255 scope global ens33
inet6 fe80::5054:ff:fe95:1f6d/64 scope link
valid_lft forever preferred_lft forever
[root@slave ~]# tail -f /var/log/messages
4)一樣,關閉master1機器的keepalived服務,vip資源會自動切換到master2機器上。當master1的keepalived服務恢復後,會將vip資源再次切回來。
以上在vip資源切換過程當中,對於客戶端鏈接mysql(使用vip鏈接)來講幾乎是沒有任何影響的。
------------------------------------舒適提示(Keepalived的搶佔和非搶佔模式)---------------------------------------
keepalive是基於vrrp協議在linux主機上以守護進程方式,根據配置文件實現健康檢查。
VRRP是一種選擇協議,它能夠把一個虛擬路由器的責任動態分配到局域網上的VRRP路由器中的一臺。
控制虛擬路由器IP地址的VRRP路由器稱爲主路由器,它負責轉發數據包到這些虛擬IP地址。
一旦主路由器不可用,這種選擇過程就提供了動態的故障轉移機制,這就容許虛擬路由器的IP地址能夠做爲終端主機的默認第一跳路由器。
keepalive經過組播,單播等方式(自定義),實現keepalive主備推選。工做模式分爲搶佔和非搶佔(經過參數nopreempt來控制)。
1)搶佔模式:
主服務正常工做時,虛擬IP會在主上,備不提供服務,當主服務優先級低於備的時候,備會自動搶佔虛擬IP,這時,主不提供服務,備提供服務。
也就是說,工做在搶佔模式下,不分主備,只管優先級。
如上配置,無論keepalived.conf裏的state配置成master仍是backup,只看誰的priority優先級高(通常而言,state爲MASTER的優先級要高於BACKUP)。
priority優先級高的那一個在故障恢復後,會自動將VIP資源再次搶佔回來!!
2)非搶佔模式:
這種方式經過參數nopreempt(通常設置在advert_int的那一行下面)來控制。無論priority優先級,只要MASTER機器發生故障,VIP資源就會被切換到BACKUP上。
而且當MASTER機器恢復後,也不會去將VIP資源搶佔回來,直至BACKUP機器發生故障時,才能自動切換回來。
千萬注意:
nopreempt這個參數只能用於state爲backup的狀況,因此在配置的時候要把master和backup的state都設置成backup,這樣纔會實現keepalived的非搶佔模式!
也就是說:
a)當state狀態一個爲master,一個爲backup的時候,加不加nopreempt這個參數都是同樣的效果。即都是根據priority優先級來決定誰搶佔vip資源的,是搶佔模式!
b)當state狀態都設置成backup,若是不配置nopreempt參數,那麼也是看priority優先級決定誰搶佔vip資源,即也是搶佔模式。
c)當state狀態都設置成backup,若是配置nopreempt參數,那麼就不會去考慮priority優先級了,是非搶佔模式!即只有vip當前所在機器發生故障,另外一臺機器才能接管vip。
即便優先級高的那一臺機器恢復 後也不會主動搶回vip,只能等到對方發生故障,纔會將vip切回來。
---------------------------------mysql狀態檢測腳本優化---------------------------------
案例一:
上面的mysql監測腳本有點過於簡單且粗暴,即腳本一旦監測到Master的mysql服務關閉,就馬上把keepalived服務關閉,從而實現vip轉移!
下面對該腳本進行優化,優化後,當監測到Master的mysql服務關閉後,就會將vip切換到Backup上(但此時Master的keepalived服務不會被暴力kill)
當Master的mysql服務恢復後,就會再次將VIP資源切回來!
[root@master ~]# cat /opt/chk_mysql.sh
#!/bin/bash
MYSQL=/usr/bin/mysql
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=root
CHECK_TIME=3
#mysql is working MYSQL_OK is 1 , mysql down MYSQL_OK is 0
MYSQL_OK=1
function check_mysql_helth (){
$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -e "show status;" >/dev/null 2>&1
if [ $? = 0 ] ;then
MYSQL_OK=1
else
MYSQL_OK=0
fi
return $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]
do
let "CHECK_TIME -= 1"
check_mysql_helth
if [ $MYSQL_OK = 1 ] ; then
CHECK_TIME=0
exit 0
fi
if [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ]
then
pkill keepalived
exit 1
fi
sleep 1
done
vim
案例二:centos
[root@master opt]# cat chk_mysql.sh
#!/bin/bash
MYSQL=/usr/bin/mysql
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=root
CHECK_TIME=3
#mysql is working MYSQL_OK is 1 , mysql down MYSQL_OK is 0
MYSQL_OK=1
VIP=192.168.0.102
function check_mysql_helth ()
{
$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -e "show status;" >/dev/null 2>&1
if [ $? -eq 0 ] ;then
MYSQL_OK=1
else
MYSQL_OK=0
fi
return $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]
do
let "CHECK_TIME -= 1"
check_mysql_helth
if [ $MYSQL_OK = 1 ] ; then
CHECK_TIME=0
exit 0
fi
ip a|grep $VIP
if [ $? -eq 0 ] && [ $CHECK_TIME -eq 0 ];then
mount /dev/mapper/mpathb /opt/diskarray
echo "vip is here,mysql is down"
service mysqll restart
if [ $? -ne 0 ];then
echo "vip is here,but mysql is not work !"
/etc/init.d/keepalived restart
umount /dev/mapper/mpathb
exit 1
fi
exit 3
elif [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ];then
echo " not vip , 2th check mysql is down "
exit 5
fi
sleep 2
done設計模式