Redis Sentinel使用说明

4,946 阅读5分钟

Redis哨兵提供了Redis的高可用功能,使用Sentinel之后可以在某些情况下无需人为介入自动的恢复Redis的服务不可用问题。

特性

哨兵主要提供以下能力:

  • 监控:不断的周期性的检查Redis的Master节点和Slave节点的信息
  • 通知:通过API可以通知系统管理员,被监控的Redis实例遇到了某种问题
  • 故障自动恢复:如果某一台Master节点不工作了,Sentinel可以启动故障切换功能,将某台Slave提升为Master,Sentinel会重新配置Slave,同时通知连接Sentinel的客户端,Master的地址改变了
  • 配置提供:客户端连接到Sentinel上获取当前的Redis Master地址

image-20190927072426181

哨兵是分布式部署的,哨兵设计的目的是为了多台哨兵节点能相互配合。运行多台哨兵有如下优点:

  • 故障检测是多台哨兵都同意这台Master不可用了,才认定Master不可用。如果节点数少,可能存在误判
    • 多个Sentinel发现Master节点有问题
    • 选举一个Sentinel作为Master,一般是发现Master不可用的Sentinel节点,Sentinel Master是具有提升Slave为Master功能的
    • 选出一个Slave作为新的Master
    • 通知其余Slave新的Master地址
    • 通知客户度主从变化
    • 等老的Master又被检测活之后,成为新Master的Slave
  • 就算挂掉了几台Sentinel,其余的Sentinel节点仍然正常工作,增强了系统的健壮性。

一套Sentinel集群可以监控多个Redis主从集群

三个定时任务

1 每10s每个Sentinel对Master和Slave执行Info信息

  • 发现Slave节点
  • 确认主从关系

2 每隔2s对每个Sentinel通过Master节点的Channel交换信息(pub/sub),注意是Master节点

  • 通过__ sentinel __:hello 频道交互,Sentinel交互数据的方式,新的Sentinel节点会自动的注册这个频道
  • 交互对节点的“看法”和自身信息

3 每隔1S对其它Sentinel和Redis节点执行ping

  • 心跳检测,失败判断依据

客户端连接Sentinel原理

Redis Sentinel高可用指的是服务端的高可用,如果我们直接连接到Redis 的Master节点挂掉了,那么我们的客户端就不可用了。

我们需要的是服务端高可用和客户端高可用的结合。

1 获取所有的Sentinel节点,遍历得到一个可用的Sentinel节点

2 在可用的Sentinel节点上执行,sentinel get-master-addr-by-name MasterName来返回Master节点信息,包含地址和端口

3 当获取到Master节点之后,执行role或者role replication来验证是否真的是Master节点

4 如果当Master节点发生了变化,Sentinel通知客户端主节点信息发生了变化。客户端接受到变化是通过一个Redis的Channel来实现

启动Sentinel

Sentinel默认监听的端口在TCP 26379,运行Sentinel有两种方式,效果是一样的:

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

下载Redis后,一般都会有sentinel.conf配置文件,里面有sentinel的配置同时配上了文档说明。

配置

一般Redis Sentinel的配置如下:

# 指定监听的Redis Master地址,不需要指定slave的地址,sentinel连接到master之后可以自行获取
# 语法:sentinel monitor <master-group-name> <ip> <port> <quorum>
# quorum是当master无法访问的时候,需要几台sentinel确认才同意master是不可用的
sentinel monitor mymaster 127.0.0.1 6379 2

# 配置格式:sentinel <option_name> <master_name> <option_value>

# down-after-milliseconds,在 60s内,sentinel一直无法ping通mymaster的时候,认为mymaster是不可用的
sentinel down-after-milliseconds mymaster 60000

# 指定当故障发生的时候,进行恢复的超时时间,当failover开始后,在此时间内仍然没有触发任何failover操作,当前sentinel  将会认为此次failoer失败
sentinel failover-timeout mymaster 180000

# 当某台slave被提升为新的master的时候,同一时间有几台slave会更新配置,值越小越好,如果值设的过大,可能导致master阻塞。
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

Sentinel在发现Slave之后,或者提升Slave为Master节点之后,或者新加入了Sentinel节点的时候会自动的更新配置文件。

其他配置:

# 开启守护进程模式
daemonize yes

# 关闭保护模式
protected-mode no

# 让Sentinel只工作在/tmp目录下,减少安全隐患
dir "/tmp"

# 设置sentinel用于连接master的密码,如果redis配置了密码,那这里必须配置认证,否则不能自动切换
sentinel auth-pass <master-name> <password>

Spring Redis配置

1 在配置文件中指定sentinel的地址,如果有多个用逗号分隔sentinel.nodes

spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=192.168.26.129:26379
spring.redis.password=123456

如果不想使用属性配置可以自行在代码中创建:

/**
 * Jedis
 */
@Bean
public RedisConnectionFactory jedisConnectionFactory() {
  RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
  .master("mymaster")
  .sentinel("127.0.0.1", 26379)
  .sentinel("127.0.0.1", 26380);
  return new JedisConnectionFactory(sentinelConfig);
}

2 配置下RedisTemplate的序列器,不然容易出现一些奇怪字符开头的key。如:

image.png

 @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        RedisSerializer stringSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setValueSerializer(stringSerializer);
        redisTemplate.setHashKeySerializer(stringSerializer);
        redisTemplate.setHashValueSerializer(stringSerializer);
        return redisTemplate;
    }

3 注入RedisTemplate使用。

最后

关于Redis Sentinel的配置与使用就到这里了。