參考資料:
http://patrick-tang.blogspot.com/2012/06/redis-keepalived-failover-system.html
http://deidara.blog.51cto.com/400447/302402
背景介紹:
目前,Redis還沒有一個類似於MySQL Proxy或Oracle RAC的官方HA方案。
Redis作者有一個名為Redis Sentinel的計劃(http://redis.io/topics/sentinel),據稱將會有監控,報警和自動故障轉移三大功能,非常不錯。
但可惜的是短期內恐怕還不能開發完成。
因此,如何在出現故障時自動轉移是一個需要解決的問題。
通過對網上一些資料的搜尋,有建議採用HAProxy或Keepalived來實現的,事實上如果是做Failover而非負載均衡的話,Keepalived的效率肯定是超過HAProxy的,所以我決定採用Keepalived的方案。
環境介紹:
Master: 10.6.1.143
Slave: 10.6.1.144
Virtural IP Address (VIP): 10.6.1.200
設計思路:
當 Master 與 Slave 均運作正常時, Master負責服務,Slave負責Standby;
當 Master 掛掉,Slave 正常時, Slave接管服務,同時關閉主從複製功能;
當 Master 恢復正常,則從Slave同步資料,同步資料之後關閉主從複製功能,恢復Master身份,於此同時Slave等待Master同步資料完成之後,恢復Slave身份。
然後依次迴圈。
需要注意的是,這樣做需要在Master與Slave上都開啟本地化策略,否則在互相自動切換的過程中,未開啟本地化的一方會將另一方的資料清空,造成資料完全丟失。
下面,是具體的實施步驟:
在Master和Slave上安裝Keepalived
$ sudo apt-get install keepalived
修改Master和Slave的/etc/hosts檔案
$ sudo vim /etc/hosts
預設安裝完成keepalived之後是沒有配置檔案的,因此我們需要手動建立:
首先,在Master上建立如下配置檔案:
$ sudo vim /etc/keepalived/keepalived.conf
01 |
vrrp_script
chk_redis { |
02 |
script "/etc/keepalived/scripts/redis_check.sh" |
20 |
notify_master
/etc/keepalived/scripts/redis_master.sh |
21 |
notify_backup
/etc/keepalived/scripts/redis_backup.sh |
22 |
notify_fault
/etc/keepalived/scripts/redis_fault.sh |
23 |
notify_stop
/etc/keepalived/scripts/redis_stop.sh |
然後,在Slave上建立如下配置檔案:
$ sudo vim /etc/keepalived/keepalived.conf
01 |
vrrp_script
chk_redis { |
02 |
script "/etc/keepalived/scripts/redis_check.sh" |
20 |
notify_master
/etc/keepalived/scripts/redis_master.sh |
21 |
notify_backup
/etc/keepalived/scripts/redis_backup.sh |
22 |
notify_fault
/etc/keepalived/scripts/redis_fault.sh |
23 |
notify_stop
/etc/keepalived/scripts/redis_stop.sh |
在Master和Slave上建立監控Redis的指令碼
$ sudo mkdir /etc/keepalived/scripts
$ sudo vim /etc/keepalived/scripts/redis_check.sh
03 |
ALIVE=`/opt/redis/bin/redis-cli
PING` |
04 |
if [ "$ALIVE" == "PONG" ]; then |
編寫以下負責運作的關鍵指令碼:
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
首先,在Redis Master上建立notity_master與notify_backup指令碼:
$ sudo vim /etc/keepalived/scripts/redis_master.sh
03 |
REDISCLI= "/opt/redis/bin/redis-cli" |
04 |
LOGFILE= "/var/log/keepalived-redis-state.log" |
06 |
echo "[master]" >>
$LOGFILE |
08 |
echo "Being
master...." >>
$LOGFILE 2>&1 |
10 |
echo "Run
SLAVEOF cmd ..." >>
$LOGFILE |
11 |
$REDISCLI
SLAVEOF 10.6.1.144 6379 >> $LOGFILE 2>&1 |
14 |
echo "Run
SLAVEOF NO ONE cmd ..." >>
$LOGFILE |
15 |
$REDISCLI
SLAVEOF NO ONE >> $LOGFILE 2>&1 |
$ sudo vim /etc/keepalived/scripts/redis_backup.sh
03 |
REDISCLI= "/opt/redis/bin/redis-cli" |
04 |
LOGFILE= "/var/log/keepalived-redis-state.log" |
06 |
echo "[backup]" >>
$LOGFILE |
08 |
echo "Being
slave...." >>
$LOGFILE 2>&1 |
11 |
echo "Run
SLAVEOF cmd ..." >>
$LOGFILE |
12 |
$REDISCLI
SLAVEOF 10.6.1.144 6379 >> $LOGFILE 2>&1 |
接著,在Redis Slave上建立notity_master與notify_backup指令碼:
$ sudo vim /etc/keepalived/scripts/redis_master.sh
03 |
REDISCLI= "/opt/redis/bin/redis-cli" |
04 |
LOGFILE= "/var/log/keepalived-redis-state.log" |
06 |
echo "[master]" >>
$LOGFILE |
08 |
echo "Being
master...." >>
$LOGFILE 2>&1 |
10 |
echo "Run
SLAVEOF cmd ..." >>
$LOGFILE |
11 |
$REDISCLI
SLAVEOF 10.6.1.143 6379 >> $LOGFILE 2>&1 |
14 |
echo "Run
SLAVEOF NO ONE cmd ..." >>
$LOGFILE |
15 |
$REDISCLI
SLAVEOF NO ONE >> $LOGFILE 2>&1 |
$ sudo vim /etc/keepalived/scripts/redis_backup.sh
03 |
REDISCLI= "/opt/redis/bin/redis-cli" |
04 |
LOGFILE= "/var/log/keepalived-redis-state.log" |
06 |
echo "[backup]" >>
$LOGFILE |
08 |
echo "Being
slave...." >>
$LOGFILE 2>&1 |
11 |
echo "Run
SLAVEOF cmd ..." >>
$LOGFILE |
12 |
$REDISCLI
SLAVEOF 10.6.1.143 6379 >> $LOGFILE 2>&1 |
然後在Master與Slave建立如下相同的指令碼:
$ sudo vim /etc/keepalived/scripts/redis_fault.sh
3 |
LOGFILE=/var/log/keepalived-redis-state.log |
5 |
echo "[fault]" >>
$LOGFILE |
$ sudo vim /etc/keepalived/scripts/redis_stop.sh
3 |
LOGFILE=/var/log/keepalived-redis-state.log |
5 |
echo "[stop]" >>
$LOGFILE |
給指令碼都加上可執行許可權:
$ sudo chmod +x /etc/keepalived/scripts/*.sh
指令碼建立完成以後,我們開始按照如下流程進行測試:
1.啟動Master上的Redis
$ sudo /etc/init.d/redis start
2.啟動Slave上的Redis
$ sudo /etc/init.d/redis start
3.啟動Master上的Keepalived
$ sudo /etc/init.d/keepalived start
4.啟動Slave上的Keepalived
$ sudo /etc/init.d/keepalived start
5.嘗試通過VIP連線Redis:
$ redis-cli -h 10.6.1.200 INFO
連線成功,Slave也連線上來了。
role:master
slave0:10.6.1.144,6379,online
6.嘗試插入一些資料:
$ redis-cli -h 10.6.1.200 SET Hello Redis
OK
從VIP讀取資料
$ redis-cli -h 10.6.1.200 GET Hello
"Redis"
從Master讀取資料
$ redis-cli -h 10.6.1.143 GET Hello
"Redis"
從Slave讀取資料
$ redis-cli -h 10.6.1.144 GET Hello
"Redis"
下面,模擬故障產生:
將Master上的Redis程式殺死:
$ sudo killall -9 redis-server
檢視Master上的Keepalived日誌
$ tailf /var/log/keepalived-redis-state.log
[fault]
Thu Sep 27 08:29:01 CST 2012
同時Slave上的日誌顯示:
$ tailf /var/log/keepalived-redis-state.log
[master]
Fri Sep 28 14:14:09 CST 2012
Being master....
Run SLAVEOF cmd ...
OK
Run SLAVEOF NO ONE cmd ...
OK
然後我們可以發現,Slave已經接管服務,並且擔任Master的角色了。
$ redis-cli -h 10.6.1.200 INFO
$ redis-cli -h 10.6.1.144 INFO
role:master
然後我們恢復Master的Redis程式
$ sudo /etc/init.d/redis start
檢視Master上的Keepalived日誌
$ tailf /var/log/keepalived-redis-state.log
[master]
Thu Sep 27 08:31:33 CST 2012
Being master....
Run SLAVEOF cmd ...
OK
Run SLAVEOF NO ONE cmd ...
OK
同時Slave上的日誌顯示:
$ tailf /var/log/keepalived-redis-state.log
[backup]
Fri Sep 28 14:16:37 CST 2012
Being slave....
Run SLAVEOF cmd ...
OK
可以發現目前的Master已經再次恢復了Master的角色,故障切換以及自動恢復都成功了。
轉載: http://heylinux.com/archives/1942.html