【Redis】Sentinel 高可用架構(gòu)
一 前言
Redis-Sentinel是Redis官方推薦的高可用性(HA)解決方案,當(dāng)用Redis做Master-slave的高可用方案時,假如master宕機了,Redis本身(包括它的很多客戶端)都沒有實現(xiàn)自動進(jìn)行主備切換,而Redis-sentinel本身也是一個獨立運行的進(jìn)程,它能監(jiān)控多個master-slave集群,發(fā)現(xiàn)master宕機后能進(jìn)行自懂切換。它的主要功能有:
-
1 不時地監(jiān)控redis是否按照預(yù)期良好地運行;
-
2 如果發(fā)現(xiàn)某個redis節(jié)點運行出現(xiàn)狀況,能夠通知另外一個進(jìn)程(例如它的客戶端);
-
3 能夠進(jìn)行自動切換。當(dāng)一個master節(jié)點不可用時,能夠選舉出master的多個slave(如果有超過一個slave的話)中的一個來作為新的master,其它的slave節(jié)點會將它所追隨的master的地址改為被提升為master的slave的新地址。
二 實踐
本文介紹搭建主從復(fù)制以及三節(jié)點的sentinel 構(gòu)建redis的高可用系統(tǒng)。其中 redis 一套主從 ,sentinel 三個節(jié)點 因為機器資源限制 ,兩個sentinel 節(jié)點和 redis復(fù)用。
2.1 環(huán)境準(zhǔn)備
-
Redis版本:Redis-3.2.8
-
OS 版本 CentOS 6
-
redis、sentinel
-
10.9.34.172 reids master
-
10.9.43.207 reids slave
-
sentinel
-
10.9.34.252 sentinel
2.2 軟件安裝
-
wget http://download.redis.io/releases/redis-3.2.8.tar.gz
-
tar zxvf redis-3.2.8.tar.gz
-
mv redis-3.2.8 /usr/local/redis
-
cd /usr/local/redis
-
make PREFIX=/usr/local/redis install
-
cd src && make test
-
創(chuàng)建軟連接
-
ln -s /usr/local/redis/bin/redis-cli /usr/local/bin/redis-cli ;
-
ln -s /usr/local/redis/bin/redis-sentinel /usr/local/bin/redis-sentinel ;
-
ln -s /usr/local/redis/bin/redis-server /usr/local/bin/redis-server ;
2.3 配置文件
復(fù)制源碼包里中的sentinel.conf和redis.conf文件到 /etc/redis 目錄,我們需要重寫編寫配置文件,進(jìn)行配置主從.
-
mkdir -p /etc/redis
-
cp /usr/local/redis/redis.conf /etc/redis/redis_7021.conf
-
cp /usr/local/redis/sentinel.conf /etc/redis/sentinel_17021.conf
-
cp /usr/local/redis/utils/redis_init_script /etc/init.d/redis
當(dāng)然如果是想
MySQL單機多實例那樣,redis配置文件還可以放到redis 的實例里面 比如 /data/redis7021/redis.conf ,跟著redis實例走。
redis_7021.conf 的內(nèi)容
需要注意的是 本人為了圖方便 去掉了權(quán)限以及 bind 并且設(shè)置了protected-mode 為no ,安裝測試的過程中因為這兩個參數(shù)導(dǎo)致額外的幾個問題。生產(chǎn)上還是建議啟用這兩個參數(shù)的。還要記得本例子是主庫,從庫還要加上
slaveof master_ip port
sentinel_17021.conf 的內(nèi)容
master 的名稱為
youzan
-
port 17021
-
dir "/data/sentinel_17021"
-
logfile "/data/sentinel_17021/log/alert.log"
-
daemonize yes
-
protected-mode no
-
sentinel monitor youzan 10.9.34.172 7021 2
-
sentinel down-after-milliseconds youzan 5000
-
sentinel failover-timeout youzan 120000
-
#sentinel client-reconfig-script youzan /opt/bin/notify.py
-
rename-command SHUTDOWN REDIS_SHUTDOWN
-
#sentinel auth-pass youzan xxx
/etc/init.d/redis 的內(nèi)容
-
#!/bin/sh
-
#
-
# Simple Redis init.d script conceived to work on Linux systems
-
# as it does use of the /proc filesystem.
-
REDISPORT=7021
-
EXEC=/usr/local/bin/redis-server
-
CLIEXEC=/usr/local/bin/redis-cli
-
PIDFILE=/data/redis_${REDISPORT}/run/redis.pid
-
CONF="/etc/redis/redis_${REDISPORT}.conf"
-
HOST=`hostname -i`
-
case "$1" in
-
start)
-
if [ -f $PIDFILE ]
-
then
-
echo "$PIDFILE exists, process is already running or crashed"
-
else
-
echo "Starting Redis server..."
-
su redis -c "$EXEC $CONF"
-
fi
-
;;
-
stop)
-
if [ ! -f $PIDFILE ]
-
then
-
echo "$PIDFILE does not exist, process is not running"
-
else
-
PID=$(cat $PIDFILE)
-
echo "Stopping ..."
-
$CLIEXEC -h $HOST -p $REDISPORT redis_shutdown
-
while [ -x /proc/${PID} ]
-
do
-
echo "Waiting for Redis to shutdown ..."
-
sleep 1
-
done
-
echo "Redis stopped"
-
fi
-
;;
-
*)
-
echo "Please use start or stop as first argument"
-
;;
-
esac
2.4 添加redis和 sentinel 賬號。
避免使用root賬號直接啟動 redis (存在相關(guān)安全隱患 利用Redis配置不當(dāng)?shù)穆┒磳inux服務(wù)器root提權(quán)體驗 )
-
useradd redis
-
mkdir -p /data/redis_7021/{data,log,run} ;
-
mkdir -p /data/sentinel_17021/{log,run} ;
-
chown -R redis.redis /data/redis_7021/ ;
-
chown -R redis.redis /etc/redis/ ;
-
useradd sentinel ;
-
chown -R sentinel.sentinel /data/sentinel_17021/ ;
-
chown -R sentinel.sentinel /etc/redis/sentinel_17021.conf ;
-
chmod 664 /etc/redis/sentinel_17021.conf
2.5 部署其他節(jié)點
上面的過程是配置redis主庫和其中的一個sentinel,復(fù)制上面的過程在10.9.43.207 重新執(zhí)行一遍進(jìn)行安裝redis和sentinel,不過需要注意的是 我們設(shè)置10.9.43.207為redis的slave節(jié)點 ,所以要在配置文件redis_7021.conf中加上 slaveof 10.9.34.172 7021 。對于安裝第三個sentinel節(jié)點10.9.34.252,只需選取上面步驟中的 sentinel相關(guān)的步驟就好。
2.6 啟動redis和sentinel
啟動redis
/etc/init.d/redis start
關(guān)閉redis
/etc/init.d/redis stop
su sentinel -c "redis-sentinel /etc/redis/sentinel_17021.conf"
啟動sentinel之后的日志如下
-
32126:X 31 Jul 23:28:41.570 # Sentinel ID is b28ca1bb3d722e09d0de905caa380397455e8e3c
-
32126:X 31 Jul 23:28:41.570 # +monitor master youzan 10.9.34.172 7021 quorum 2
-
32126:X 31 Jul 23:28:41.571 * +slave slave 10.9.43.207:7021 10.9.43.207 7021 @ youzan 10.9.34.172 7021
-
32126:X 31 Jul 23:28:54.484 * +sentinel sentinel 5c44560584c3245590e86fa78880eacccdfe8318 10.9.34.252 17021 @ youzan 10.9.34.172 7021
2.7 進(jìn)行故障演練
在主庫上進(jìn)行
-
10.9.34.172:7021> debug sleep 30
-
OK
-
(30.01s)
觀察sentinel的log輸出
32126:X 01 Aug 09:24:12.752 # +sdown master
youzan 10.9.34.172 7021
32126:X 01 Aug 09:24:12.828 # +odown master youzan 10.9.34.172 7021 #quorum 2/2
32126:X 01 Aug 09:24:12.828 # +new-epoch 1
32126:X 01 Aug 09:24:12.828 # +try-failover master
youzan 10.9.34.172 7021
32126:X 01 Aug 09:24:12.838 # +vote-for-leader b28ca1bb3d722e09d0de905caa380397455e8e3c 1
32126:X 01 Aug 09:24:12.842 # 5c44560584c3245590e86fa78880eacccdfe8318 voted for b28ca1bb3d722e09d0de905caa380397455e8e3c 1
32126:X 01 Aug 09:24:12.928 # +elected-leader master
youzan 10.9.34.172 7021
32126:X 01 Aug 09:24:12.928 # +failover-state-select-slave master
youzan 10.9.34.172 7021
32126:X 01 Aug 09:24:12.995 # +selected-slave slave 10.9.43.207:7021 10.9.43.207 7021 @
youzan 10.9.34.172 7021
32126:X 01 Aug 09:24:12.995 * +failover-state-send-slaveof-noone slave 10.9.43.207:7021 10.9.43.207 7021 @
youzan 10.9.34.172 7021
32126:X 01 Aug 09:24:13.047 * +failover-state-wait-promotion slave 10.9.43.207:7021 10.9.43.207 7021 @
youzan 10.9.34.172 7021
32126:X 01 Aug 09:24:13.893 # +promoted-slave slave 10.9.43.207:7021 10.9.43.207 7021 @
youzan 10.9.34.172 7021
32126:X 01 Aug 09:24:13.893 # +failover-state-reconf-slaves master
youzan 10.9.34.172 7021
32126:X 01 Aug 09:24:13.942 # +failover-end master youzan 10.9.34.172 7021
32126:X 01 Aug 09:24:13.942 # +switch-master youzan 10.9.34.172 7021 10.9.43.207 7021
32126:X 01 Aug 09:24:13.943 * +slave slave 10.9.34.172:7021 10.9.34.172 7021 @
youzan 10.9.43.207 7021
32126:X 01 Aug 09:24:18.986 # +sdown slave 10.9.34.172:7021 10.9.34.172 7021 @
youzan 10.9.43.207 7021
32126:X 01 Aug 09:24:37.041 # -sdown slave 10.9.34.172:7021 10.9.34.172 7021 @
youzan 10.9.43.207 7021
32126:X 01 Aug 09:24:47.041 * +convert-to-slave slave 10.9.34.172:7021 10.9.34.172 7021 @
youzan 10.9.43.207 7021
觀察sentinel 的配置文件以及被修改重寫.
三 總結(jié)
本文算是一篇入門級別的how to文檔,介紹如何安裝部署redis sentinel 高可用架構(gòu),內(nèi)容比較淺顯,需要注意的是運維一套數(shù)據(jù)庫存儲系統(tǒng)肯定不會這么簡單,還要涉及到相關(guān)進(jìn)程,日志,性能監(jiān)控等等,本文的案例距離生產(chǎn)實踐還有一大段路要走,繼續(xù)學(xué)習(xí)。
四 參考文章
[1] Redis之Sentinel高可用安裝部署
[2] Redis Sentinel機制與用法(一)
[3] Redis Sentinel機制與用法(二)