概述
- 无哨兵模式的主从切换的方法是
当主服务器宕机后,需要手动把一台从服务器切换为主服务器,这就需要人工干预,费时费力,还会造成一段时间内服务不可用。这不是一种推荐的方式,更多时候,我们优先考虑哨兵模式。
Redis从2.8开始正式提供了Sentinel(哨兵)架构来解决这个问题。
- 哨兵模式的主从故障切换
主从自动切换,能够后台监控主库是否故障,如果主库发生故障,根据投票数自动将从库转换为主库。
哨兵模式是一种特殊的模式,首先Redis提供了哨兵的命令,哨兵是一个独立的进程,作为进程,它会独立运行。
其原理是哨兵通过发送命令,等待Redis服务器响应,从而监控运行的多个Redis实例。
哨兵的作用
- 通过发送命令,让Redis服务器返回监控其运行状态,包括主服务器和从服务器
- 当哨兵监测到master宕机,会自动将slave切换成master,然后通过发布订阅模式通知其他的从服务器,修改配置文件,让它们切换主机。
然而一个哨兵进程对Redis服务器进行监控,可能会出现问题,为此,我们可以使用多个哨兵进行监控。各个哨兵之间还会进行监控,这样就形成了多哨兵模式。
假设主服务器宕机,哨兵1先检测到这个结果,系统并不会马上进行failover过程,仅仅是哨兵1主观的认为主服务器不可用,这个现象成为主观下线。
当后面的哨兵也检测到主服务器不可用,并且数量达到一定值时,那么哨兵之间就会进行一次投票,投票的结果由一个哨兵发起,进行failover(故障转移)操作。
切换成功后,就会通过发布订阅模式,让各个哨兵把自己监控的从服务器实现切换主机,这个过程称为客观下线。
配置哨兵模式
- 准备一主两从的集群模式
# redis-cli -p 6379
127.0.0.1:6379> info replication
# Replication
role:master #主库
connected_slaves:2 #2个从库
slave0:ip=127.0.0.1,port=6380,state=online,offset=25014,lag=0
slave1:ip=127.0.0.1,port=6381,state=online,offset=25014,lag=0
master_failover_state:no-failover
master_replid:abfde85bb3fca5e0e73f88547ce18eb8f1c09470
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:25014
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:25014
- 创建配置文件
# vi sentinel.conf
#sentinel monitor 被监控的名称 host port 有“1”个哨兵认为主库宕机时,该主库才被判定为宕机。
sentinel monitor mymaster 127.0.0.1 6379 1
- 启动哨兵
# redis-sentinel sentinel.conf
55469:X 11 Jun 2024 21:52:13.846 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
55469:X 11 Jun 2024 21:52:13.846 # Redis version=7.0.15, bits=64, commit=00000000, modified=0, pid=55469, just started
55469:X 11 Jun 2024 21:52:13.846 # Configuration loaded
55469:X 11 Jun 2024 21:52:13.847 * Increased maximum number of open files to 10032 (it was originally set to 1024).
55469:X 11 Jun 2024 21:52:13.847 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 7.0.15 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-' | PID: 55469
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
55469:X 11 Jun 2024 21:52:13.847 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
55469:X 11 Jun 2024 21:52:13.848 # Sentinel ID is 37425172dd61ab0afee253f0404108db1fefeb37
55469:X 11 Jun 2024 21:52:13.848 # +monitor master mymaster 127.0.0.1 6379 quorum 1
55469:X 11 Jun 2024 21:52:55.913 # +sdown master mymaster 127.0.0.1 6379
55469:X 11 Jun 2024 21:52:55.913 # +odown master mymaster 127.0.0.1 6379 #quorum 1/1
55469:X 11 Jun 2024 21:52:55.913 # +new-epoch 1
55469:X 11 Jun 2024 21:52:55.913 # +try-failover master mymaster 127.0.0.1 6379
55469:X 11 Jun 2024 21:52:55.915 * Sentinel new configuration saved on disk
验证哨兵模式
- 主库手动宕机
[root@jgswy-pro ~]# redis-cli -p 6379
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=25014,lag=0
slave1:ip=127.0.0.1,port=6381,state=online,offset=25014,lag=0
master_failover_state:no-failover
master_replid:abfde85bb3fca5e0e73f88547ce18eb8f1c09470
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:25014
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:25014
127.0.0.1:6379> set k1 sentinel
OK
127.0.0.1:6379> shutdown
not connected>
- 出现故障转移告警
55469:X 11 Jun 2024 21:52:55.915 # +vote-for-leader 37425172dd61ab0afee253f0404108db1fefeb37 1
55469:X 11 Jun 2024 21:52:55.915 # +elected-leader master mymaster 127.0.0.1 6379
55469:X 11 Jun 2024 21:52:55.915 # +failover-state-select-slave master mymaster 127.0.0.1 6379
55469:X 11 Jun 2024 21:52:56.005 # +selected-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
55469:X 11 Jun 2024 21:52:56.005 * +failover-state-send-slaveof-noone slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
55469:X 11 Jun 2024 21:52:56.105 * +failover-state-wait-promotion slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
55469:X 11 Jun 2024 21:52:56.861 * Sentinel new configuration saved on disk
55469:X 11 Jun 2024 21:52:56.861 # +promoted-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
55469:X 11 Jun 2024 21:52:56.861 # +failover-state-reconf-slaves master mymaster 127.0.0.1 6379
55469:X 11 Jun 2024 21:52:56.961 * +slave-reconf-sent slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
55469:X 11 Jun 2024 21:52:57.890 * +slave-reconf-inprog slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
55469:X 11 Jun 2024 21:52:57.890 * +slave-reconf-done slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
55469:X 11 Jun 2024 21:52:57.962 # +failover-end master mymaster 127.0.0.1 6379
55469:X 11 Jun 2024 21:52:57.962 # +switch-master mymaster 127.0.0.1 6379 127.0.0.1 6380
55469:X 11 Jun 2024 21:52:57.962 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6380
55469:X 11 Jun 2024 21:52:57.962 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
55469:X 11 Jun 2024 21:52:57.964 * Sentinel new configuration saved on disk
55469:X 11 Jun 2024 21:53:28.022 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380 #主库选举为6380端口的库
查看从库已经切换为主库
# redis-cli -p 6380
127.0.0.1:6380> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6381,state=online,offset=19030,lag=0
master_failover_state:no-failover
master_replid:627cb93ba990ca536a5b40f30bdd64ac572f8c10
master_replid2:a07fc1fafd0c77c9737aa79336b81959d46a247a
master_repl_offset:19163
second_repl_offset:808
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:19149
# redis-cli -p 6381
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:0
master_sync_in_progress:0
slave_read_repl_offset:20388
slave_repl_offset:20388
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:627cb93ba990ca536a5b40f30bdd64ac572f8c10
master_replid2:a07fc1fafd0c77c9737aa79336b81959d46a247a
master_repl_offset:20388
second_repl_offset:808
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:20374
注意:
如果主库此时恢复,只能归并到新的主机下,当做从库,这就是哨兵模式的规则!
哨兵模式的优缺点
- 优点
1、哨兵集群,基于主从复制模式,所有的主从配置优点,它全有
2、主从可以切换,故障可以转移,系统的可用性更好
3、哨兵模式就是主从模式的升级,手动到自动,更加健壮! - 缺点
1、Redis 不好在线扩容的,集群容量一旦到达上限,在线扩容就十分麻烦!
2、实现哨兵模式的配置其实是很麻烦的,里面有很多选择