Redis Sentinel

官方说明文档:http://www.redis.io/topics/sentinel

Sentinel: 提供高可用为Redis。

Sentinel提供的功能

监控:监控Master和Slaves的工作状况

通知:Redis实例有问题,通知系统管理员,程序

自动故障转移:如果master出现故障,从slaves中推选出一个master,其他的slaves重新配置链接新的master,并且通知应用程序使用新的链接。

配置信息提供者:给客户端提供可用的地址。

启动Sentinel

将src目录下产生的redis-sentinel文件copy到$REDIS_HOMEin目录下

redis-sentinel /path/to/sentinel.conf

or

redis-server /path/to/sentinel.conf --sentinel

运行Sentinel必须使用配置文件,默认监听端口26379。

基本原则:

1、最少需要三个实例;

2、三个实例分布在不同的主机或虚拟机中。

3、Sentinel分布式系统在故障期间不能保证写操作被保留。

4、客户端需要Sentinel支持。

配置文件

sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 60000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1

sentinel monitor resque 192.168.1.3 6380 4
sentinel down-after-milliseconds resque 10000
sentinel failover-timeout resque 180000
sentinel parallel-syncs resque 5

配置不需要指定slaves

sentinel monitor <master-group-name> <ip> <port> <quorum>

quorum 至少需要多少个sentinel同意master下线

down-after-milliseconds:  过了多少毫秒链接不上认为故障

parallel-syncs:故障切换后有多少从节点同步主节点数据,数量越少故障切换处理完成的时间越长,

Sentinel部署

       +----+
       | M1 |
       | S1 |
       +----+
          |
+----+    |    +----+
| R2 |----+----| R3 |
| S2 |         | S3 |
+----+         +----+

Configuration: quorum = 2

编译安装3台redis,node1、node2、node3
node1为master
node2、node3是Slaves

配置文件中

slaveof node1 6379

分别启动node1、node2、node3的redis服务

service redisd start


在3个节点的~目录下,创建sentinel.conf
vim sentinel.conf

port 26379
sentinel monitor mymaster node1 6379 2

执行如下命令:

cp redis-2.8.18/src/redis-sentinel /usr/redis/bin
redis-sentinel sentinel.conf

分别在3个节点使用redis-cli
测试redis主从复制效果

keys *

将node1服务停掉

service redisd stop

隔一会会启动failover,这个时间可以配置以下选项
down-after-milliseconds 选项指定了 Sentinel 认为服务器已经断线所需的毫秒数

master切换到node2节点。

当node1节点的redis服务恢复,成为slave节点。

在node2、node3上测试

redis-cli -h node1 -p 26379 info sentinel
redis-cli -p 26379 SENTINEL master mymaster
redis-cli -p 26379 SENTINEL slaves mymaster
redis-cli -p 26379 SENTINEL get-master-addr-by-name mymaster
原文地址:https://www.cnblogs.com/one--way/p/5757153.html