官网: https://redis.io/topics/sentinel
参考: http://download.redis.io/redis-stable/sentinel.conf
上篇文章: https://www.jianshu.com/p/e23c95f18dff
什么是Sentinel, 能做什么
Sentinel是Redis的高可用性解决方案, 主从复制解决了读性能的优化, 但是一旦Master宕机后, 其他的Slave的全部就变为不可用, 因此Sentinel由此而生, 当Master不可用时候, Sentinel就会用投票策略提升另外一台可用的Slave作为Master, 当Master重新恢复之后, 自动变为Slave.
本文是在我的上一篇文章的基础上进行讲解, 因为涉及到Master-slaver
1: 安装
apt-get install redis-sentinel
2: 卸载
apt-get remove redis-sentinel
3: 配置文件
新建配置文件:sentinel.conf
参考官方的配置: http://download.redis.io/redis-stable/sentinel.conf
port 26379
sentinel monitor mymaster 10.0.0.99 6379 1
sentinel down-after-milliseconds mymaster 10000
sentinel failover-timeout mymaster 6000
sentinel auth-pass mymaster 111111
4: 依次启动主(6379), 从(6380, 6381)
5: 启动
redis-sentinel sentinel.conf
或者
redis-server sentinel.conf --sentinel
6: 观察日志
从日志中可以观察到, sentinel(6380,6381已经被监控)
# WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
# Sentinel ID is 0b53549b2b6d5d8c276d2cb3b2148fbc72008c0c
# +monitor master mymaster 10.0.0.99 6379 quorum 1
* +slave slave 10.0.0.99:6381 10.0.0.99 6381 @ mymaster 10.0.0.99 6379
* +slave slave 10.0.0.99:6380 10.0.0.99 6380 @ mymaster 10.0.0.99 6379
7: 停掉主的redis(6379)
kill
8: 观察日志
从日志看, 已经将6380提升为Master
# +monitor master mymaster 10.0.0.99 6379 quorum 1
# +sdown master mymaster 10.0.0.99 6379
# +odown master mymaster 10.0.0.99 6379 #quorum 1/1
# +new-epoch 1
# +try-failover master mymaster 10.0.0.99 6379
# +vote-for-leader 0b53549b2b6d5d8c276d2cb3b2148fbc72008c0c 1
# +elected-leader master mymaster 10.0.0.99 6379
# +failover-state-select-slave master mymaster 10.0.0.99 6379
# +selected-slave slave 10.0.0.99:6380 10.0.0.99 6380 @ mymaster 10.0.0.99 6379
* +failover-state-send-slaveof-noone slave 10.0.0.99:6380 10.0.0.99 6380 @ mymaster 10.0.0.99 6379
* +failover-state-wait-promotion slave 10.0.0.99:6380 10.0.0.99 6380 @ mymaster 10.0.0.99 6379
# +promoted-slave slave 10.0.0.99:6380 10.0.0.99 6380 @ mymaster 10.0.0.99 6379
# +failover-state-reconf-slaves master mymaster 10.0.0.99 6379
* +slave-reconf-sent slave 10.0.0.99:6381 10.0.0.99 6381 @ mymaster 10.0.0.99 6379
* +slave-reconf-inprog slave 10.0.0.99:6381 10.0.0.99 6381 @ mymaster 10.0.0.99 6379
# +failover-end-for-timeout master mymaster 10.0.0.99 6379
# +failover-end master mymaster 10.0.0.99 6379
* +slave-reconf-sent-be slave 10.0.0.99:6380 10.0.0.99 6380 @ mymaster 10.0.0.99 6379
* +slave-reconf-sent-be slave 10.0.0.99:6381 10.0.0.99 6381 @ mymaster 10.0.0.99 6379
# +switch-master mymaster 10.0.0.99 6379 10.0.0.99 6380
* +slave slave 10.0.0.99:6381 10.0.0.99 6381 @ mymaster 10.0.0.99 6380
* +slave slave 10.0.0.99:6379 10.0.0.99 6379 @ mymaster 10.0.0.99 6380
# +sdown slave 10.0.0.99:6379 10.0.0.99 6379 @ mymaster 10.0.0.99 6380
9: 连接6380,进行读写操作
redis-cli -p 6380 -h 10.0.0.99
> keys *
1) "name"
> set age 112
OK