為什麼很多人用keepalived來實現redis容錯移轉

來源:互聯網
上載者:User

標籤:

目前,Redis還沒有一個類似於MySQL Proxy或Oracle RAC的官方HA方案。
Redis作者有一個名為Redis 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
127.0.0.1localhost
10.6.1.143redis
10.6.1.144redis-slave

預設安裝完成keepalived之後是沒有設定檔的,因此我們需要手動建立:
首先,在Master上建立如下設定檔:
$ sudo vim /etc/keepalived/keepalived.conf
vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh" ###監控指令碼
interval 2 ###監控時間
}
vrrp_instance VI_1 {
state MASTER ###設定為MASTER
interface eth0 ###監控網卡
virtual_router_id 51
priority 101 ###權重值
authentication {
auth_type PASS ###加密
auth_pass redis ###密碼
}
track_script {
chk_redis ###執行上面定義的chk_redis
}
virtual_ipaddress {
10.6.1.200 ###VIP
}
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上建立如下設定檔:
$ sudo vim /etc/keepalived/keepalived.conf
vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh" ###監控指令碼
interval 2 ###監控時間
}
vrrp_instance VI_1 {
state BACKUP ###設定為BACKUP
interface eth0 ###監控網卡
virtual_router_id 51
priority 100 ###比MASTRE權重值低
authentication {
auth_type PASS
auth_pass redis ###密碼與MASTRE相同
}
track_script {
chk_redis ###執行上面定義的chk_redis
}
virtual_ipaddress {
10.6.1.200 ###VIP
}
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
}

在Master和Slave上建立監控Redis的指令碼
$ sudo mkdir /etc/keepalived/scripts
$ sudo vim /etc/keepalived/scripts/redis_check.sh
#!/bin/bash

ALIVE=`/opt/redis/bin/redis-cli PING`
if [ "$ALIVE" == "PONG" ]; then
echo $ALIVE
exit 0
else
echo $ALIVE
exit 1
fi

編寫以下負責運作的關鍵指令碼:
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
#!/bin/bash

REDISCLI="/opt/redis/bin/redis-cli"
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 10.6.1.144 6379 >> $LOGFILE 2>&1
sleep 10 #延遲10秒以後待資料同步完成後再取消同步狀態

echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1

$ sudo vim /etc/keepalived/scripts/redis_backup.sh
#!/bin/bash

REDISCLI="/opt/redis/bin/redis-cli"
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 10.6.1.144 6379 >> $LOGFILE 2>&1

接著,在Redis Slave上建立notity_master與notify_backup指令碼:
$ sudo vim /etc/keepalived/scripts/redis_master.sh
#!/bin/bash

REDISCLI="/opt/redis/bin/redis-cli"
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 10.6.1.143 6379 >> $LOGFILE 2>&1
sleep 10 #延遲10秒以後待資料同步完成後再取消同步狀態

echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1

$ sudo vim /etc/keepalived/scripts/redis_backup.sh
#!/bin/bash

REDISCLI="/opt/redis/bin/redis-cli"
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 10.6.1.143 6379 >> $LOGFILE 2>&1

然後在Master與Slave建立如下相同的指令碼:
$ sudo vim /etc/keepalived/scripts/redis_fault.sh
#!/bin/bash

LOGFILE=/var/log/keepalived-redis-state.log

echo "[fault]" >> $LOGFILE
date >> $LOGFILE

$ sudo vim /etc/keepalived/scripts/redis_stop.sh
#!/bin/bash

LOGFILE=/var/log/keepalived-redis-state.log

echo "[stop]" >> $LOGFILE
date >> $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的角色,故障切換以及自動回復都成功了。
轉載

為什麼很多人用keepalived來實現redis容錯移轉

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.