Redis配置sentinel模式

                Redis配置sentinel模式

                                      作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

 

  哨兵(sentinel)主要是完成三个功能:监控,通知,自动故障转移功能。sentinel是安装Redis服务时默认安装的,因此我们可以直接使用!

 

 

一.配置项说明

1>.port

   服务的监听端口,比如:port 26479。

 

2>.sentinel monitor <master-name> <ip><redis-port><quorm>

  <quorum>表示sentinel集群的quorum机制,即至少有quorum个sentinel节点同时判断主节点故障时,才任务其真的故障。

    s_down:subjectively down

    o_down:objectively down

 

3>.semtinel auth-pass <master-name><password>

 

4>.sentinel down-afer-milliseconds <master-name><millseconds>

  监听到指定的集群的主节点异常状态持久多久方才将标记为“故障”。

 

5>.sentinel parallel-syncs <master-name> <numslaves>

  指在failover过程中,能够被sentinel并行配置的从节点的数量。

 

6>.sentinel failover-timeout <master-name> <milliseconds>

  sentinel必须在此指定的时间长内完成故障转移操作,否则,将视为故障转移操作失败。

 

7>.sentinel notification-script <master-name> <script-path>

  通知脚本,此脚本被自动传递多个参数。

 

8>.redis-cli -h SENTINEL_HOST -p SENTINEL_PORT

 

二.实操案例

1>.编写sentinel的配置文件(我们复用redis集群的虚拟机)

[root@node101.yinzhengjie.org.cn ~]# egrep -v "^$|^#" /etc/redis-sentinel.conf
bind node101.yinzhengjie.org.cn
port 26379
dir /tmp
sentinel monitor yinzhengjie-redis node101.yinzhengjie.org.cn 6379 2
sentinel auth-pass yinzhengjie-redis yinzhengjie
sentinel down-after-milliseconds yinzhengjie-redis 5000
sentinel parallel-syncs yinzhengjie-redis 2
sentinel failover-timeout yinzhengjie-redis 180000
logfile /var/log/redis/sentinel.log
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# egrep -v "^$|^#" /etc/redis-sentinel.conf
[root@node102.yinzhengjie.org.cn ~]# egrep -v "^$|^#" /etc/redis-sentinel.conf
bind node102.yinzhengjie.org.cn
port 26379
dir /tmp
sentinel monitor yinzhengjie-redis node101.yinzhengjie.org.cn 6379 2
sentinel auth-pass yinzhengjie-redis yinzhengjie
sentinel down-after-milliseconds yinzhengjie-redis 5000
sentinel parallel-syncs yinzhengjie-redis 2
sentinel failover-timeout yinzhengjie-redis 180000
logfile /var/log/redis/sentinel.log
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# egrep -v "^$|^#" /etc/redis-sentinel.conf
[root@node103.yinzhengjie.org.cn ~]# egrep -v "^$|^#" /etc/redis-sentinel.conf 
bind node103.yinzhengjie.org.cn
port 26379
dir /tmp
sentinel monitor yinzhengjie-redis node101.yinzhengjie.org.cn 6379 2
sentinel auth-pass yinzhengjie-redis yinzhengjie
sentinel down-after-milliseconds yinzhengjie-redis 5000
sentinel parallel-syncs yinzhengjie-redis 2
sentinel failover-timeout yinzhengjie-redis 180000
logfile /var/log/redis/sentinel.log
[root@node103.yinzhengjie.org.cn ~]# 
[root@node103.yinzhengjie.org.cn ~]# egrep -v "^$|^#" /etc/redis-sentinel.conf

2>.启动sentinel服务

[root@node101.yinzhengjie.org.cn ~]# systemctl start redis-sentinel
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# systemctl status redis-sentinel
● redis-sentinel.service - Redis Sentinel
   Loaded: loaded (/usr/lib/systemd/system/redis-sentinel.service; disabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/redis-sentinel.service.d
           └─limit.conf
   Active: active (running) since Sat 2019-04-06 22:39:47 CST; 11s ago
 Main PID: 9685 (redis-sentinel)
   CGroup: /system.slice/redis-sentinel.service
           └─9685 /usr/bin/redis-sentinel node101.yinzhengjie.org.cn:26379 [sentinel]

Apr 06 22:39:47 node101.yinzhengjie.org.cn systemd[1]: Starting Redis Sentinel...
Apr 06 22:39:47 node101.yinzhengjie.org.cn systemd[1]: Started Redis Sentinel.
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State      Recv-Q Send-Q                                              Local Address:Port                                                             Peer Address:Port              
LISTEN     0      511                                                  172.30.1.101:26379                                                                       *:*                  
LISTEN     0      511                                                  172.30.1.101:6379                                                                        *:*                  
LISTEN     0      128                                                             *:22                                                                          *:*                  
LISTEN     0      128                                                            :::22                                                                         :::*                  
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tail /etc/redis-sentinel.conf 
logfile "/var/log/redis/sentinel.log"
# Generated by CONFIG REWRITE
supervised systemd
sentinel config-epoch yinzhengjie-redis 0
sentinel leader-epoch yinzhengjie-redis 0
sentinel known-slave yinzhengjie-redis 172.30.1.102 6379
sentinel known-slave yinzhengjie-redis 172.30.1.103 6379
sentinel known-sentinel yinzhengjie-redis 172.30.1.102 26379 9c195f20e954d9032f57900c1839857ee4c31be4
sentinel known-sentinel yinzhengjie-redis 172.30.1.103 26379 fbafb3bdbd04c61cf9c4e8dcf0147c7a20f22243
sentinel current-epoch 0
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# systemctl start redis-sentinel
[root@node102.yinzhengjie.org.cn ~]# systemctl start redis-sentinel
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# systemctl status redis-sentinel
● redis-sentinel.service - Redis Sentinel
   Loaded: loaded (/usr/lib/systemd/system/redis-sentinel.service; disabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/redis-sentinel.service.d
           └─limit.conf
   Active: active (running) since Sat 2019-04-06 22:39:44 CST; 42s ago
 Main PID: 9981 (redis-sentinel)
   CGroup: /system.slice/redis-sentinel.service
           └─9981 /usr/bin/redis-sentinel node102.yinzhengjie.org.cn:26379 [sentinel]

Apr 06 22:39:44 node102.yinzhengjie.org.cn systemd[1]: Starting Redis Sentinel...
Apr 06 22:39:44 node102.yinzhengjie.org.cn systemd[1]: Started Redis Sentinel.
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# ss -ntl
State      Recv-Q Send-Q                                              Local Address:Port                                                             Peer Address:Port              
LISTEN     0      511                                                  172.30.1.102:26379                                                                       *:*                  
LISTEN     0      511                                                  172.30.1.102:6379                                                                        *:*                  
LISTEN     0      128                                                             *:22                                                                          *:*                  
LISTEN     0      128                                                            :::22                                                                         :::*                  
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# tail /etc/redis-sentinel.conf
logfile "/var/log/redis/sentinel.log"
# Generated by CONFIG REWRITE
supervised systemd
sentinel config-epoch yinzhengjie-redis 0
sentinel leader-epoch yinzhengjie-redis 0
sentinel known-slave yinzhengjie-redis 172.30.1.103 6379
sentinel known-slave yinzhengjie-redis 172.30.1.102 6379
sentinel known-sentinel yinzhengjie-redis 172.30.1.103 26379 fbafb3bdbd04c61cf9c4e8dcf0147c7a20f22243
sentinel known-sentinel yinzhengjie-redis 172.30.1.101 26379 5697f4a91fe1bf43f075e729bdde6ec504dc820e
sentinel current-epoch 0
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# systemctl start redis-sentinel
[root@node103.yinzhengjie.org.cn ~]# systemctl start redis-sentinel
[root@node103.yinzhengjie.org.cn ~]# 
[root@node103.yinzhengjie.org.cn ~]# systemctl status redis-sentinel
● redis-sentinel.service - Redis Sentinel
   Loaded: loaded (/usr/lib/systemd/system/redis-sentinel.service; disabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/redis-sentinel.service.d
           └─limit.conf
   Active: active (running) since Sat 2019-04-06 07:39:29 PDT; 5s ago
 Main PID: 10042 (redis-sentinel)
   CGroup: /system.slice/redis-sentinel.service
           └─10042 /usr/bin/redis-sentinel node103.yinzhengjie.org.cn:26379 [sentinel]

Apr 06 07:39:29 node103.yinzhengjie.org.cn systemd[1]: Starting Redis Sentinel...
Apr 06 07:39:29 node103.yinzhengjie.org.cn systemd[1]: Started Redis Sentinel.
[root@node103.yinzhengjie.org.cn ~]# 
[root@node103.yinzhengjie.org.cn ~]# tail /etc/redis-sentinel.conf
logfile "/var/log/redis/sentinel.log"
# Generated by CONFIG REWRITE
supervised systemd
sentinel config-epoch yinzhengjie-redis 0
sentinel leader-epoch yinzhengjie-redis 0
sentinel known-slave yinzhengjie-redis 172.30.1.102 6379
sentinel known-slave yinzhengjie-redis 172.30.1.103 6379
sentinel known-sentinel yinzhengjie-redis 172.30.1.101 26379 5697f4a91fe1bf43f075e729bdde6ec504dc820e
sentinel known-sentinel yinzhengjie-redis 172.30.1.102 26379 9c195f20e954d9032f57900c1839857ee4c31be4
sentinel current-epoch 0
[root@node103.yinzhengjie.org.cn ~]# 
[root@node103.yinzhengjie.org.cn ~]# 
[root@node103.yinzhengjie.org.cn ~]# systemctl start redis-sentinel

3>.查看当前集群的状态

[root@node101.yinzhengjie.org.cn ~]# redis-cli -h node101.yinzhengjie.org.cn -p 26379
node101.yinzhengjie.org.cn:26379> 
node101.yinzhengjie.org.cn:26379> SENTINEL masters
1)  1) "name"
    2) "yinzhengjie-redis"
    3) "ip"
    4) "172.30.1.101"
    5) "port"
    6) "6379"
    7) "runid"
    8) "514f95dd0a54449ec221b662da02caa65f74353e"
    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) "442"
   19) "last-ping-reply"
   20) "442"
   21) "down-after-milliseconds"
   22) "5000"
   23) "info-refresh"
   24) "7044"
   25) "role-reported"
   26) "master"
   27) "role-reported-time"
   28) "418718"
   29) "config-epoch"
   30) "0"
   31) "num-slaves"
   32) "2"
   33) "num-other-sentinels"
   34) "2"
   35) "quorum"
   36) "2"
   37) "failover-timeout"
   38) "180000"
   39) "parallel-syncs"
   40) "2"
node101.yinzhengjie.org.cn:26379> 
node101.yinzhengjie.org.cn:26379> 
node101.yinzhengjie.org.cn:26379> SENTINEL masters                  #查看主节点信息
node101.yinzhengjie.org.cn:26379> SENTINEL slaves yinzhengjie-redis
1)  1) "name"
    2) "172.30.1.102:6379"
    3) "ip"
    4) "172.30.1.102"
    5) "port"
    6) "6379"
    7) "runid"
    8) "2b4d70685d17183e80816314d160162770d01aec"
    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) "340"
   19) "last-ping-reply"
   20) "340"
   21) "down-after-milliseconds"
   22) "5000"
   23) "info-refresh"
   24) "9880"
   25) "role-reported"
   26) "slave"
   27) "role-reported-time"
   28) "491744"
   29) "master-link-down-time"
   30) "0"
   31) "master-link-status"
   32) "ok"
   33) "master-host"
   34) "172.30.1.101"
   35) "master-port"
   36) "6379"
   37) "slave-priority"
   38) "100"
   39) "slave-repl-offset"
   40) "146509"
2)  1) "name"
    2) "172.30.1.103:6379"
    3) "ip"
    4) "172.30.1.103"
    5) "port"
    6) "6379"
    7) "runid"
    8) "4ed8d98123094a5922650a4d7047bba03851f6b7"
    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) "144"
   19) "last-ping-reply"
   20) "144"
   21) "down-after-milliseconds"
   22) "5000"
   23) "info-refresh"
   24) "9880"
   25) "role-reported"
   26) "slave"
   27) "role-reported-time"
   28) "491743"
   29) "master-link-down-time"
   30) "0"
   31) "master-link-status"
   32) "ok"
   33) "master-host"
   34) "172.30.1.101"
   35) "master-port"
   36) "6379"
   37) "slave-priority"
   38) "100"
   39) "slave-repl-offset"
   40) "146509"
node101.yinzhengjie.org.cn:26379> 
node101.yinzhengjie.org.cn:26379> 
node101.yinzhengjie.org.cn:26379> SENTINEL slaves yinzhengjie-redis        #查看从节点信息

  我们可以使用SENTINEL实现自动容灾,也可以根据SENTINEL提供的信息手动修改master节点等操作。

 

posted @ 2019-03-22 00:18  尹正杰  阅读(730)  评论(0编辑  收藏  举报