Sentinel 的主要功能包括 主节点存活检测、主从运行状况检测、自动故障转移 (failover)、主从切换。Redis 的 Sentinel 最小配置是 一主一从。html
Redis 的 Sentinel 系统能够用来管理多个 Redis 服务器,该系统能够执行如下四个任务:redis
Sentinel 会不断的检查 主服务器 和 从服务器 是否正常运行。服务器
当被监控的某个 Redis 服务器出现问题,Sentinel 经过 API 脚本 向 管理员 或者其余的 应用程序 发送通知。架构
当 主节点 不能正常工做时,Sentinel 会开始一次 自动的 故障转移操做,它会将与 失效主节点 是 主从关系 的其中一个 从节点 升级为新的 主节点,而且将其余的 从节点 指向 新的主节点。调试
在 Redis Sentinel 模式下,客户端应用 在初始化时链接的是 Sentinel 节点集合,从中获取 主节点 的信息。日志
默认状况下,每一个 Sentinel 节点会以 每秒一次 的频率对 Redis 节点和 其它 的 Sentinel 节点发送 PING 命令,并经过节点的 回复 来判断节点是否在线。code
主观下线 适用于全部 主节点 和 从节点。若是在 down-after-milliseconds 毫秒内,Sentinel 没有收到 目标节点 的有效回复,则会断定 该节点 为 主观下线。server
客观下线 只适用于 主节点。若是 主节点 出现故障,Sentinel 节点会经过 sentinel is-master-down-by-addr 命令,向其它 Sentinel 节点询问对该节点的 状态判断。若是超过 <quorum> 个数的节点断定 主节点 不可达,则该 Sentinel 节点会判断 主节点 为 客观下线。htm
# cat 6380/redis-6380.conf port 6380 logfile "" #调试时可打开日志 #logfile "./redis-6380" #守护进程模式 daemonize yes bind 127.0.0.1 # cat 6381/redis-6381.conf port 6381 logfile "./redis-6381" daemonize yes replicaof 127.0.0.1 6380 bind 127.0.0.1
cat 26379/redis-sentinel.conf port 26379 # 守护进程模式 daemonize yes pidfile /var/run/redis-sentinel.pid logfile "" #调试时可打开日志 #logfile /var/log/redis/sentinel.log dir /tmp # 哨兵监控这个master,在至少quorum个哨兵实例都认为master down后把master标记为odown # (objective down客观down;相对应的存在sdown,subjective down,主观down)状态。 # slaves是自动发现,因此你不必明确指定slaves。 sentinel monitor mymaster 127.0.0.1 6380 2 # master或slave多长时间(默认30秒)不能使用后标记为s_down状态。 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 # 若sentinel在该配置值内未能完成failover操做(即故障时master/slave自动切换),则认为本次failover失败。 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes cat 26380/redis-sentinel.conf port 26380 daemonize yes pidfile /var/run/redis-sentinel.pid logfile /var/log/redis/sentinel.log dir /tmp sentinel monitor mymaster 127.0.0.1 6380 2 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes bind 127.0.0.1 cat 26381/redis-sentinel.conf port 26381 daemonize yes pidfile /var/run/redis-sentinel.pid logfile /var/log/redis/sentinel.log dir /tmp sentinel monitor mymaster 127.0.0.1 6380 2 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes bind 127.0.0.1
redis-server 6380/redis-6380.conf redis-server 6381/redis-6381.conf redis-server 26379/redis-sentinel.conf --sentinel redis-server 26380/redis-sentinel.conf --sentinel redis-server 26381/redis-sentinel.conf --sentinel #启动后配置文件会被修改
#链接到sentinel节点 redis-cli -h 127.0.0.1 -p 26381 #查询主节点状态 127.0.0.1:26381> SENTINEL get-master-addr-by-name mymaster 1) "127.0.0.1" 2) "6380" 127.0.0.1:26381> SENTINEL slaves mymaster 127.0.0.1:26381> SENTINEL sentinels mymaster #第一个将提供有关链接到主服务器的从服务器的相似信息, #第二个将提供有关其余Sentinel的信息。 #查询哨兵状态 127.0.0.1:26381> info sentinel # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=mymaster,status=ok,address=127.0.0.1:6380,slaves=1,sentinels=3 #模拟故障 关闭主节点进程 #再次查询 127.0.0.1:26381> SENTINEL get-master-addr-by-name mymaster 1) "127.0.0.1" 2) "6381" #成功切换 #验证成功
https://www.cnblogs.com/bingshu/p/9776610.html 博客
https://redis.io/topics/sentinel 官网资料blog