博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Redis配置sentinel模式
阅读量:6894 次
发布时间:2019-06-27

本文共 12102 字,大约阅读时间需要 40 分钟。

                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.confbind node101.yinzhengjie.org.cnport 26379dir /tmpsentinel monitor yinzhengjie-redis node101.yinzhengjie.org.cn 6379 2sentinel auth-pass yinzhengjie-redis yinzhengjiesentinel down-after-milliseconds yinzhengjie-redis 5000sentinel parallel-syncs yinzhengjie-redis 2sentinel failover-timeout yinzhengjie-redis 180000logfile /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.confbind node102.yinzhengjie.org.cnport 26379dir /tmpsentinel monitor yinzhengjie-redis node101.yinzhengjie.org.cn 6379 2sentinel auth-pass yinzhengjie-redis yinzhengjiesentinel down-after-milliseconds yinzhengjie-redis 5000sentinel parallel-syncs yinzhengjie-redis 2sentinel failover-timeout yinzhengjie-redis 180000logfile /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.cnport 26379dir /tmpsentinel monitor yinzhengjie-redis node101.yinzhengjie.org.cn 6379 2sentinel auth-pass yinzhengjie-redis yinzhengjiesentinel down-after-milliseconds yinzhengjie-redis 5000sentinel parallel-syncs yinzhengjie-redis 2sentinel failover-timeout yinzhengjie-redis 180000logfile /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 -ntlState      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 REWRITEsupervised systemdsentinel config-epoch yinzhengjie-redis 0sentinel leader-epoch yinzhengjie-redis 0sentinel known-slave yinzhengjie-redis 172.30.1.102 6379sentinel known-slave yinzhengjie-redis 172.30.1.103 6379sentinel known-sentinel yinzhengjie-redis 172.30.1.102 26379 9c195f20e954d9032f57900c1839857ee4c31be4sentinel known-sentinel yinzhengjie-redis 172.30.1.103 26379 fbafb3bdbd04c61cf9c4e8dcf0147c7a20f22243sentinel 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 -ntlState      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.conflogfile "/var/log/redis/sentinel.log"# Generated by CONFIG REWRITEsupervised systemdsentinel config-epoch yinzhengjie-redis 0sentinel leader-epoch yinzhengjie-redis 0sentinel known-slave yinzhengjie-redis 172.30.1.103 6379sentinel known-slave yinzhengjie-redis 172.30.1.102 6379sentinel known-sentinel yinzhengjie-redis 172.30.1.103 26379 fbafb3bdbd04c61cf9c4e8dcf0147c7a20f22243sentinel known-sentinel yinzhengjie-redis 172.30.1.101 26379 5697f4a91fe1bf43f075e729bdde6ec504dc820esentinel 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.conflogfile "/var/log/redis/sentinel.log"# Generated by CONFIG REWRITEsupervised systemdsentinel config-epoch yinzhengjie-redis 0sentinel leader-epoch yinzhengjie-redis 0sentinel known-slave yinzhengjie-redis 172.30.1.102 6379sentinel known-slave yinzhengjie-redis 172.30.1.103 6379sentinel known-sentinel yinzhengjie-redis 172.30.1.101 26379 5697f4a91fe1bf43f075e729bdde6ec504dc820esentinel known-sentinel yinzhengjie-redis 172.30.1.102 26379 9c195f20e954d9032f57900c1839857ee4c31be4sentinel 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 26379node101.yinzhengjie.org.cn:26379> node101.yinzhengjie.org.cn:26379> SENTINEL masters1)  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-redis1)  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节点等操作。

 

转载于:https://www.cnblogs.com/yinzhengjie/p/10575741.html

你可能感兴趣的文章
第十二周学习总结
查看>>
面试题目
查看>>
struts2执行流程
查看>>
nodejs基础学习
查看>>
【windows8开发】异步编程 之Concurrency::task(C++)
查看>>
杭电1873--看病要排队
查看>>
指针 new delete
查看>>
Xadmin显示视图
查看>>
option菜单个数变化
查看>>
Python 函数(可变参数)
查看>>
FFmpeg中HLS文件解析源码
查看>>
IPMI总结
查看>>
配置OWASP的ModSecurity规则
查看>>
laravel 5.1 单元测试 Cannot modify header information 错误
查看>>
周四作业
查看>>
golang 转换markdown文件为html
查看>>
hdu 3016 dp+线段树
查看>>
对象的类型转换
查看>>
maven 下载源码及 Javadoc
查看>>
MySQL优化三之MySQL配置
查看>>