10.1.1.174:23316 M1
10.1.1.237:23316 M2
[root@master1 ~]# cd /usr/local/src/
[root@master1 src]# wget https://www.keepalived.org/software/keepalived-2.0.17.tar.gz
[root@master1 src]# tar -xf keepalived-2.0.17.tar.gz
[root@master1 src]# cd keepalived-2.0.17
[root@master1 keepalived-2.0.17]# yum install openssl* libnl‐dev* gcc-c++
[root@master1 keepalived-2.0.17]# ./configure --prefix=/usr/local/keepalived
[root@master1 keepalived-2.0.17]# make
[root@master1 keepalived-2.0.17]# make install
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
echo "/etc/init.d/keepalived start" >> /etc/rc.local
mkdir -p /etc/keepalived/
mv /etc/keepalived/keepalived.conf /root/keepalived.conf.bak
M1:keepalive配置文件:
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vrrp_script chk_mysql_port { #檢測mysql服務(wù)是否在運(yùn)行。有很多方式,比如進(jìn)程,用腳本檢測等等
script "/etc/keepalived/chk_mysql.sh" #這里通過腳本監(jiān)測
interval 2 #腳本執(zhí)行間隔,每2s檢測一次
weight -5 #腳本結(jié)果導(dǎo)致的優(yōu)先級變更,檢測失敗(腳本返回非0)則優(yōu)先級 -5
fall 2 #檢測連續(xù)2次失敗才算確定是真失敗。會用weight減少優(yōu)先級(1-255之間)
rise 1 #檢測1次成功就算成功。但不修改優(yōu)先級
}
vrrp_instance VI_1 {
state MASTER
interface ens192 #指定虛擬ip的網(wǎng)卡接口,改成你自己的 eth0或者其它什么的
# mcast_src_ip 10.1.1.174
virtual_router_id 51 #路由器標(biāo)識,MASTER和BACKUP必須是一致的
priority 101 #定義優(yōu)先級,數(shù)字越大,優(yōu)先級越高,在同一個(gè)vrrp_instance下,MASTER的優(yōu)先級必須大于BACKUP的優(yōu)先級。這樣MASTER故障恢復(fù)后,就可以將VIP資源再次搶回來
unicast_src_ip 10.1.1.174 ##(本地IP地址)
unicast_peer {
10.1.1.237 ##(對端IP地址)此地址一定不能忘記
}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.1.1.111
}
track_script {
chk_mysql_port
}
}
[root@crm-db01 ~]# cat /etc/keepalived/chk_mysql.sh #####注意可執(zhí)行權(quán)限
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "23316"|wc -l)
if [ "${counter}" -eq 0 ]; then
/etc/init.d/keepalived stop
fi
M2:keepalived配置文件:
[root@#localhost keepalived]# cat keepalived.conf
! Configuration File for keepalived
vrrp_script chk_mysql_port {
script "/etc/keepalived/chk_mysql.sh"
interval 2
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state BACKUP
interface ens192 #指定虛擬ip的網(wǎng)卡接口,改成你自己的 eth0或者其它什么的
#mcast_src_ip 10.1.1.237
virtual_router_id 51
priority 90
unicast_src_ip 10.1.1.237 ##(本地IP地址)
unicast_peer {
10.1.1.174 ##(對端IP地址)此地址一定不能忘記
}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.1.1.111
}
track_script {
chk_mysql_port
}
}
[root@#localhost keepalived]# cat chk_mysql.sh #####注意可執(zhí)行權(quán)限
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "23316"|wc -l)
if [ "${counter}" -eq 0 ]; then
/etc/init.d/keepalived stop
fi
啟動keepalived服務(wù)
[root@master1 ~]# /etc/init.d/keepalived start
常用命令:
/etc/init.d/keepalived restart|start|stop
/etc/init.d/mysqld restart|start|stop
查看keepalive狀態(tài):
systemctl status keepalived.service
查看日志:
tail -f /var/log/messages
1.啟動兩個(gè)keepalived ,兩邊都有VIP。
我這個(gè)配置文件已經(jīng)修改過,應(yīng)該不會出現(xiàn)這個(gè)情況。
2.VIP起來后,無法通過VIP訪問MySQL數(shù)據(jù)庫。
關(guān)閉防火墻或者開放相關(guān)端口。
查看你的MySQL配置文件,看看有沒有這個(gè)參數(shù)#bind-address = 10.1.1.237,有的話注釋掉,重啟數(shù)據(jù)庫。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。