1. 程式人生 > >基於docker實現哨兵集群部署

基於docker實現哨兵集群部署

color class eply 其它 update 所有 timeout latest last

簡單dockerfile文件,用於演示sentinel哨兵故障轉移
FROM centos:latest MAINTAINER BIXIAOYU RUN groupadd
-r redis &&useradd -r -g redis redis RUN yum -y update && yum -y install epel-release && yum -y install redis && yum -y install net-tools EXPOSE 6379

# docker build --no-cache -t redis .



啟動docker容器實例

docker run -itd --name redis-master --net=mynetwork -p 6383:6379 --ip 172.60.0.3 redis

docker run -itd --name redis-slave1 --net=mynetwork -p 6384:6379 --ip 172.60.0.4 redis

docker run -itd --name redis-slave2 --net=mynetwork -p 6385:6379 --ip 172.60.0.5 redis

docker run -itd --name redis-sentinel1 --net=mynetwork -p 22536
:26379 --ip 172.60.0.5 redis docker run -itd --name redis-sentinel2 --net=mynetwork -p 22537:26379 --ip 172.60.0.6 redis docker run -itd --name redis-sentinel3 --net=mynetwork -p 22538:26379 --ip 172.60.0.7 redis

#docker network create --subnet=172.60.0.0/16 mynetwork

技術分享圖片

【配置】

先完成主從同步,主從同步的配置可參考https://www.cnblogs.com/bixiaoyu/p/10706811.html這篇文章,再次就不在細說了,重點是sentinel的配置

bind 0.0.0.0            #設置允許訪問的IP,這裏仍然設置0.0.0.0
protected-mode no         #允許在連接時,密碼為空
port 26379
dir "/tmp"
sentinel myid 6d0d4099c13cdeab018e1f2005455be6f1cd6f6b
sentinel monitor mymaster 172.60.0.3 6379 2        #設置監聽
sentinel config-epoch mymaster 1
sentinel leader-epoch mymaster 1
logfile "/var/log/redis/sentinel.log"
sentinel known-slave mymaster 172.60.0.2 6379
sentinel known-slave mymaster 172.60.0.4 6379
sentinel known-sentinel mymaster 172.60.0.7 26379 dfce433e021aa3e82276974aa12fa0684fb0b4f0
sentinel known-sentinel mymaster 172.60.0.6 26379 ceb363cf84103950cfa2a785816c4e8a36c02143
sentinel current-epoch 1

技術分享圖片

技術分享圖片

選擇一個sentinel節點,查看日誌信息,發現默認主節點為172.60.0.2

技術分享圖片

【測試故障轉移】

先通過pkill模擬故障master故障宕機,隨後觀察哨兵任意節點的情況

技術分享圖片

我們可以從下面sentinel節點上看到172.0.0.3被選舉成為新主節點

[[email protected] /]# tail /var/log/redis/sentinel.log 
68:X 21 Apr 07:51:26.472 * +sentinel sentinel ceb363cf84103950cfa2a785816c4e8a36c02143 172.60.0.6 26379 @ mymaster 172.60.0.2 6379
68:X 21 Apr 07:53:24.154 * +sentinel sentinel dfce433e021aa3e82276974aa12fa0684fb0b4f0 172.60.0.7 26379 @ mymaster 172.60.0.2 6379
68:X 21 Apr 08:16:05.724 # +sdown master mymaster 172.60.0.2 6379
68:X 21 Apr 08:16:05.895 # +new-epoch 1
68:X 21 Apr 08:16:05.895 # +vote-for-leader dfce433e021aa3e82276974aa12fa0684fb0b4f0 1
68:X 21 Apr 08:16:06.276 # +config-update-from sentinel dfce433e021aa3e82276974aa12fa0684fb0b4f0 172.60.0.7 26379 @ mymaster 172.60.0.2 6379
68:X 21 Apr 08:16:06.276 # +switch-master mymaster 172.60.0.2 6379 172.60.0.3 6379
68:X 21 Apr 08:16:06.276 * +slave slave 172.60.0.4:6379 172.60.0.4 6379 @ mymaster 172.60.0.3 6379
68:X 21 Apr 08:16:06.276 * +slave slave 172.60.0.2:6379 172.60.0.2 6379 @ mymaster 172.60.0.3 6379

查看sentinel節點的日誌信息

技術分享圖片

此時,我們把pkill掉的172.60.0.2(舊主節點)重啟,在看看新任主節點172.60.0.3有何變化

重啟的舊主節點成為從節點並加入進來,此時正在進行復制動作,offset的偏移量還沒有同步一致,

技術分享圖片

【小結】

進入哨兵節點客戶端執行SENTINEL masterts命令顯示被監控的所有master以及狀態

127.0.0.1:26379> SENTINEL masters
1)  1) "name"
    2) "mymaster"        #被監控主節點的名稱
    3) "ip"
    4) "172.60.0.3"      #被監控主節點的IP
    5) "port"
    6) "6379"
    7) "runid"
    8) "dd3696a2793e4e19892fca48793d75cec3f07bea"   #被監控主節點的runid值
    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) "847"
   19) "last-ping-reply"
   20) "847"
   21) "down-after-milliseconds"
   22) "30000"                 #監控節點不可達超時時間
   23) "info-refresh"
   24) "6480"
   25) "role-reported"
   26) "master"
   27) "role-reported-time"
   28) "2547025"
   29) "config-epoch"
   30) "1"
   31) "num-slaves"                   #檢測剩余slave節點個數
   32) "2"
   33) "num-other-sentinels"          #檢測其它sentinel節點個數
   34) "2"
   35) "quorum"                 #允許主節點不可用的sentinels的數量(最多允許兩個sentinel節點故障)
   36) "2"
   37) "failover-timeout"     #延遲時間
   38) "180000"
   39) "parallel-syncs"          #復制轉移數量
   40) "1"

執行SENTINEL slaves mymastert查看從節點信息,此時你會看到兩個從節點

1)  1) "name"
    2) "172.60.0.2:6379"
    3) "ip"
    4) "172.60.0.2"
    5) "port"
    6) "6379"
    7) "runid"
    8) "3ede55439a3ce6fb1ab171ed7fd6b6c639725966"
    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) "537"
   19) "last-ping-reply"
   20) "537"
   21) "down-after-milliseconds"
   22) "30000"
   23) "info-refresh"
   24) "4867"
   25) "role-reported"
   26) "slave"
   27) "role-reported-time"
   28) "2525163"
   29) "master-link-down-time"
   30) "0"
   31) "master-link-status"
   32) "ok"
   33) "master-host"
   34) "172.60.0.3"
   35) "master-port"
   36) "6379"
   37) "slave-priority"
   38) "100"
   39) "slave-repl-offset"
   40) "566056"
2)  1) "name"
    2) "172.60.0.4:6379"
    3) "ip"
    4) "172.60.0.4"
    5) "port"
    6) "6379"
    7) "runid"
    8) "f17cfcfc4b9217e1f5a3c0d0a2c55d82da46c37e"
    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) "304"
   19) "last-ping-reply"
   20) "304"
   21) "down-after-milliseconds"
   22) "30000"
   23) "info-refresh"
   24) "3480"
   25) "role-reported"
   26) "slave"
   27) "role-reported-time"
   28) "2865146"
   29) "master-link-down-time"
   30) "0"
   31) "master-link-status"
   32) "ok"
   33) "master-host"
   34) "172.60.0.3"
   35) "master-port"
   36) "6379"
   37) "slave-priority"
   38) "100"
   39) "slave-repl-offset"
   40) "566326"

查看主節點的端口

127.0.0.1:26379> sentinel get-master-addr-by-name mymaster
1) "172.60.0.3"
2) "6379"

執行sentinel failover mymaster命令,強制切換主節點!下圖所示,發現主節點已經由172.60.0.3變為172.60.0.2了,

技術分享圖片

技術分享圖片

待更。。。。

基於docker實現哨兵集群部署