關(guān)于mysql主從配置請(qǐng)查看文章:http://www.reibang.com/p/86eac0db552b。
本篇將的是如何用keepalived保證mysql主從的自動(dòng)切換皮仁。
keepalive(mysql) vip:192.168.1.66
keepalive(mysql) master:192.168.1.60
keepalive(mysql) slave:192.168.1.61
1掀序、keepalived 節(jié)點(diǎn)配置
global_defs:主要是配置故障發(fā)生時(shí)的通知對(duì)象以及機(jī)器標(biāo)識(shí)璧疗。
vrrp_instance:用來(lái)定義對(duì)外提供服務(wù)的VIP區(qū)域及其相關(guān)屬性肴甸。
virtual_server:虛擬服務(wù)器定義
1.1相嵌、master節(jié)點(diǎn)配置如下:
! Configuration File for keepalived
global_defs {
router_id MYSQL-1 //表示運(yùn)行keepalived服務(wù)器的一個(gè)標(biāo)識(shí)
}
// 定義自動(dòng)核對(duì)mysql的腳本
vrrp_script check_mysql {
script "/etc/keepalived/bin/check_mysql.sh"
interval 22
weight 2
}
vrrp_instance VI_1 {
state BACKUP //指定keepalived的角色, 兩臺(tái)配置此處均是BACKUP,設(shè)為BACKUP將根據(jù)優(yōu)先級(jí)決定主或從
interface eth1 //指定HA監(jiān)測(cè)網(wǎng)絡(luò)的接口
virtual_router_id 51 /虛擬路由標(biāo)識(shí)屎暇,這個(gè)標(biāo)識(shí)是一個(gè)數(shù)字(取值在0-255之間,用來(lái)區(qū)分多個(gè)instance的VRRP組播)承桥,同一個(gè)vrrp實(shí)例使用唯一的標(biāo)識(shí),確保和master2相同,同網(wǎng)內(nèi)不同集群此項(xiàng)必須不同,否則發(fā)生沖突恭垦。
priority 100 //用來(lái)選舉master的快毛,要成為master格嗅,該項(xiàng)取值范圍是1-255(在此范圍之外會(huì)被識(shí)別成默認(rèn)值100),此處master2上設(shè)置為50
advert_int 1 //發(fā)VRRP包的時(shí)間間隔,即多久進(jìn)行一次master選舉(可以認(rèn)為是健康查檢時(shí)間間隔)
nopreempt //不搶占唠帝,即允許一個(gè)priority比較低的節(jié)點(diǎn)作為master屯掖,即使有priority更高的節(jié)點(diǎn)啟動(dòng)
authentication {
//認(rèn)證區(qū)域,認(rèn)證類(lèi)型有PASS和HA(IPSEC)襟衰,推薦使用PASS(密碼只識(shí)別前8位)
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
// 指定vip地址
192.168.1.66
}
track_script {
//指定核對(duì)的腳本贴铜,check_mysql是上述自定義的
check_mysql
}
}
virtual_server 192.168.1.66 3306 {
delay_loop 2 //設(shè)置運(yùn)行情況檢查時(shí)間,單位是秒
lb_algo rr //設(shè)置后端調(diào)度算法瀑晒,這里設(shè)置為rr绍坝,即輪詢算法
lb_kind DR //設(shè)置LVS實(shí)現(xiàn)負(fù)載均衡的機(jī)制,有NAT苔悦、TUN轩褐、DR三個(gè)模式可選
persistence_timeout 50 //會(huì)話保持時(shí)間,單位是秒玖详。這個(gè)選項(xiàng)對(duì)動(dòng)態(tài)網(wǎng)頁(yè)是非常有用的把介,為集群系統(tǒng)中的session共享提供了一個(gè)很好的解決方案。有了這個(gè)會(huì)話保持功能蟋座,用戶的請(qǐng)求會(huì)被一直分發(fā)到某個(gè)服務(wù)節(jié)點(diǎn)拗踢,直到超過(guò)這個(gè)會(huì)話的保持時(shí)間。
protocol TCP //指定轉(zhuǎn)發(fā)協(xié)議類(lèi)型向臀,有TCP和UDP兩種
real_server 192.168.1.60 3306 {
weight 3 //配置服務(wù)節(jié)點(diǎn)的權(quán)值巢墅,權(quán)值大小用數(shù)字表示,數(shù)字越大券膀,權(quán)值越高君纫,設(shè)置權(quán)值大小為了區(qū)分不同性能的服務(wù)器
notify_stop /etc/keepalived/bin/shutdown.sh //檢測(cè)到mysql服務(wù)down后執(zhí)行的腳本,在一些版本中叫notify_down
TCP_CHECK {
connect_timeout 3 //連接超時(shí)時(shí)間
nb_get_retry 3 //重連次數(shù)
delay_before_retry 3 //重連間隔時(shí)間
connect_port 3306 //健康檢查端口
}
}
}
沒(méi)有注釋的代碼如下:
! Configuration File for keepalived
global_defs {
router_id MYSQL-1
}
vrrp_script check_mysql {
script "/etc/keepalived/bin/check_mysql.sh"
interval 22
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id 51
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.66
}
track_script {
check_mysql
}
}
virtual_server 192.168.1.66 3306 {
delay_loop 2
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.1.60 3306 {
weight 3
notify_stop /etc/keepalived/bin/shutdown.sh
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
上述是mysql的master節(jié)點(diǎn)的配置三娩,slave的配置只需修改如下三個(gè)參數(shù):
global_defs 的router_id 改為MYSQL-2
vrrp_instance VI_1的priority 改為50庵芭;
virtual_server 192.168.1.66 3306的real_server 192.168.1.60 3306改為192.168.1.61 3306;
1.2雀监、slave節(jié)點(diǎn)配置如下:
! Configuration File for keepalived
global_defs {
router_id MYSQL-2
}
vrrp_script check_mysql {
script "/etc/keepalived/bin/check_mysql.sh"
interval 22
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id 51
priority 50
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.66
}
track_script {
check_mysql
}
}
virtual_server 192.168.1.66 3306 {
delay_loop 2
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.1.61 3306 {
weight 3
notify_stop /etc/keepalived/bin/shutdown.sh
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
1.3 check_mysql.sh和shutdown.sh文件
check_mysql.sh
#!/bin/bash
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=Dogoicq@Sec
mysql -h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASSWORD -e "show status;" >/dev/null 2>&1
if [ $? == 0 ]
then
echo " $host mysql login successfully "
exit 0
else
service keepalived stop
exit 2
fi
shutdown.sh
#!/bin/bash
service keepalived stop
2双吆、啟動(dòng)keepalive
命令:service keepalived start
啟動(dòng)后檢查本地地址是否啟動(dòng)成功:ip addr
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 6c:92:bf:5d:04:d3 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.60/26 brd 192.168.1.63 scope global eth1
valid_lft forever preferred_lft forever
inet 192.168.1.66/32 scope global eth1
valid_lft forever preferred_lft forever
看到inet 192.168.1.66/32 即表明keepalive啟動(dòng)成功晰搀。
啟動(dòng)過(guò)程中遇到的問(wèn)題:
1)剛開(kāi)始配置的是eth0控淡,啟動(dòng)后沒(méi)有查看到192.168.1.66/32,查看日志/var/log/messages發(fā)現(xiàn):
Keepalived_vrrp[16653]: Kernel is reporting: interface eth0 DOWN
查看ip addr發(fā)現(xiàn)绰沥,eth0是關(guān)閉的:
2: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN qlen 1000
指定網(wǎng)卡為eht1:
vrrp_instance VI_1 {
interface eth1
}
3瓦宜、遠(yuǎn)程登錄mysql
在遠(yuǎn)程登錄之前蔚万,確保mysql master和slave節(jié)點(diǎn)有被遠(yuǎn)程訪問(wèn)的權(quán)限:
grant all PRIVILEGES on *.* to 'root'@'192.168.1.59' identified by 'pass123';
在192.168.1.59機(jī)器上登錄192.168.1.66的mysql:
mysql -uroot -ppass123 -h 192.168.1.66
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.66' (113)
連接不上vip的mysql,檢查發(fā)現(xiàn)是因?yàn)?92.168.1.60和192.168.1.61防火墻的3306端口沒(méi)看临庇,開(kāi)啟3306端口:
iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
service iptables save
service iptables restart
再次嘗試反璃,即可連接昵慌。
4、測(cè)試keeplaived+mysql
通過(guò)vip登錄后淮蜈,查看當(dāng)前連接:
mysql> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 1 |
+---------------+-------+
1 row in set (0.01 sec)
server_id為1是master節(jié)點(diǎn)斋攀, server_id為2是slave節(jié)點(diǎn)。
vip mysql插入一條數(shù)據(jù)梧田,查看slave節(jié)點(diǎn)數(shù)據(jù)庫(kù):
vip:
insert into runoob_tbl(runoob_title) values("adad");
slave:
MySQL [sysctest]> select * from runoob_tbl;
+-----------+----------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+----------------+---------------+-----------------+
| 1 | al | bb | 2018-03-08 |
| 2 | ap | bb | 2018-03-08 |
| 3 | aq | bb | 2018-03-08 |
| 4 | aaaaaaaaaaaa | bb | 2018-03-08 |
| 5 | aaaaaaaaadddaa | bb | 2018-03-08 |
| 6 | aaaaaaaeeeddaa | bb | 2018-03-08 |
| 7 | aaac | bb | 2018-03-09 |
| 8 | adad | | NULL
發(fā)現(xiàn)可正常插入數(shù)據(jù)淳蔼。
測(cè)試master節(jié)點(diǎn)掛點(diǎn)后,vip能否自動(dòng)切換:
master:
service mysqld stop
vip:
mysql> show variables like 'server_id';
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 8
Current database: *** NONE ***
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 2 |
+---------------+-------+
1 row in set (0.03 sec)
mysql> select * from runoob_tbl;
+-----------+----------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+----------------+---------------+-----------------+
| 1 | al | bb | 2018-03-08 |
| 2 | ap | bb | 2018-03-08 |
| 3 | aq | bb | 2018-03-08 |
| 4 | aaaaaaaaaaaa | bb | 2018-03-08 |
| 5 | aaaaaaaaadddaa | bb | 2018-03-08 |
| 6 | aaaaaaaeeeddaa | bb | 2018-03-08 |
| 7 | aaac | bb | 2018-03-09 |
| 8 | adad | | NULL |
+-----------+----------------+---------------+-----------------+
發(fā)現(xiàn)vip正常切換到slave節(jié)點(diǎn)裁眯。同時(shí)shutdown腳本被調(diào)用鹉梨,本地的keepalived進(jìn)程被殺死。
ps -aux | grep keepa
root 39672 0.0 0.0 115764 1000 pts/0 S+ 14:56 0:00 grep --color=auto keepalived
重啟mysql進(jìn)程后穿稳,記得手動(dòng)重啟keepalived程序存皂。
防火墻問(wèn)題
在一個(gè)時(shí)間vip只指向一個(gè)真實(shí)的IP。因此在ip addr的時(shí)候司草,只有一臺(tái)機(jī)器可以看到vip艰垂。如下所示:
inet 192.168.1.60/26 brd 192.168.1.191 scope global eth1
valid_lft forever preferred_lft forever
inet 192.168.1.66/32 scope global eth1
valid_lft forever preferred_lft forever
網(wǎng)上有說(shuō)通過(guò)配置防火墻支持vrrp協(xié)議。
iptables -A INPUT -d 224.0.0.18 -j ACCEPT
最好不要這么設(shè)置埋虹,因?yàn)樵O(shè)置完后,會(huì)出現(xiàn)機(jī)器reject所有的請(qǐng)求娩怎,無(wú)法ssh搔课,scp等。