前述
哨兵模式是一种特殊的模式,首先Redis提供了哨兵的命令,哨兵是一个独立的进程,作为进程,它会独立运行。其原理是哨兵通过发送命令,等待Redis服务器响应,从而监控运行的多个Redis实例。
哨兵作用
- 通过发送命令,让Redis服务器返回监控其运行状态,包括主服务器和从服务器。
- 当哨兵监测到master宕机,会自动将slave切换成master,然后通过发布订阅模式通知其他的从服务器,修改配置文件,让它们切换主机。
哨兵配置
- 注意前提是已经搭建好主从复制
1、配置sentinel
[root@localhost kconfig]# ll
总用量 336
-rw-r--r--. 1 root root 83405 3月 3 11:51 redis79.conf
-rw-r--r--. 1 root root 83405 3月 3 11:54 redis80.conf
-rw-r--r--. 1 root root 83405 3月 3 11:56 redis81.conf
-rw-r--r--. 1 root root 83393 3月 3 12:20 redis.conf
[root@localhost kconfig]# vim sentinel.conf
# sentinel.conf 内容如下
# 语法:sentinel monitor 被监控的名称 host port 1
# 后面的数字 1,代表主机挂了,slave投票看让谁接替成为主机,票数最多的,就会成为主机!
sentinel monitor myredis 127.0.0.1 6379 1
2、配置后启动哨兵
[root@localhost redis]# cd bin
[root@localhost bin]# ls
6379.log 6381.log dump6379.rdb dump6381.rdb mkreleasehdr.sh redis-check-aof redis-sentinel
6380.log appendonly.aof dump6380.rdb dump.rdb redis-benchmark redis-cli redis-server
[root@localhost bin]# redis-sentinel /usr/local/redis/kconfig/sentinel.conf
97974:X 03 Mar 2021 14:24:38.611 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
97974:X 03 Mar 2021 14:24:38.611 # Redis version=6.0.6, bits=64, commit=00000000, modified=0, pid=97974, just started
97974:X 03 Mar 2021 14:24:38.611 # Configuration loaded
97974:X 03 Mar 2021 14:24:38.611 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.0.6 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-' | PID: 97974
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
97974:X 03 Mar 2021 14:24:38.612 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
97974:X 03 Mar 2021 14:24:38.614 # Sentinel ID is 61644413c27e72084533d34fa95591da303943fc
97974:X 03 Mar 2021 14:24:38.614 # +monitor master myredis 127.0.0.1 6379 quorum 1
97974:X 03 Mar 2021 14:24:38.615 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ myredis 127.0.0.1 6379
97974:X 03 Mar 2021 14:24:38.616 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ myredis 127.0.0.1 6379
3、测试哨兵
1、将Master(redis79)宕机
2、观察哨兵,查看哨兵日志
3、观察从机信息,哨兵自动从从机中选举一个为主机
4、如果主机此时回来了,只能归并到新的主机下,当做从机,这就是哨兵模式的规则
优缺点
优点
- 哨兵集群,基于主从复制模式,所有的主从配置优点,它全有
- 主从可以切换,故障可以转移,系统的可用性就会更好
- 哨兵模式就是主从模式的升级,手动到自动,更加健壮
缺点
- Redis不好在线扩容的,集群容量一旦到达上限,在线扩容就十分麻烦
- 实现哨兵模式的配置其实是很麻烦的,里面有很多选择
哨兵模式的全部配置
#Example sentinel.conf
# 哨兵sentinel实例运行的端口 默认26379
port 26379
# 哨兵sentinel的工作目录
dir /tmp
#哨兵sentinel监控的redis主节点的ip port
# master-name可以自己命名的主节点名字只能由字母A-z、数字O-9 、这三个字符".-_"组成。
# quorum配置多少个sentine1哨兵统一认为master主节点失联那么这时客观上认为主节点失联了
# sentinel monitor <master-name> <ip> <redis-port> <quorum>
sentinel monitor mymaster 127.0.0.1 6379 2
#当在Redis实例中开启了requirepass foobared 授权密码这样所有连接Redis实例的客户端都要提供密码
#设置哨兵sentinel连接主从的密码注意必须为主从设置一样的验证密码
# sentinel auth-pass <master-name> <plassword>
sentinel auth-pass mymaster MySUPER--secret-o123passwOrd
#指定多少毫秒之后主节点没有应答哨兵sentine1 此时哨兵主观上认为主节点下线默认30秒
# sentinel down-after-mi1liseconds <master-name> <mi77iseconds>
sentinel down-after-mi1liseconds mymaster 30000
#这个配置项指定了在发生failover主备切换时最多可以有多少个s1ave同时对新的master进行同步,这个数字越小,完成failover所需的时间就越长,但是如果这个数字越大,就意味着越多的s1ave因为replication而不可用。
#可以通过将这个值设为1来保证每次只有一个s1ave 处于不能处理命令请求的状态。
#sentinel para1le1-syncs <master-name> <nums1aves>
sentinel para1le1-syncs mymaster 1
#故障转移的超时时间 failover-timeout可以用在以下这些方面:
#1.同一个sentine1对同一个master两次failover之间的间隔时间。
#2,当一个slave从一个错误的master那里同步数据开始计算时间。直到slave被纠正为向正确的master那里同步数据时。#3.当想要取消一个正在进行的failover所需要的时间。
#4.当进行failoverit,配置所有slaves指向新的master所需的最大时间。不过,即使过了这个超时,slaves依然会被正确配置为指向master,但是就不按paralle1-syncs所配置的规则来了
#默认三分钟
# sentinel failover-timeout <master-name> <mi17iseconds>
sentinel failover-timeout mymaster 180000
#通知脚本
#sentine7 notification-script <master-name> <script-path>
sentinel notification-script mymaster /var/redis/notify.sh