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所在的哨兵没有显示出来。

原文地址:https://www.cnblogs.com/linlf03/p/13238064.html