9.2.1 部署拓扑结构
以3个 Sentinel 节点、1个主节点、2个从节点组成一个 Redis Sentinel 进行说明:
物理部署:
9.2.2 部署 Redis 数据节点
1. 启动主节点
配置:
redis-6379.conf
port 6379
daemonize yes
logfile "6379.log"
dbfilename "dump-6379.rdb"
dir "/opt/soft/redis/data/"
此时拓扑结构:
图9-14 启动主节点
2. 启动两个从节点
配置:
redis-6380.conf
port 6380
daemonize yes
logfile "6380.log"
dbfilename "dump-6380.rdb"
dir "/opt/soft/redis/data/"
slaveof 127.0.0.1 6379
3. 确认主从关系
此时的拓扑结构:
9.2.3 部署 Sentinel 节点
1. 配置 Sentinel 节点
redis-sentinel-26379.conf
port 26379
daemonize yes
logfile "26379.log"
dir /opt/soft/redis/data
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 monitor mymaster 127.0.0.163792 配置代表 sentinel-1 节点需要监控127.0.0.1:6379这个主节点, 2代表判断主节点失败至少需要2个 Sentinel 节点同意, mymaster 是主节点的别名
2. 启动 Sentinel 节点
有两种启动方法:
- 方法一, 使用 redis-sentinel 命令
redis-sentinel redis-sentinel-26379.conf
- 方法二, 使用 redis-server 命令加 —sentinel 参数
redis-server redis-sentinel-26379.conf --sentinel
3. 确认
- info sentinel
此时的拓扑结构:
9.2.4 配置优化
Redis 安装目录下有一个 sentinel.conf, 是默认的 Sentinel 节点配置文件
1. 配置说明和优化
port 26379
dir /opt/soft/redis/data
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 auth-pass <master-name> <password>
#sentinel notification-script <master-name> <script-path>
#sentinel client-reconfig-script <master-name> <script-path>
(1) sentinel monitor
sentinel monitor <master-name> <ip> <port> <quorum>
- quorum: 代表要判定主节点最终不可达所需要的票数。
- 但实际上 Sentinel 节点会对所有节点进行监控, 但是在 Sentinel 节点的配置中没有看到有关从节点和其余 Sentinel 节点的配置, 那是因为 Sentinel 节点会从主节点中获取有关从节点以及其余 Sentinel 节点的相关信息
- 设置的越小, 那么达到下线的条件越宽松, 反之越严格。一般建议将其设置为 Sentinel 节点的一半加1
- 与 Sentinel 节点的领导者选举有关, 至少要有 max(quorum, num(sentinels)/2+1) 个 Sentinel 节点参与选举, 才能选出领导者 Sentinel
(2) sentinel down-after-milliseconds
sentinel down-after-milliseconds <master-name> <times>
- 判断节点不可达的超时时间
- 单位为 ms
(3) sentinel parallel-syncs
sentinel parallel-syncs <master-name> <nums>
用来限制在一次故障转移之后, 每次向新的主节点发起复制操作的从节点个数
(4) sentinel failover-timeout
sentinel failover-timeout <master-name> <times>
通常被解释成故障转移超时时间, 但实际上它作用于故障转移的各个阶段:
a. 选出合适从节点
b. 晋升选出的从节点为主节点
c. 命令其余从节点复制新的主节点
d. 等待原主节点恢复后命令它去复制新的主节点
failover-timeout 的作用具体体现在四个方面:
- 如果 Redis Sentinel 对一个主节点故障转移失败, 那么下次再对该主节点做故障转移的起始时间是 failover-timeout 的2倍
- 在 b) 阶段时, 如果 Sentinel 节点向 a) 阶段选出来的从节点执行 slaveof no one 一直失败 (例如该从节点此时出现故障), 当此过程超过 failover-timeout 时, 则故障转移失败
- 在 b) 阶段如果执行成功, Sentinel 节点还会执行 info 命令来确认 a) 阶段选出来的节点确实晋升为主节点, 如果此过程执行时间超过 failover-timeout 时, 则故障转移失败
- 如果 c) 阶段执行时间超过了 failover-timeout (不包含复制时间), 则故障转移失败。注意即使超过了这个时间, Sentinel 节点也会最终配置从节点去同步最新的主节点
(5) sentinel auth-pass
sentinel auth-pass <master-name> <password>
如果 Sentinel 监控的主节点配置了密码, sentinel auth-pass 配置通过添加主节点的密码, 防止 Sentinel 节点对主节点无法监控
(6) sentinel notification-script
sentinel notification-script <master-name> <script-path>
作用是在故障转移期间, 当一些警告级别的 Sentinel 事件发生 (指重要事件, 例如 -sdown: 客观下线、-odown: 主观下线) 时, 会触发对应路径的脚本, 并向脚本发送相应的事件参数 (会运行一个 shell 脚本)
(7) sentinel client-reconfig-script
sentinel client-reconfig-script <master-name> <script-path>
作用是在故障转移结束后, 会触发对应路径的脚本, 并向脚本发送故障转移结果的相关参数
具体参数如下:
<master-name> <role> <state> <from-ip> <from-port> <to-ip> <to-port>
- role: Sentinel 节点的角色, 分别是 leader 和 observer
- leader 代表当前 Sentinel 节点是领导者, 是它进行的故障转移
- observer 是其余 Sentinel 节点
- from-ip: 原主节点的 ip 地址
- to-ip: 新主节点 ip
有关 sentinel notification-script 和 sentinel client-reconfig-script 有几点需要注意:
必须有可执行权限 开头必须包含 shell 脚本头 (例如 #!/bin/sh), 否则事件发生时 Redis 将无法执行脚本 - Redis 规定脚本的最大执行时间不能超过60秒, 超过后脚本将被杀掉
- 如果 shell 脚本以 exit 1 结束, 那么脚本稍后重试执行。如果以 exit 2 或者更高的值结束, 那么脚本不会重试。正常返回值是 exit 0
- 如果需要运维的 Redis Sentinel 比较多, 建议不要使用这种脚本的形式来进行通知, 这样会增加部署的成本
2. 如何监控多个主节点
拓扑图:
在配置中指定多个 masterName:
这里有个问题是客户端如何通知. 在9.1中说 client 会先连接 sentinel, 这里如果 client 连不上原来的 master 会再次连接 sentinel 吗?
3. 调整配置
Sentinel 节点也支持动态地设置参数, 而且和普通的 Redis 数据节点一样并不是支持所有的参数:
sentinel set <param> <value>
表9-3 sentinel set 命令支持的参数
有几点需要注意一下:
- sentinel set 命令只对当前 Sentinel 节点有效
- sentinel set 命令如果执行成功会立即刷新配置文件, 这点和 Redis 普通数据节点设置配置需要执行 config rewrite 刷新到配置文件不同
- 建议所有 Sentinel 节点的配置尽可能一致, 这样在故障发现和转移时比较容易达成一致
- 表9-3中为 sentinel set 支持的参数, 具体可以参考源码中的 sentinel.c 的 sentinelSetCommand 函数
- Sentinel 对外不支持 config 命令
9.2.5 部署技巧
- Sentinel 节点不应该部署在一台物理“机器”上
- 部署至少三个且奇数个的 Sentinel 节点
- 只有一套 Sentinel, 还是每个主节点配置一套 Sentinel?
图9-20 多套Sentine节点集合