天天看點

Redis學習筆記(十四)Sentinel(哨兵)(上)

最近談到Redis就會聽到哨兵模式,工作期間同僚也分享過關于哨兵模式的知識,但由于工作忙(給自己找個借口)沒有沒認真看,現在惡補下,老樣子還是分上篇應用,下篇看實作過程,下面我們來看下哨兵到底是啥?

哨兵模式(Sentinel)是Redis的高可用解決方案。由一個或多個Sentinel執行個體組成的Sentinel系統,可以監控任意多個主伺服器,以及這些主伺服器下的所有從伺服器,當某個主伺服器下線時,自動将這個主伺服器下的某個從伺服器更新為新的主伺服器,代替下線的主伺服器繼續處理指令。另外Sentinel系統還會繼續監視已下線的主伺服器,當主伺服器恢複時,它将被降級成該主伺服器的從伺服器。

在看哨兵模式的實作過程之前,我們先看一下哨兵的用法:

1、我們将redis.windows.conf配置檔案拷貝3份出來,改下名字便于識别:6379.conf,6380,conf,6381.conf。(這裡我們模拟使用經典的三節點搭建)

6379.conf我們使用預設配置的6379端口,分别改下 6380,conf與6381.conf的端口号

Redis學習筆記(十四)Sentinel(哨兵)(上)
Redis學習筆記(十四)Sentinel(哨兵)(上)

 上一章我們使用slaveof <masterip> <masterport>指令設定從節點,現在我們修改6380.conf與6391.conf,将複制指令配置在檔案中,将6379作為主節點:

Redis學習筆記(十四)Sentinel(哨兵)(上)
Redis學習筆記(十四)Sentinel(哨兵)(上)

2、下面我們執行指令,啟動三個節點:

redis-server.exe 6379.conf 

redis-server.exe 6380.conf 

redis-server.exe 6381.conf

Redis學習筆記(十四)Sentinel(哨兵)(上)
Redis學習筆記(十四)Sentinel(哨兵)(上)
Redis學習筆記(十四)Sentinel(哨兵)(上)

 好了,現在三個節點都已經啟動,并且從控台上看出,從節點已經在複制主節點的資料。

3、分别建立三個Sentinel配置檔案:sentinel6379.conf ,sentinel6380.conf ,sentinel3381.conf

内容分别是:

 sentinel6379.conf

port 26379
#目前Sentinel服務運作的端口
port 26379  
# 哨兵監聽的主伺服器
sentinel monitor mymaster 127.0.0.1 6379 2
#如果在3秒内無相應,則認為主站點當機
sentinel down-after-milliseconds mymaster 3000
#如果10秒後,mysater仍沒啟動過來,則啟動failover 
sentinel failover-timeout mymaster 10000  
#執行故障轉移時,最多有一台對新的主伺服器進行同
sentinel parallel-syncs mymaster 1      

sentinel6380.conf

#目前Sentinel服務運作的端口
port 26380
# 哨兵監聽的主伺服器
sentinel monitor mymaster 127.0.0.1 6379 2
#如果在3秒内無相應,則認為主站點當機
sentinel down-after-milliseconds mymaster 3000
#如果10秒後,mysater仍沒啟動過來,則啟動failover 
sentinel failover-timeout mymaster 10000  
#執行故障轉移時,最多有一台對新的主伺服器進行同
sentinel parallel-syncs mymaster 1      

sentinel3381.conf

port 26381
# 哨兵監聽的主伺服器
sentinel monitor mymaster 127.0.0.1 6379 2
#如果在3秒内無相應,則認為主站點當機
sentinel down-after-milliseconds mymaster 3000
#如果10秒後,mysater仍沒啟動過來,則啟動failover 
sentinel failover-timeout mymaster 10000  
#執行故障轉移時,最多有一台對新的主伺服器進行同步
sentinel parallel-syncs mymaster 1      

3、執行指令啟動:

redis-server.exe sentinel6379.conf --sentinel

Redis學習筆記(十四)Sentinel(哨兵)(上)

 redis-server.exe sentinel6380.conf --sentinel

Redis學習筆記(十四)Sentinel(哨兵)(上)

 redis-server.exe sentinel6381.conf --sentinel

Redis學習筆記(十四)Sentinel(哨兵)(上)

 4、校驗:

先看下主伺服器6379的資訊:

Redis學習筆記(十四)Sentinel(哨兵)(上)

 切換到6380與6381我們看下:

Redis學習筆記(十四)Sentinel(哨兵)(上)
Redis學習筆記(十四)Sentinel(哨兵)(上)

 現在我們模拟主伺服器6379當機(關閉掉6379的服務端),觀察6380與6381的反應。

Redis學習筆記(十四)Sentinel(哨兵)(上)

 從以上資訊來看,6381被選舉為主伺服器,那麼現在我們分别看下6380與6381的資訊:

Redis學習筆記(十四)Sentinel(哨兵)(上)

 此時監聽資訊為:

Redis學習筆記(十四)Sentinel(哨兵)(上)

 下一步我們恢複6379看看會發生什麼:

Redis學習筆記(十四)Sentinel(哨兵)(上)

 現在看下監聽資訊:

Redis學習筆記(十四)Sentinel(哨兵)(上)

此時發現6379被設定為從伺服器。

現在我們redis-cli 連接配接到6379看下:

Redis學習筆記(十四)Sentinel(哨兵)(上)

OK完工,暫時隻模拟這一種當機情況。

篇幅有些長,不知道有多少同學能看到這裡。

每天學一點,總會有收獲。

下一步我們看下Redis的Sentinel(哨兵)的實作過程