redis sentinel 配置

   Redis 主从配置中,主节点挂了以后,需要手动把一个从节点升成主节点,把另外的从节点做为新的主节点的从节点。redis 提供了 sentinel,可以自动的进行上面的处理。

   在 redis 的安装包里,有一个 sentinel 的示例。运行下面的命令,可以获得一个去掉空行和注释后的配置文件:

cat sentinel.conf | grep -v "#" | grep -v "^$" > sentinel-26379.conf

编辑上面生成的文件,编辑后的内容如下:

port 26379
daemonize yes
pidfile /var/run/redis-sentinel.pid
logfile "s26379.log"
dir /opt/data/redis
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

其中 mymaster 是自定义的名称,如果取另外的名称,需要把所有出现的地方都要改掉。

sentinel monitor mymaster 127.0.0.1 6379 2 配置的是主节点的 IP 和 端口

运行下面的命令,启动 sentinel

redis-sentinel sentinel-26379.conf

启动成功后,我们查看 sentinel-26379.conf 这个配置文件,可以看到文件发生了变化,自动加入了从节点的信息

port 26379
daemonize yes
pidfile "/var/run/redis-sentinel-26379.pid"
logfile "s26379.log"
dir "/opt/data/redis"
sentinel myid 53d9f86064361bcd0b02901f7cc4f12b6230a6f7
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
# Generated by CONFIG REWRITE
protected-mode no
sentinel known-replica mymaster 127.0.0.1 6380
sentinel known-replica mymaster 127.0.0.1 6381
sentinel current-epoch 0

配置完成后,使用 jedis 去连 redis 时,发现报 connection refuse 的错误。最后发现 JedisSentinelPool 返回的主节点的 IP 是 127.0.0.1 ,而不是 redis 主节点真正的 IP。 所以上面的 sentinel 的配置文件中的 redis 的主节点的IP 需要改成真正的 IP。

原文地址:https://www.cnblogs.com/langfanyun/p/11136369.html