您好,登錄后才能下訂單哦!
一環境介紹
master: 172.168.1.69
slave : 172.168.1.57
VIP 172.168.1.253 虛擬IP
二。設計思路:
當 Master 與 Slave 均運作正常時, Master負責服務,Slave負責Standby;
當 Master 掛掉,Slave 正時, Slave接管服務,同時關閉主從復制功能;
當 Master 恢復正常,則從Slave同步數據,同步數據之后關閉主從復制功能,恢復Master身份,于此同時Slave等待Master同步數據完成之后,恢復Slave身份。
然后依次循環。
需要注意的是,這樣做需要在Master與Slave上都開啟本地化策略,否則在互相自動切換的過程中,未開啟本地化的一方會將另一方的數據清空,造成數據完全丟失。(配置方式,打開redis的配置文件。找到appendonly。默認是appendonly no。改成appendonly yes。)
redis配置文件不可設置只讀模式
三。安裝前準備工作
1.在主服務器172.168.1.69 上面做下面操作
echo "172.168.1.69 test01" >> /etc/hosts
echo "172.168.1.57 test" >> /etc/hosts
2.在從服務器172.168.1.57上面做下面操作
echo "172.168.1.69 test01" >> /etc/hosts
echo "172.168.1.57 test" >> /etc/hosts
四:配置redis主從 (略)
端口號 :P :6379
密碼:123456
啟動服務:
# /mnt/tools/redis/bin/redis-server /mnt/tools/redis/etc/redis.conf
主服務器上執行:
#/mnt/tools/redis/bin/redis-cli -h 172.168.1.69 -a 123456 set test 123456
從服務器上執行:
#/mnt/tools/redis/bin/redis-cli -h 172.168.1.57 get test
測試是否已啟動
# /mnt/tools/redis/bin/redis-cli ping
性能測試
# /mnt/tools/redis/bin/redis-benchmark
關閉服務
#/mnt/tools/redis/bin/redis-cli -p 6379 shutdown
五:安裝配置keepalived (直接yum安裝也可以 方便 簡單粗暴 不用自己裝依賴包)
1.mkdir /root/tools -p
cd /root/tools
wget http://www.keepalived.org/software/keepalived-1.1.17.tar.gz
tar -zxf keepalived*
2.做一個內核連接
[root@zjx tools]# uname -r
3.10.0-123.9.3.el7.x86_64
[root@zjx tools]# ln -s /usr/src/kernels/3.10.0-123.9.3.el7.x86_64 /usr/src/linux
cd keepalived-1.1.17
./configure error(yum install -y openssl openssl-devel yum install popt-devel)
make && make install
移動啟動腳本和配置文件
[root@zjx /]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
[root@zjx /]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@zjx /]# mkdir /etc/keepalived 啟動腳本中默認指定的配置文件路徑
[root@zjx /]# /bin/cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
[root@zjx /]# /bin/cp /usr/local/sbin/keepalived /usr/sbin/
執行上述操作后就可以以 /etc/init.d/keepalived 的方式啟動了
/etc/init.d/keepalived start
/etc/init.d/keepalived stop
/etc/init.d/keepalived restart
3.master keepalived.cnf 配置文件
! Configuration File for keepalived
vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh"
interval 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
mcast_src_ip 172.168.1.253
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_redis
}
virtual_ipaddress {
172.168.1.253
}
notify_master /etc/keepalived/scripts/redis_master.sh
notify_backup /etc/keepalived/scripts/redis_backup.sh
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop /etc/keepalived/scripts/redis_stop.sh
}
slave:keepalived.cnf 配置文件
! Configuration File for keepalived
vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh"
interval 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
mcast_src_ip 172.168.1.253
priority 10
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_redis
}
virtual_ipaddress {
172.168.1.253
}
notify_master /etc/keepalived/scripts/redis_master.sh
notify_backup /etc/keepalived/scripts/redis_backup.sh
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop /etc/keepalived/scripts/redis_stop.sh
}
4.在Master和Slave上創建監控Redis的腳本
$ mkdir /etc/keepalived/scripts
$ vim /etc/keepalived/scripts/redis_check.sh
#!/bin/bash
ALIVE=`/mnt/tools/redis/bin/redis-cli -a 123456 PING`
if [ "$ALIVE" == "PONG" ]; then
echo $ALIVE
exit 0
else
echo $ALIVE
exit 1
fi
5.編寫以下負責運作的關鍵腳本:
notify_master /etc/keepalived/scripts/redis_master.sh
notify_backup /etc/keepalived/scripts/redis_backup.sh
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop /etc/keepalived/scripts/redis_stop.sh
因為Keepalived在轉換狀態時會依照狀態來呼叫:
當進入Master狀態時會呼叫notify_master
當進入Backup狀態時會呼叫notify_backup
當發現異常情況時進入Fault狀態呼叫notify_fault
當Keepalived程序終止時則呼叫notify_stop
1)首先,在Redis Master上創建notity_master與notify_backup腳本:
$ vim /etc/keepalived/scripts/redis_master.sh
#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli -a 123456"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF 172.168.1.57 6379 >> $LOGFILE 2>&1
sleep 10 #延遲10秒以后待數據同步完成后再取消同步狀態
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
$ vim /etc/keepalived/scripts/redis_backup.sh
#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli -a 123456"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >> $LOGFILE 2>&1
sleep 15 #延遲15秒待數據被對方同步完成之后再切換主從角色
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF 172.168.1.57 6379 >> $LOGFILE 2>&1
2)在Redis Slave上創建notity_master與notify_backup腳本:
$ vim /etc/keepalived/scripts/redis_master.sh
#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli -a 123456"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF 172.168.1.69 6379 >> $LOGFILE 2>&1
sleep 10 #延遲10秒以后待數據同步完成后再取消同步狀態
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
$ vim /etc/keepalived/scripts/redis_backup.sh
#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli -a 123456"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >> $LOGFILE 2>&1
sleep 15 #延遲15秒待數據被對方同步完成之后再切換主從角色
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF 172.168.1.69 6379 >> $LOGFILE 2>&1
3)然后在Master與Slave創建如下相同的腳本:
$ vim /etc/keepalived/scripts/redis_fault.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[fault]" >> $LOGFILE
date >> $LOGFILE
$ vim /etc/keepalived/scripts/redis_stop.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE
6.在主從服務器上面給腳本都加上可執行權限:
$ chmod +x /etc/keepalived/scripts/*.sh
7.進行相關功能測試
啟動Master和slave上的Redis
$ /mnt/tools/redis/bin/redis-server /mnt/tools/redis/etc/redis.conf
啟動Master和slave上的Keepalived
$
嘗試通過VIP連接Redis:
$ redis-cli -h 172.168.1.253 INFO
連接成功,Slave也連接上來了。
role:master
slave0:172.168.1.57,6379,online
嘗試插入一些數據:
$ redis-cli -h 172.168.1.69 SET Hello Redis
OK
從VIP讀取數據
$ redis-cli -h 172.168.1.253 GET Hello
"Redis"
從Master讀取數據
$ redis-cli -h 172.168.1.69 GET Hello
"Redis"
從Slave讀取數據
$ redis-cli -h 172.168.1.57 GET Hello
"Redis"
8.通過模擬相關故障,進行功能測試()
將Master上的Redis進程殺死:
killall -9 redis-server
查看Master上的Keepalived日志
查看Master上的Keepalived日志
$ tail -f /var/log/keepalived-redis-state.log
[fault]
Thu Sep 27 08:29:01 CST 2012
同時Slave上的日志顯示:
$ tail -f /var/log/keepalived-redis-state.log
[master]
Being master....
Run SLAVEOF cmd ...
OK Already connected to specified master
Run SLAVEOF NO ONE cmd ...
OK
然后我們可以發現,Slave已經接管服務,并且擔任Master的角色了。
$ redis-cli -h 172.168.1.253 INFO
$ redis-cli -h 172.168.1.57 INFO
role:master
然后我們恢復Master的Redis進程(恢復主redis后,需要重啟keepalived 和從庫的redis 可恢復正常)
$ /etc/init.d/redis start
查看Master上的Keepalived日志
$ tail -f /var/log/keepalived-redis-state.log
[master]
Being master....
Run SLAVEOF cmd ...
OK
Run SLAVEOF NO ONE cmd ...
OK
同時Slave上的日志顯示:
$ tail -f /var/log/keepalived-redis-state.log
[backup]
Being slave....
Run SLAVEOF cmd ...
OK
發現目前的Master已經再次恢復了Master的角色,故障切換以及自動恢復都成功了。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。