天天看點

【Redis主從架構】搭建經典的3節點哨兵叢集

1. 哨兵配置檔案

位于redis解壓根目錄下:sentinel.conf
【Redis主從架構】搭建經典的3節點哨兵叢集
  • 最小配置

每一個哨兵都可以去監控多個master-slaves的主從架構

因為生産環境一般會部署多個master-slaves的redis主從叢集

相同的一套哨兵叢集,可以去監控不同的多個redis主從叢集。

你可以給每個redis主從叢集配置設定一個邏輯的名稱:

sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 60000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1

           

1.1 sentinel monitor mymaster 192.168.0.102 6379 2

指定一個master的監控,給監控的master指定一個名稱,指定監控的IP位址和端口号,2表示qronum。
  • quorum詳解:
  1. 至少需要quorum個哨兵一緻同意,master程序挂掉了,擷取slave程序挂掉了,擷取要啟動一個故障轉移操作。
  2. quorum是用來識别故障的,真正執行故障轉移的時候,還要在哨兵叢集執行選舉,選舉一個哨兵程序出來進行故障轉移操作。
  3. 假如有5個哨兵,quorum設定了2,那麼5個哨兵中的2個都認為master挂掉了,master才會認為是odown了。就會從2個哨兵中的選舉一個哨兵,來執行故障轉移。如果5個哨兵中有3個哨兵都是運作的,那麼故障轉移就會别允許執行。

1.2 sentinel down-after-milliseconds

超過多少毫秒跟一個redis執行個體端口了連接配接,哨兵就可能認為redis執行個體挂掉了。

1.3 sentinel failover-timeout

執行故障轉移的timeout的逾時時長,如果限定時長内沒有完成故障轉移,就會認為執行故障轉移失敗,就從剩下的哨兵選擇一個哨兵從新執行故障轉移。

1.4 sentinel parallel-syncs

新的master切換之後,允許同時有多少個slave被切換到去連接配接新的master,重新做同步,數字越低,花費的事件就越高。
假如你的redis是1個master,4個slave
然後你的master當機了,4個salve中有一個切換成了master,剩下3個slave就要挂到新的master上去。
這個時候,如果parallel-syncs是1,那麼3個slave,會一個接一個挂到新的master上面去,一個挂完後,而且從新的master sync完資料之後,再挂接下一個。
如果parallel-syncs是3,那麼一次性就會把所有的slave挂接到新的master上去。

2. 安裝一主二從的redis主從架構

eshop-cache01,eshop-cache02,eshop-cache03上安裝redis,并啟動。

# 安裝tcl
wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz
tar -xzvf tcl8.6.1-src.tar.gz
cd  /usr/local/tcl8.6.1/unix/
./configure  
make && make install

# 安裝redis
tar -zxvf redis-3.2.8.tar.gz
cd redis-3.2.8
make && make test
make install
           

3. 搭建經典三個節點的哨兵叢集

哨兵預設是用26279端口,預設不能跟其他叢集在指定端口聯通,隻能在本地通路
mkdir /etc/sentinal

mkdir -p /var/sentinal/5000/log

# 編輯etc/sentinel/5000.conf
cp sentinal.conf 5000.conf

vi 5000.conf

           
  • 配置sentinal
# redis綁定端口
port 5000
# redis綁定ip
bind 192.168.31.187
# redis綁定緩存存儲目錄
dir /var/sentinal/5000
# 哨兵綁定端口,ip,設定quorum
sentinel monitor mymaster 192.168.0.102 6379 2
# 哨兵連接配接 master 逾時時間
sentinel down-after-milliseconds mymaster 30000
# 哨兵設定 哨兵故障轉移逾時時間
sentinel failover-timeout mymaster 60000
# 哨兵設定 運作同時挂載到新master的slave數量
sentinel parallel-syncs mymaster 1
           

3.1 啟動哨兵程序

eshop-cache01,eshop-cache02,eshop-cache03三個節點啟動sentinal,并檢視日志。
  • 啟動sentinal指令
redis-sentinel /etc/sentinal/5000.conf
redis-server /etc/sentinal/5000.conf --sentinal
           

日志會顯示出來,每個哨兵去監控對應的redis master,并且能夠自動發現對應的slave。

哨兵之間,互相也會自動進行發現,用的就是pub/sub消息釋出和訂閱資訊系統和機制

【Redis主從架構】搭建經典的3節點哨兵叢集

3.2 檢查哨兵轉态

SENTINEL get-master-addr-by-name mymaster
           
  • 進入哨兵指令行
redis-cli -h 192.168.31.187 -p 5000
           
  • 檢視sentinel是否搭建成功
info sentinel
           
【Redis主從架構】搭建經典的3節點哨兵叢集

從圖檔中可以看到:redis的master節點有兩個從節點,sentinel有三個節點。

  • 檢視所有master的資訊
sentinel master mymaster
           
【Redis主從架構】搭建經典的3節點哨兵叢集
  • 檢視所有slave的資訊
SENTINEL slaves mymaster
           
【Redis主從架構】搭建經典的3節點哨兵叢集
【Redis主從架構】搭建經典的3節點哨兵叢集
  • 檢視所有的哨兵資訊
SENTINEL sentinels mymaster
           
【Redis主從架構】搭建經典的3節點哨兵叢集
  • 根據master名稱擷取master位址資訊
SENTINEL get-master-addr-by-name mymaster
           
【Redis主從架構】搭建經典的3節點哨兵叢集

繼續閱讀