主从配置中:当主服务器宕机后,需要手动把一台从服务器切换为主服务器,人工干预,费事费力,还会造成一段时间内服务不可用。更多时候,我们优先考虑哨兵模式。
哨兵概述:
哨兵模式是一种特殊的模式,首先Redis提供了哨兵的命令,哨兵是一个独立的进程,作为进程,它会独立运行。其原理是哨兵通过发送命令,等待Redis服务器响应,从而监控运行的多个Redis实例
故障切换(failover):假设主服务器宕机,哨兵1先检测到这个结果,系统并不会马上进行failover过程,仅仅是哨兵1主观的认为主服务器不可用,这个现象成为主观下线。当后面的哨兵也检测到主服务器不可用,并且数量达到一定值时,那么哨兵之间就会进行一次投票,投票的结果由一个哨兵发起,进行failover操作。然后通过发布订阅模式通知其他的从服务器,修改配置文件,让它们切换主机。切换成功后,就会通过发布订阅模式,让各个哨兵把自己监控的从服务器实现切换主机,这个过程称为客观下线。
测试:
准备哨兵的配置文件(之前的主从配置中是一主二从,所以这里配置三份哨兵配置文件,内容相同)
#哨兵是否要后台启动(不写默认为no,这里测试需要查看日志,不写或者写no)
daemonize no
#sentinel monitor 主节点 ip(测试写localhost不起作用,暂未研究) 端口 几个哨兵同意后切换主服务器
sentinel monitor master 127.0.0.1 6379 2
#哨兵每隔多久监听一次redis架构,默认为30秒,这里设置10秒
sentinel down-after-milliseconds master 10000
启动测试:
[root@localhost bin]# redis-server config/redis.conf
[root@localhost bin]# redis-server config/redis-slave01.conf
[root@localhost bin]# redis-server config/redis-slave02.conf
一主二从:
分别启动三个哨兵(以三个配置文件启动即可,下面展示一个)
[root@localhost bin]# redis-sentinel config/sentinel.conf
5902:X 17 Nov 2020 14:08:08.895 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5902:X 17 Nov 2020 14:08:08.895 # Redis version=6.0.9, bits=64, commit=00000000, modified=0, pid=5902, just started
5902:X 17 Nov 2020 14:08:08.895 # Configuration loaded
5902:X 17 Nov 2020 14:08:08.896 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.0.9 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-' | PID: 5902
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
5902:X 17 Nov 2020 14:08:08.896 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
5902:X 17 Nov 2020 14:08:08.896 # Sentinel ID is 56c3d6fec327c8dffa94d2e4ee066596827188c4
5902:X 17 Nov 2020 14:08:08.896 # +monitor master master 127.0.0.1 6380 quorum 2
将主服务下线
127.0.0.1:6379> SHUTDOWN
not connected>
10秒后查看从服务
127.0.0.1:6380> INFO replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6381,state=online,offset=0,lag=1
master_replid:9bfb66e6c9f7ca5918aaa2c7da4f14158d744982
master_replid2:c51af14b524c0c0166247d0965c01d1cfa500ff1
master_repl_offset:14128
second_repl_offset:13713
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:14128
127.0.0.1:6381> INFO replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6380
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:17286
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:9bfb66e6c9f7ca5918aaa2c7da4f14158d744982
master_replid2:c51af14b524c0c0166247d0965c01d1cfa500ff1
master_repl_offset:17286
second_repl_offset:13713
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:17272
哨兵已将6380设为主服务,这时再将6379服务启动
[root@localhost bin]# redis-server config/redis.conf
[root@localhost bin]# redis-cli -p 6379
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6380
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:69956
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:9bfb66e6c9f7ca5918aaa2c7da4f14158d744982
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:69956
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:68362
repl_backlog_histlen:1595