Redis+Keepalived主從熱備秒級切換

獵手家園發表於2016-05-01

當 Master 與 Slave 均運作正常時, Master負責服務,Slave負責Standby; 

當 Master 掛掉,Slave 正常時, Slave接管服務,同時關閉主從複製功能; 

當 Master 恢復正常,則從Slave同步資料,同步資料之後關閉主從複製功能,恢復Master身份,於此同時Slave等待Master同步資料完成之後,恢復Slave身份。 

然後依次迴圈……

需要注意的是,這樣做需要在Master與Slave上都開啟本地化策略,否則在互相自動切換的過程中,未開啟本地化的一方會將另一方的資料清空,造成資料完全丟失。

 

Master 10.10.51.241

Slave   10.10.51.242

vip       10.10.51.240

 

master配置:

mkdir –p /etc/keepalived
vi /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.10.51.240                       #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 
}
vi /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.10.51.242(slave) 6379 >> $LOGFILE  2>&1
sleep 10  #延遲10秒以後待資料同步完成後再取消同步狀態
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
vi /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.10.51.242(slave) 6379 >> $LOGFILE  2>&1

slave配置

vi /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.10.51.240                           #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 
}
vi /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.10.51.241(master) 6379 >> $LOGFILE  2>&1
sleep 10 #延遲10秒以後待資料同步完成後再取消同步狀態
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
vi /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.10.51.241(master) 6379 >> $LOGFILE  2>&1

以下指令碼分別在master和slave上建立:

mkdir /etc/keepalived/scripts
vi /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
vi /etc/keepalived/scripts/redis_fault.sh

#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[fault]" >> $LOGFILE
date >> $LOGFILE
vi /etc/keepalived/scripts/redis_stop.sh

#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE
chmod +x /etc/keepalived/scripts/*.sh    #授權

相關文章