redis主備部署方案

五柳-先生發表於2015-05-08

redis主備部署方案

Redis部署方式採用主備的方式,通過keepalived來對外提供虛IP,並實現主備自動切換功能。

主例項A:192.168.20.30

備例項B:192.168.20.232

虛IP:192.168.20.110

正常工作時,虛IP在主例項A上,主例項A上的資料自動同步到備例項B上,當主例項A掛掉之後,備例項B將自動接管虛IP,並將redis轉換為主模式,待原主例項A恢復後,A將自動切換成備模式,從B上同步資料,主備角色互換,實現融災備份。

 

安裝部署步驟如下:

  1. 1.       安裝keepalived

wget http://www.keepalived.org/software/keepalived-1.2.6.tar.gz

tar zxvf keepalived-1.2.6.tar.gz

cd keepalived-1.2.6

./configure

如果報錯

configure: error:

        !!! OpenSSL is not properly installed on your system. !!!

  !!! Can not include OpenSSL headers files.

解決辦法:

yum -y install openssl-devel

yum -y install popt-devel

ln -s /usr/src/kernels/2.6.32-220.el6.x86_64/ /usr/src/linux

 

 

./configure

make

make install

 

cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

cp /usr/local/sbin/keepalived /usr/sbin/

mkdir /etc/keepalived

新增keepalived的配置項:

vi /etc/keepalived/keepalived.conf

global_defs {

   router_id LVS_DEVEL

}

vrrp_script Monitor_Redis {

 script "/home/bbcv/redis/redis_keepalive.sh"

 interval 3 #每3秒執行一次

 weight 2

}

# VIP1

vrrp_instance VI_1 {

    state BACKUP           #主備伺服器都設定成BACKUP

    interface eth0

    virtual_router_id 55

    priority 100     # 備份服務上將100改為90

    advert_int 1    #檢查間隔

    nopreempt     #設定為不搶佔,注意這個配置只能設定在state為BACKUP的主機上,而且這個主機的priority必須比另外一臺高

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    notify_master /home/bbcv/redis/redis_master.sh

    notify_backup /home/bbcv/redis/redis_backup.sh

    track_script {

        Monitor_Redis #(呼叫redis程式檢測指令碼)

    }

    virtual_ipaddress {

        192.168.20.110

        #(如果有多個VIP,繼續換行填寫.)

    }

}

啟動keepalived

service keepalived start

  1. 2.       redis_keepalive.sh指令碼

該指令碼主要實現對redis程式進行監控,當檢測到redis程式掛掉時,自動停止keepalived程式,使虛IP進行漂移

²  注意新增該指令碼的可執行許可權!

chmod +x redis_keepalive.sh

#!/bin/bash

time=$(date '+%Y-%m-%d %H:%M:%S' )

#redis部署路徑

redispath=/home/bbcv/redis

#redis-cli部署路徑

rediscli=$redispath/redis-2.0.0-rc4/redis-cli

if [ ! -d "logs" ]; then

        mkdir $redispath/logs

fi

logfile=$redispath/logs/redis-state.log

oldfile=$logfile'.'$(date +%Y-%m-%d --date='30 days ago')

yesterdayfile=$logfile'.'$(date +%Y-%m-%d --date='1 days ago')

#把昨天的日誌重新命名

if [ -f $yesterdayfile ]

then

        echo "OK"

else

        mv $logfile $yesterdayfile >> $logfile 2>&1

        echo "$time [$yesterdayfile] Move yesterdayfile Success!" >> $logfile

fi

#刪除老的日誌檔案

if [ -f $oldfile ]

then

        rm -f $oldfile >> $logfile 2>&1

        echo "$time [$oldfile] Delete Old File Success!" >> $logfile

else

        echo "no old file"

fi

#檢測redis埠是否正常

status=`$rediscli -h 127.0.0.1 -p 6379 info|grep role|awk -F ":" '{print $2}'`

$rediscli -h 127.0.0.1 -p 6379 info > /dev/null

if [ $? -eq 0 ]; then

        echo "$time redis server is OK" >>$logfile

        echo 目前狀態為:$status >>$logfile

else

        echo "$time no redis service found!" >>$logfile

        sleep 2

        # try to found it again

        $rediscli -h 127.0.0.1 -p 6379 info > /dev/null

        if [ $? -eq 0 ]; then

                echo "$time redis server is OK" >>$logfile

                echo 目前狀態為:$status >>$logfile

                #exit 0

        else

                echo "$time redis server error" >>$logfile

                #stop keepalived

                /etc/init.d/keepalived stop

                echo "$time stop keepalived" >>$logfile

        fi

fi

  1. 3.       redis_master.sh指令碼

該指令碼主要實現當keepalived切換到master時,執行slaveof no one語句把redis的埠轉換為主模式

²  注意新增該指令碼的可執行許可權!

chmod +x redis_master.sh

#!/bin/sh

#本機IP地址

localip=127.0.0.1

#另一臺伺服器的地址

backip=192.168.20.232

#redis埠號

port="6379"

 

time=$(date '+%Y-%m-%d %H:%M:%S' )

#redis部署路徑

redispath=/home/bbcv/redis

#redis-cli部署路徑

rediscli=$redispath/redis-2.0.0-rc4/redis-cli

#日誌檔案目錄

logfile=$redispath/logs/redis-state.log

for p in $port

do

{

        echo "$time redis $p埠切換成主埠" >>$logfile

        $rediscli -h $localip -p $p slaveof no one >>$logfile 2>&1

        sleep 1

}

done

  1. 4.       redis_backup.sh

該指令碼主要實現當keepalived切換到master時,執行slaveof 主redisIP 埠 語句,把redis的埠轉換為備模式,並同步主redis的資料

²  注意新增該指令碼的可執行許可權!

chmod +x redis_backup.sh

#!/bin/sh

#本機IP地址

localip=127.0.0.1

#另一臺伺服器的地址

backip=192.168.20.232

#redis埠號

port="6379"

 

time=$(date '+%Y-%m-%d %H:%M:%S' )

#redis部署路徑

redispath=/home/bbcv/redis

#redis-cli部署路徑

rediscli=$redispath/redis-2.0.0-rc4/redis-cli

#日誌檔案目錄

logfile=$redispath/logs/redis-state.log

for p in $port

do

{

        echo "$time redis $p埠切換成$backip的備埠" >>$logfile

        $rediscli -h $localip -p $p slaveof $backip $p  >>$logfile 2>&1

        sleep 1

}

done

  1. 5.       安裝redis

1.新建資料夾,如mkdir redis

2.將redis-2.2.13.tar.gz拷入其中

3.解壓並進入主目錄

4.make

5.make install(Ubuntu上需要sudo執行)

6.修改配置檔案redis.conf

修改如下:

daemonize no 改為 daemonize yes

取消註釋:

syslog-enabled no (改為syslog-enabled yes )

syslog-facility local0

已將修改後的redis.conf檔案放入附件,不同需求還需再修改配置檔案(如修改埠等)

7.在執行redis-server redis.conf啟動時,可能會報如下錯誤:

Can't chdir to '/var/db/redis': No such file or directory

需要手動建立'/var/db/redis'資料夾

# WARNING overcommit_memory is set to 0! Background save may fail under low memory condition

需要執行 sysctl vm.overcommit_memory=1 修改核心引數

8.再次執行redis-server redis.conf可成功啟動redis

  1. 6.       測試

1.檢視當前redis是主還是備

/home/bbcv/redis/redis-2.0.0-rc4/redis-cli -h 127.0.0.1 -p 6379 info

 

2.在主redisA上新增一條資料,檢視備redisB是否同步該資料

/home/bbcv/redis/redis-2.0.0-rc4/redis-cli -h 127.0.0.1 -p 6379

 

3.停止主redisA,檢視虛IP是否切換到備redisB伺服器,並檢視備redisB伺服器是否切換成主狀態

ip a檢視虛IP是否在本機上

 

4.恢復redisA,檢視redisA是否從redisB中同步資料

來源: http://www.cnblogs.com/aspnethot/articles/3468823.html

相關文章