溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

redis主從+keepalived 高可用 自動切換

發(fā)布時間:2020-07-29 23:13:49 來源:網(wǎng)絡(luò) 閱讀:1318 作者:DB檸檬茶 欄目:關(guān)系型數(shù)據(jù)庫

一環(huán)境介紹

 master:  172.168.1.69

slave  : 172.168.1.57      

VIP     172.168.1.253   虛擬IP

 

二。設(shè)計思路:

 

當(dāng) Master 與 Slave 均運作正常時, Master負責(zé)服務(wù),Slave負責(zé)Standby;

 

當(dāng) Master 掛掉,Slave 正時, Slave接管服務(wù),同時關(guān)閉主從復(fù)制功能;

 

當(dāng) Master 恢復(fù)正常,則從Slave同步數(shù)據(jù),同步數(shù)據(jù)之后關(guān)閉主從復(fù)制功能,恢復(fù)Master身份,于此同時Slave等待Master同步數(shù)據(jù)完成之后,恢復(fù)Slave身份。

 

然后依次循環(huán)。

 

需要注意的是,這樣做需要在Master與Slave上都開啟本地化策略,否則在互相自動切換的過程中,未開啟本地化的一方會將另一方的數(shù)據(jù)清空,造成數(shù)據(jù)完全丟失。(配置方式,打開redis的配置文件。找到appendonly。默認(rèn)是appendonly no。改成appendonly yes。)

 redis配置文件不可設(shè)置只讀模式

 

 

三。安裝前準(zhǔn)備工作

 

1.在主服務(wù)器172.168.1.69 上面做下面操作

 

echo "172.168.1.69  test01" >> /etc/hosts

 

echo "172.168.1.57 test" >> /etc/hosts

 

2.在從服務(wù)器172.168.1.57上面做下面操作

 

echo "172.168.1.69  test01" >> /etc/hosts

 

echo "172.168.1.57 test" >> /etc/hosts

 

四:配置redis主從 (略)

 

端口號 P :6379

密碼:123456

 

 啟動服務(wù):

# /mnt/tools/redis/bin/redis-server /mnt/tools/redis/etc/redis.conf

 

 

主服務(wù)器上執(zhí)行:

#/mnt/tools/redis/bin/redis-cli -h 172.168.1.69 -a 123456 set test 123456

 

 

從服務(wù)器上執(zhí)行:

#/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

 

 

關(guān)閉服務(wù)

#/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.做一個內(nèi)核連接

[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   啟動腳本中默認(rèn)指定的配置文件路徑

[root@zjx /]# /bin/cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

[root@zjx /]# /bin/cp /usr/local/sbin/keepalived  /usr/sbin/

執(zhí)行上述操作后就可以以 /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上創(chuàng)建監(jiān)控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.編寫以下負責(zé)運作的關(guān)鍵腳本:

 

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在轉(zhuǎn)換狀態(tài)時會依照狀態(tài)來呼叫:

 

當(dāng)進入Master狀態(tài)時會呼叫notify_master

 

當(dāng)進入Backup狀態(tài)時會呼叫notify_backup

 

當(dāng)發(fā)現(xiàn)異常情況時進入Fault狀態(tài)呼叫notify_fault

 

當(dāng)Keepalived程序終止時則呼叫notify_stop

 

1)首先,在Redis Master上創(chuàng)建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秒以后待數(shù)據(jù)同步完成后再取消同步狀態(tài)

 

 

 

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秒待數(shù)據(jù)被對方同步完成之后再切換主從角色

 

echo "Run SLAVEOF cmd ..." >> $LOGFILE

 

$REDISCLI SLAVEOF 172.168.1.57 6379 >> $LOGFILE  2>&1

 

 

2)在Redis Slave上創(chuàng)建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秒以后待數(shù)據(jù)同步完成后再取消同步狀態(tài)

 

 

 

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秒待數(shù)據(jù)被對方同步完成之后再切換主從角色

 

echo "Run SLAVEOF cmd ..." >> $LOGFILE

 

$REDISCLI SLAVEOF 172.168.1.69 6379 >> $LOGFILE  2>&1

 

 

 

3)然后在Master與Slave創(chuàng)建如下相同的腳本:

 

$  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.在主從服務(wù)器上面給腳本都加上可執(zhí)行權(quán)限:

 

$  chmod +x /etc/keepalived/scripts/*.sh

 

 

7.進行相關(guān)功能測試

 

啟動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

 

嘗試插入一些數(shù)據(jù):

 

$ redis-cli -h 172.168.1.69 SET Hello Redis

 

OK

 

 從VIP讀取數(shù)據(jù)

 

$ redis-cli -h 172.168.1.253 GET Hello

 

"Redis"

 

Master讀取數(shù)據(jù)   

 

$ redis-cli -h 172.168.1.69 GET Hello

 

"Redis"

 

Slave讀取數(shù)據(jù)

 

$ redis-cli -h 172.168.1.57 GET Hello

 

"Redis"

 

8.通過模擬相關(guān)故障,進行功能測試()

 

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

 

然后我們可以發(fā)現(xiàn),Slave已經(jīng)接管服務(wù),并且擔(dān)任Master的角色了。

 

$ redis-cli -h 172.168.1.253 INFO

 

$ redis-cli -h 172.168.1.57 INFO

 

role:master

 

 

 

 

然后我們恢復(fù)Master的Redis進程(恢復(fù)主redis后,需要重啟keepalived  和從庫的redis  可恢復(fù)正常)

 

$  /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

 

發(fā)現(xiàn)目前的Master已經(jīng)再次恢復(fù)了Master的角色,故障切換以及自動恢復(fù)都成功了。

 

 



向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI