Redis 哨兵工作原理和常用命令介紹


一、哨兵原理介紹

1、哨兵部署圖

 

 

2、哨兵故障轉移

 

 這里設置quorum為2,當有兩個哨兵任務master掛了,才確認master掛了。

因為由於網絡抖動,只有一個哨兵檢測到master掛了,而另外一個哨兵檢測到master正常,這時候master是正常的。

 

3、哨兵選舉leader(Redis主掛了)

如我們這里有三個哨兵,投票選擇出一個哨兵為leader。然后由leader負責故障轉移,選擇一個master

master同時和幾個slave進行同步的參數設置為 sentinel parallel-syncs master num

 

4、原Rdis主起來了

 

 原Redis主起來后,它不會再成為Redis主,而是成為Redis從,並且同步新的Redis主的數據,然后被哨兵集群監控。

 

二、哨兵部署約定

哨兵節點要有至少3個或者奇數個節點

哨兵分布式部署在不同的計算機節點

一組哨兵只監聽一組主從(如redis主為larry-master)

 

三、哨兵常用命令

登錄哨兵

redis-cli -p 26379

 

查看redis主的信息

sentinel master larry-master

 1) "name"
 2) "larry-master"
 3) "ip"
 4) "192.168.127.128"
 5) "port"
 6) "6379"
 7) "runid"
 8) "2d15b7313732f62dd598f4da80f7db8b82a8ef60"
 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) "554"
19) "last-ping-reply"
20) "554"
21) "down-after-milliseconds"
22) "10000"
23) "info-refresh"
24) "5179"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "155752"
29) "config-epoch"
30) "7"
31) "num-slaves"
32) "2"
33) "num-other-sentinels"
34) "2"
35) "quorum"
36) "2"
37) "failover-timeout"
38) "180000"
39) "parallel-syncs"
40) "1"

可以看到master節點的IP為192.168.127.128

slaves節點有2個   num-slaves 2

其它哨兵還有2個  num-other-sentinels 2

1個節點並行同步(1個接着1個)   parallel-syncs 1

 

3、查看redis從

sentinel slaves larry-master

 1) "name"
 2) "larry-master"
 3) "ip"
 4) "192.168.127.128"
 5) "port"
 6) "6379"
 7) "runid"
 8) "2d15b7313732f62dd598f4da80f7db8b82a8ef60"
 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) "554"
19) "last-ping-reply"
20) "554"
21) "down-after-milliseconds"
22) "10000"
23) "info-refresh"
24) "5179"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "155752"
29) "config-epoch"
30) "7"
31) "num-slaves"
32) "2"
33) "num-other-sentinels"
34) "2"
35) "quorum"
36) "2"
37) "failover-timeout"
38) "180000"
39) "parallel-syncs"
40) "1"
127.0.0.1:26379> sentinel slave larry-master
(error) ERR Unknown sentinel subcommand 'slave'
127.0.0.1:26379> sentinel slaves larry-master
1)  1) "name"
    2) "192.168.127.130:6379"
    3) "ip"
    4) "192.168.127.130"
    5) "port"
    6) "6379"
    7) "runid"
    8) "da45d5c17a41eed651f8b881c524c3e03ef5f501"
    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) "1032"
   19) "last-ping-reply"
   20) "1032"
   21) "down-after-milliseconds"
   22) "10000"
   23) "info-refresh"
   24) "874"
   25) "role-reported"
   26) "slave"
   27) "role-reported-time"
   28) "553029"
   29) "master-link-down-time"
   30) "0"
   31) "master-link-status"
   32) "ok"
   33) "master-host"
   34) "192.168.127.128"
   35) "master-port"
   36) "6379"
   37) "slave-priority"
   38) "100"
   39) "slave-repl-offset"
   40) "678142"
2)  1) "name"
    2) "192.168.127.129:6379"
    3) "ip"
    4) "192.168.127.129"
    5) "port"
    6) "6379"
    7) "runid"
    8) "1e9bb414770851c5790fe4b07606296f726c5b78"
    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) "287"
   19) "last-ping-reply"
   20) "287"
   21) "down-after-milliseconds"
   22) "10000"
   23) "info-refresh"
   24) "874"
   25) "role-reported"
   26) "slave"
   27) "role-reported-time"
   28) "553029"
   29) "master-link-down-time"
   30) "0"
   31) "master-link-status"
   32) "ok"
   33) "master-host"
   34) "192.168.127.128"
   35) "master-port"
   36) "6379"
   37) "slave-priority"
   38) "100"
   39) "slave-repl-offset"
   40) "678142"

  可以看到有兩台redis從

 

4、查看有多少個哨兵信息

1)  1) "name"
    2) "fa64ad5e0de67a44c80cc1376ac9f8911d906ffe"
    3) "ip"
    4) "192.168.127.128"
    5) "port"
    6) "26379"
    7) "runid"
    8) "fa64ad5e0de67a44c80cc1376ac9f8911d906ffe"
    9) "flags"
   10) "sentinel"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "686"
   19) "last-ping-reply"
   20) "686"
   21) "down-after-milliseconds"
   22) "10000"
   23) "last-hello-message"
   24) "926"
   25) "voted-leader"
   26) "?"
   27) "voted-leader-epoch"
   28) "0"
2)  1) "name"
    2) "ad3a89df27414fdacf9edc3141a40847df6bfc0c"
    3) "ip"
    4) "192.168.127.129"
    5) "port"
    6) "26379"
    7) "runid"
    8) "ad3a89df27414fdacf9edc3141a40847df6bfc0c"
    9) "flags"
   10) "sentinel"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "685"
   19) "last-ping-reply"
   20) "685"
   21) "down-after-milliseconds"
   22) "10000"
   23) "last-hello-message"
   24) "505"
   25) "voted-leader"
   26) "?"
   27) "voted-leader-epoch"
   28) "0"

  這里有兩台哨兵,自己登錄的是192.168.127.130所在的哨兵沒有顯示出來。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM