天天看點

快來學Redis | Sentinel(哨兵)模式搭建過程

Redis Sentinel(哨兵)模式的搭建過程分為兩個部分:

  • Master-Slave 叢集的搭建;
  • Sentinel(哨兵)叢集的搭建。

整體環境

  • Master-Slave 叢集;
    • Master:192.168.1.128:6379
    • Slave01:192.168.1.128:6380
    • Slave02:192.168.1.128:6381
  • Sentinel(哨兵)叢集
    • Sentinel01: 192.168.1.129:26379
    • Sentinel02: 192.168.1.129:26380
    • Sentinel03: 192.168.1.129:26381

Master-Slave 叢集的搭建過程請看:快來學Redis | 主從複制架構的搭建過程

Redis 的安裝請參看:快來學Redis | Linux下的安裝

下面我們來看 Sentinel(哨兵)叢集的搭建過程。

搭建過程

安裝好 Redis 後,複制三份配置檔案:

sentinel.conf

檔案在源碼目錄下。

[root@peipei3514 /]# cp /usr/local/src/redis-4.0.9/sentinel.conf /usr/local/redis/sentinel26379.conf
[root@peipei3514 /]# cp /usr/local/src/redis-4.0.9/sentinel.conf /usr/local/redis/sentinel26380.conf
[root@peipei3514 /]# cp /usr/local/src/redis-4.0.9/sentinel.conf /usr/local/redis/sentinel26381.conf
           
分别修改配置檔案:
# 設定綁定位址,使之可以被外部通路(綁定哨兵所在機器的ip位址)
bind  

# 指定監聽端口
port 

# 以背景方式運作
daemonize yes

# 設定日志級别為 verbose,友善觀看日志資訊
loglevel verbose

# 設定日志檔案
logfile /usr/local/redis/logs/sentinel26379.log

# 指定工作目錄
# dir <working-directory>
dir /usr/local/redis/sentinel/

# 指定别名  主節點位址  端口  哨兵個數(有幾個哨兵監控到主節點當機執行轉移)
# sentinel monitor <master-name> <ip> <redis-port> <quorum>
sentinel monitor mymaster   

# 如果哨兵3s内沒有收到主節點的心跳,哨兵就認為主節點當機了,預設是30秒
sentinel down-after-milliseconds mymaster 

# 選舉出新的主節點之後,可以同時連接配接從節點的個數
sentinel parallel-syncs mymaster 

# 如果10秒後,master仍沒活過來,則啟動failover,預設180s
sentinel failover-timeout mymaster 

# 配置連接配接redis主節點密碼(如果需要的話進行配置)
# sentinel auth-pass <master-name> <password>
           

以此類推,修改端口 26380 及 26381 的配置。

建立相應的檔案夾:

[root@peipei3514 /]# mkdir -p /usr/local/redis/sentinel/26379
[root@peipei3514 /]# mkdir -p /usr/local/redis/sentinel/26380
[root@peipei3514 /]# mkdir -p /usr/local/redis/sentinel/26381
           

注意:我們稍後要啟動三個 redis 執行個體,其中端口為 6379 的 redis 設為 master,其他兩個個設為 slave 。是以 mymaster 後跟的是 master 的 ip 和端口,最後一個 ‘2’ 代表隻要有2個 sentinel 認為 master 下線,就認為該 master 客觀下線,啟動 failover 并選舉産生新的 master。通常最後一個參數不能多于啟動的 sentinel 執行個體數。

建議至少啟動三台 sentinel 執行個體

分别啟動三個 Sentinel 執行個體

啟動 Sentinel 執行個體之前,先啟動 Redis 伺服器執行個體,并設定 6379 為 master,6380 和 6381 為 slave。

[root@peipei3514 /]# /usr/local/redis/bin/redis-sentinel /usr/local/redis/sentinel26379.conf
[root@peipei3514 /]# /usr/local/redis/bin/redis-sentinel /usr/local/redis/sentinel26380.conf
[root@peipei3514 /]# /usr/local/redis/bin/redis-sentinel /usr/local/redis/sentinel26381.conf

[root@peipei3514 /]# ps -ef | grep redis
root                : ?        :: /usr/local/redis/bin/redis-sentinel *: [sentinel]
root                : ?        :: /usr/local/redis/bin/redis-sentinel *: [sentinel]
root                : ?        :: /usr/local/redis/bin/redis-sentinel *: [sentinel]
root             : pts/    :: grep --color=auto redis
           

啟動日志(26379)(三台哨兵伺服器的日志是一樣的):

[root@peipei3514 ~]# tail -f /usr/local/redis/logs/sentinel26379.log

:X  Jul :: # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=1548, just started
:X  Jul :: # Configuration loaded
:X  Jul :: * Running mode=sentinel, port=. # 在 26379 端口運作哨兵模式
:X  Jul :: # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
:X  Jul :: # Sentinel ID is 4ed5b983c127292f1a9ebf6c76af4d71c546693a
:X  Jul :: # +monitor master mymaster 192.168.1.128 6379 quorum 2   # 已經監控到了master
:X  Jul :: - Accepted .:   # 增加新的哨兵
:X  Jul :: - Accepted .:   # 增加新的哨兵
           

sentinel 一些指令介紹

要使用 sentinel 的指令,我們需要用

redis-cli

指令進入到 sentinel:

[root@peipei3514 /]# /usr/local/redis/bin/redis-cli -h 192.168.1.129 -p 26379
.:>
           
  • INFO

    :sentinel 的基本狀态資訊
  • SENTINEL masters | SENTINEL master mymaster

    :列出所有被監視的主伺服器,以及這些主伺服器的目前狀态
  • SENTINEL slaves mymaster

    :列出給定主伺服器的所有從伺服器,以及這些從伺服器的目前狀态
  • SENTINEL get-master-addr-by-name mymaster

    :傳回給定名字的主伺服器的 IP 位址和端口号
  • SENTINEL reset

    :重置所有名字和給定模式 pattern 相比對的主伺服器。重置操作清除主伺服器目前的所有狀态,包括正在執行中的故障轉移, 并移除目前已經發現和關聯的, 主伺服器的所有從伺服器和 Sentinel 。
  • SENTINEL failover

    :當主伺服器失效時, 在不詢問其他 Sentinel 意見的情況下,強制開始一次自動故障遷移,但是它會給其他sentinel發送一個最新的配置,其他sentinel會根據這個配置進行更新。
測試:

(1)登陸到 master:

[[email protected] /]# /usr/local/redis/bin/redis-cli -h 192.168.1.128 -p 6379
:> keys *  # 查詢所有的key
) "lpf01"
:> set lpf02 liupeifeng02  # 設定新的資料
OK

[[email protected] /]# /usr/local/redis/bin/redis-cli -h 192.168.1.128 -p 6380
:> keys * # 檢視所有的key,可以看到資料同步成功
) "lpf01"
) "lpf02"
:> set lpf02 liupeifeng02  # 在從設定資料是不允許的(預設隻讀)。
(error) READONLY You can't write against a read only slave.
           

可以看到:我們的主從模式中,slave 預設是隻讀。

(2)目前 6379 是 master,我們強制 kill 掉 6379 的程序以後,檢視sentinel 打出的日志資訊:

kill 掉 6379 伺服器的程序:

[root@peipei3514 ~]# ps -ef | grep redis
root                : ?        :: /usr/local/redis/bin/redis-server .:
root                : ?        :: /usr/local/redis/bin/redis-server .:
root                : ?        :: /usr/local/redis/bin/redis-server .:
root             : pts/    :: grep --color=auto redis
[root@peipei3514 ~]# kill 1658
           

哨兵伺服器的日志:

:X  Jul :: # +sdown master mymaster 192.168.1.128 6379  # 這個哨兵認為 master 下線(主觀)
:X  Jul :: # +new-epoch 14
:X  Jul :: # +vote-for-leader e062a3797da4b8f8ebc64c5b7cf38fd7495f2c40 14
:X  Jul :: # +odown master mymaster 192.168.1.128 6379 #quorum 3/2  # 已經有兩個哨兵認為 master 下線(變為客觀下線)
:X  Jul :: # Next failover delay: I will not start a failover before Sun Jul  1 11:12:25 2018 # 設定一個故障轉移的時間點
:X  Jul :: # +config-update-from sentinel e062a3797da4b8f8ebc64c5b7cf38fd7495f2c40 192.168.1.129 26381 @ mymaster 192.168.1.128                            6379
:X  Jul :: # +switch-master mymaster 192.168.1.128 6379 192.168.1.128 6380 # 将 master 從 6379 切換為 6380
:X  Jul :: * +slave slave .: .  @ mymaster .    # 增加 6380 的 slave 節點
:X  Jul :: * +slave slave .: .  @ mymaster .    # 增加 6380 的 slave 節點
:X  Jul :: # +sdown slave 192.168.1.128:6379 192.168.1.128 6379 @ mymaster 192.168.1.128 6380 # 不停地試探 6379 伺服器
           

注意事項

故障轉移失敗:

-failover-abort-no-good-slave master mymaster 192.168.1.128 6379

,日志如下:

:X  Jun :: # Sentinel ID is 4ed5b983c127292f1a9ebf6c76af4d71c546693a
:X  Jun :: # +monitor master mymaster 192.168.1.128 6379 quorum 2
:X  Jun :: * +sentinel-address-switch master mymaster .  ip .. port  for bf5144e4f9d40c61ab71485ae3039b0aa5df3506
:X  Jun :: * +sentinel-address-switch master mymaster .  ip .. port  for e062a3797da4b8f8ebc64c5b7cf38fd7495f2c4
:X  Jun :: # +sdown master mymaster 192.168.1.128 6379
:X  Jun :: # +sdown slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 192.168.1.128 6379
:X  Jun :: # +sdown slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 192.168.1.128 6379
:X  Jun :: # +new-epoch 1
:X  Jun :: # +vote-for-leader bf5144e4f9d40c61ab71485ae3039b0aa5df3506 1
:X  Jun :: # +odown master mymaster 192.168.1.128 6379 #quorum 2/2
:X  Jun :: # Next failover delay: I will not start a failover before Sun Jun 24 23:04:48 2018
:X  Jun :: # +new-epoch 2
:X  Jun :: # +try-failover master mymaster 192.168.1.128 6379
:X  Jun :: # +vote-for-leader 4ed5b983c127292f1a9ebf6c76af4d71c546693a 2
:X  Jun :: # e062a3797da4b8f8ebc64c5b7cf38fd7495f2c40 voted for 4ed5b983c127292f1a9ebf6c76af4d71c546693a 2
:X  Jun :: # bf5144e4f9d40c61ab71485ae3039b0aa5df3506 voted for 4ed5b983c127292f1a9ebf6c76af4d71c546693a 2
:X  Jun :: # +elected-leader master mymaster 192.168.1.128 6379
:X  Jun :: # +failover-state-select-slave master mymaster 192.168.1.128 6379
:X  Jun :: # -failover-abort-no-good-slave master mymaster 192.168.1.128 6379
:X  Jun :: # Next failover delay: I will not start a failover before Sun Jun 24 23:10:48 2018
:X  Jun :: # +new-epoch 3
           

從哨兵的日志可以看出,哨兵是從位址為 127.0.0.1 的 Redis 伺服器接收資訊的,這明顯不對,再看一下配置檔案(這部分是哨兵自動生成的)

快來學Redis | Sentinel(哨兵)模式搭建過程

可以看到,哨兵認為 master 和 slave 的位址為 127.0.0.1。從這裡可以知道問題,使我們 Redis 伺服器 bind 的位址不對:

去掉 bind 位址清單中的 127.0.0.1

下面來看一個正确的

快來學Redis | Sentinel(哨兵)模式搭建過程

另外,上面的問題好了之後,切換 master 還是不成功,不知道為什麼,但是過了幾天,在重試的時候就好了,應該就是重新開機一下機器吧!!!!

參考文章:

  1. Redis Sentinel機制與用法(一)
  2. Redis Sentinel 機制與用法(二)
  3. Redis-sentinel哨兵模式叢集方案配置
  4. 基于Sentinel(哨兵)搭建實作Redis高可用叢集
  5. Redis-sentinel哨兵模式叢集方案配置
  6. Redis進階實踐之十 Redis哨兵叢集模式

前兩篇文章寫的非常好,大家可以重點關注一下。

繼續閱讀