1、什麼是哨兵模式redis
redis的哨兵模式爲redis提供了高可用。哨兵模式是對主從複製的一個支持,在主從模式下,若是主服務器宕機了的時候,哨兵能夠自動的將salve端提高爲master。當master從新鏈接以後,master會以salve節點加入。ubuntu
哨兵模式的功能:bash
2、如何配置哨兵模式服務器
聽說解壓版會內置sentinel.conf配置文件,我是使用的ubuntu的apt-get命令安裝的,沒有看到該配置文件,因此我本身手動建立了sentinel.conf配置文件,因爲哨兵要大於3個,因此創建三份配置文件。ui
#Sentinel節點的端口 port 26379 dir /var/redis/data/ logfile "26379.log" #當前Sentinel節點監控 127.0.0.1:6379 這個主節點 #2表明判斷主節點失敗至少須要2個Sentinel節點節點贊成 #mymaster是主節點的別名 sentinel monitor mymaster 127.0.0.1 6379 2 #每一個Sentinel節點都要按期PING命令來判斷Redis數據節點和其他Sentinel節點是否可達,若是超過30000毫秒且沒有回覆,則斷定不可達 sentinel down-after-milliseconds mymaster 30000 #當Sentinel節點集合對主節點故障斷定達成一致時,Sentinel領導者節點會作故障轉移操做,選出新的主節點,原來的從節點會向新的主節點發起復制操做,限制每次向新的主節點發起復制操做的從節點個數爲1 sentinel parallel-syncs mymaster 1 #故障轉移超時時間爲180000毫秒 sentinel failover-timeout mymaster 180000
同理配置26380和26381端口配置文件。code
啓動上一章節的主從複製。server
啓動哨兵。進程
啓動哨兵有兩種方式ip
redis-sentinel /path/to/sentinel.conf
redis-server /path/to/sentinel.conf --sentinel
因爲個人安裝版的,也沒找到redis-sentinel命令在哪裏,因此我使用了第二種啓動方式。get
sudo redis-server /etc/redis/sentinel-26379.conf --sentinel
分別啓動三個哨兵。
啓動好了以後開啓一個redis-cli鏈接哨兵
redis-cli -p 26379
執行命令查看master狀態
127.0.0.1:26379> sentinel masters 1) 1) "name" 2) "mymaster" 3) "ip" 4) "127.0.0.1" 5) "port" 6) "6379" 7) "runid" 8) "c323ef9f9036ad491885bbc22d45dd3085bc766b" 9) "flags" 10) "master" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "790" 19) "last-ping-reply" 20) "790" 21) "down-after-milliseconds" 22) "30000" 23) "info-refresh" 24) "8796" 25) "role-reported" 26) "master" 27) "role-reported-time" 28) "78979" 29) "config-epoch" 30) "0" 31) "num-slaves" 32) "1" 33) "num-other-sentinels" 34) "2" 35) "quorum" 36) "2" 37) "failover-timeout" 38) "180000" 39) "parallel-syncs" 40) "1"
看到master狀態正常,下面咱們模擬master掛掉的狀況,新開一個redis-cli執行命令
redis-cli -p 6379 DEBUG sleep 60 #讓master睡眠60秒
因爲咱們剛纔配置的是超過30秒,哨兵會認爲master宕機了,在30秒後咱們從新查看master狀態
127.0.0.1:26379> sentinel masters 1) 1) "name" 2) "mymaster" 3) "ip" 4) "127.0.0.1" 5) "port" 6) "6380" 7) "runid" 8) "bbad4ebb9f103cd252ef5611ca100b564fab8718" 9) "flags" 10) "master" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "634" 19) "last-ping-reply" 20) "634" 21) "down-after-milliseconds" 22) "30000" 23) "info-refresh" 24) "5287" 25) "role-reported" 26) "master" 27) "role-reported-time" 28) "136152" 29) "config-epoch" 30) "1" 31) "num-slaves" 32) "1" 33) "num-other-sentinels" 34) "2" 35) "quorum" 36) "2" 37) "failover-timeout" 38) "180000" 39) "parallel-syncs" 40) "1"
能夠看到slave自動被提高爲了master,在60秒後,原來的master從新上線,咱們再查看salve的狀態
127.0.0.1:26379> sentinel slaves mymaster 1) 1) "name" 2) "127.0.0.1:6379" 3) "ip" 4) "127.0.0.1" 5) "port" 6) "6379" 7) "runid" 8) "c323ef9f9036ad491885bbc22d45dd3085bc766b" 9) "flags" 10) "slave" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "193" 19) "last-ping-reply" 20) "193" 21) "down-after-milliseconds" 22) "30000" 23) "info-refresh" 24) "7841" 25) "role-reported" 26) "slave" 27) "role-reported-time" 28) "802263" 29) "master-link-down-time" 30) "0" 31) "master-link-status" 32) "ok" 33) "master-host" 34) "127.0.0.1" 35) "master-port" 36) "6380" 37) "slave-priority" 38) "100" 39) "slave-repl-offset" 40) "620621"
原來的6379master變成了salve節點。至此哨兵模式配置完成。
3、經過發佈訂閱監控節點運行的狀態
在剛纔的基礎上,咱們新開一個redis-cli窗口
127.0.0.1:26379> SUBSCRIBE +sdown Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "+sdown" 3) (integer) 1
訂閱了+sdown這個頻道
這時候咱們再讓6380進入sleep,觀察頻道接受的消息
127.0.0.1:26379> SUBSCRIBE +sdown Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "+sdown" 3) (integer) 1 1) "message" 2) "+sdown" 3) "master mymaster 127.0.0.1 6380" 1) "message" 2) "+sdown" 3) "slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379"
會自動的發佈節點down掉的狀態和salve提高爲master的信息
查看master狀態能夠看到6379的確成爲了master
127.0.0.1:26379> sentinel masters 1) 1) "name" 2) "mymaster" 3) "ip" 4) "127.0.0.1" 5) "port" 6) "6379" 7) "runid" 8) "c323ef9f9036ad491885bbc22d45dd3085bc766b" 9) "flags" 10) "master" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "923" 19) "last-ping-reply" 20) "923" 21) "down-after-milliseconds" 22) "30000" 23) "info-refresh" 24) "9237" 25) "role-reported" 26) "master" 27) "role-reported-time" 28) "330385" 29) "config-epoch" 30) "2" 31) "num-slaves" 32) "1" 33) "num-other-sentinels" 34) "2" 35) "quorum" 36) "2" 37) "failover-timeout" 38) "180000" 39) "parallel-syncs" 40) "1"
4、哨兵的自動配置
當master宕機的時候,從slave中選擇一個出來當作master,同時會通知其餘的slave,重新的master來同步數據,下面來驗證一下。
再開啓一個redis-server 6381 做爲一個新的slave,如今就變成了1個master2個slave。一樣咱們讓master進行睡眠,此次選擇了6381做爲master
觀察6380的輸出
7713:S 06 May 09:28:29.928 * Connecting to MASTER 127.0.0.1:6381 7713:S 06 May 09:28:29.928 * MASTER <-> SLAVE sync started 7713:S 06 May 09:28:29.928 * Non blocking connect for SYNC fired the event. 7713:S 06 May 09:28:29.928 * Master replied to PING, replication can continue... 7713:S 06 May 09:28:29.928 * Trying a partial resynchronization (request 9083f950d1060650dc83a8f2d82c76fb9dba6755:963245). 7713:S 06 May 09:28:29.929 * Full resync from master: a72b065da9fcfd76111653bef3b2c7613044fe24:996050 7713:S 06 May 09:28:29.929 * Discarding previously cached master state. 7713:S 06 May 09:28:30.019 * MASTER <-> SLAVE sync: receiving 2636 bytes from master 7713:S 06 May 09:28:30.019 * MASTER <-> SLAVE sync: Flushing old data 7713:S 06 May 09:28:30.019 * MASTER <-> SLAVE sync: Loading DB in memory 7713:S 06 May 09:28:30.020 * MASTER <-> SLAVE sync: Finished with success
能夠看到會自動的從6381同步數據。