测试主从结构的高可用失败,单独使用主从复制,只能做到数据的备份,无法使任何一个从节点在主节点宕机后启动为主节点继续提供服务,redis中提供主从高可用的技术为哨兵模式。
也就是说,哨兵模式是针对主从模式进行高可用,哨兵模式是基于主从复制模式的。
在redis中可以启动哨兵的进程,将某一个主从结构配置到哨兵,让哨兵来管理当前的主从结构。
同一个主从结构可以由多个哨兵进程管理(便于选举),在监控主从结构时,所有的哨兵进程会调用info命令查看当前的主从状态,一旦发现返回的结果中master宕机,所有的哨兵进程会进行选举的操作(过半选举),选出替代主节点执行服务的从节点,执行命令将从节点变换成主节点,继续提供服务。
结构如下图:
哨兵进程启动之后,会自动加载主节点下挂接的从节点,调用info命令查看到的从节点信息,哨兵监听过程使用的是底层RPC通信协议。
最终的哨兵高可用集群模式如下图:
可以调用测试代码,执行代码逻辑,操作sentinel。
哨兵集群中,监控管理主从结构的哨兵个数最好是奇数个。
集群选举容忍度:
2个哨兵存在的时候,为了达到过半原则,可以允许几个宕机?
由上述得出结论:2n和2n-1个集群的选举容忍度相同,为了节省资源,最好配置奇数个哨兵集群。
哨兵集群是基于Redis的主从集群而来的,所以要进行哨兵集群的安装,需要先安装Redis的主从集群。
参看前一章节。
哨兵的配置文件所在的目录为Redis的HOME的根下。
/redis-6.2.6/sentinel.conf
在这个配置文件中,配置主从的关系,主节点信息。
规划两个配置文件,一个使用默认的26379端口,另外一个使用26380端口。
将此配置文件从HOME目录移动到和命令所在的同一目录。
[root@lk7 bin]# cp /home/software/redis-6.2.6/sentinel.conf /usr/local/bin/
修改启动哨兵的配置文件sentinel.conf,内容如下:
例如:
15 # bind 127.0.0.1 192.168.1.1
17 protected-mode no
21 port 26379
26 daemonize yes
31 pidfile /var/run/redis-sentinel.pid
36 logfile "sentinel01.log"
65 dir "/tmp/redis"
84 sentinel monitor mymaster 127.0.0.1 6381 2
224 # Default is 3 minutes.
225 sentinel failover-timeout mymaster 10000
将配置好的sentinel.conf文件复制2个。
[root@lk7 bin]# cp sentinel.conf sentinel01.conf
[root@lk7 bin]# cp sentinel.conf sentinel02.conf
[root@lk7 bin]# vim sentinel02.conf
将sentinel02.conf的端口号改为26380。
启动哨兵进程,开启监听主从结构,命令如下:
redis-sentinel 启动文件
操作如下:
[root@lk7 bin]# redis-sentinel sentinel01.conf
[root@lk7 bin]# tail -f /tmp/redis/sentinel01.log
4063:X 12 Feb 2022 20:53:10.020 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4063:X 12 Feb 2022 20:53:10.021 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=4063, just started
4063:X 12 Feb 2022 20:53:10.021 # Configuration loaded
4063:X 12 Feb 2022 20:53:10.021 * Increased maximum number of open files to 10032 (it was originally set to 1024).
4063:X 12 Feb 2022 20:53:10.021 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.6 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-' | PID: 4063
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
4063:X 12 Feb 2022 20:53:10.021 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
4063:X 12 Feb 2022 20:53:10.022 # Sentinel ID is 110f88ebe35298d2a07ace06d18a29468fb36ad3
4063:X 12 Feb 2022 20:53:10.022 # +monitor master mymaster 127.0.0.1 6381 quorum 2
4063:X 12 Feb 2022 20:53:10.023 * +slave slave 192.168.106.171:6383 192.168.106.171 6383 @ mymaster 127.0.0.1 6381
4063:X 12 Feb 2022 20:53:10.024 * +slave slave 192.168.106.171:6382 192.168.106.171 6382 @ mymaster 127.0.0.1 6381
4063:X 12 Feb 2022 20:53:18.130 * +sentinel sentinel 89ac1bb631eb8ccfc9c854478dfa9114073ec760 192.168.106.171 26380 @ mymaster 127.0.0.1 6381
4063:X 12 Feb 2022 20:53:18.134 * +sentinel-address-switch master mymaster 127.0.0.1 6381 ip 127.0.0.1 port 26380 for 89ac1bb631eb8ccfc9c854478dfa9114073ec760
4063:X 12 Feb 2022 20:53:18.135 * +sentinel-address-switch master mymaster 127.0.0.1 6381 ip 192.168.106.171 port 26380 for 89ac1bb631eb8ccfc9c854478dfa9114073ec760
4063:X 12 Feb 2022 20:53:18.136 * +sentinel-address-switch master mymaster 127.0.0.1 6381 ip 127.0.0.1 port 26380 for 89ac1bb631eb8ccfc9c854478dfa9114073ec760
4063:X 12 Feb 2022 20:53:20.173 * +fix-slave-config slave 192.168.106.171:6383 192.168.106.171 6383 @ mymaster 127.0.0.1 6381
4063:X 12 Feb 2022 20:53:20.173 * +fix-slave-config slave 192.168.106.171:6382 192.168.106.171 6382 @ mymaster 127.0.0.1 6381
kill掉主节点进程,查看哨兵能否启动高可用。
[root@lk7 ~]# ps -ef | grep redis
root 2150 1 0 20:16 ? 00:00:01 redis-server *:6382
root 2159 1 0 20:16 ? 00:00:01 redis-server *:6383
root 3648 2380 0 20:45 pts/1 00:00:00 redis-sentinel *:26379 [sentinel]
root 3843 3302 0 20:49 pts/2 00:00:00 redis-sentinel *:26380 [sentinel]
root 3887 1 0 20:49 ? 00:00:00 redis-server *:6381
root 3918 3460 0 20:50 pts/3 00:00:00 grep --color=auto redis
[root@lk7 ~]# kill 3648
[root@lk7 ~]# ps -ef | grep redis
root 2150 1 0 20:16 ? 00:00:01 redis-server *:6382
root 2159 1 0 20:16 ? 00:00:01 redis-server *:6383
root 3887 1 0 20:49 ? 00:00:00 redis-server *:6381
root 4073 3302 0 20:53 pts/2 00:00:00 redis-sentinel *:26380 [sentinel]
root 4171 2380 0 20:55 pts/1 00:00:00 redis-sentinel *:26379 [sentinel]
root 4190 3460 0 20:55 pts/3 00:00:00 grep --color=auto redis
观察哨兵监视信息,看到如下信息:
4171:X 12 Feb 2022 20:58:27.242 * +switch-master mymaster 192.168.106.171 6382 127.0.0.1 6382
4171:X 12 Feb 2022 20:58:27.242 * +slave slave 192.168.106.171:6383 192.168.106.171 6383 @ mymaster 127.0.0.1 6382
4171:X 12 Feb 2022 20:58:27.242 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6382
4171:X 12 Feb 2022 20:58:27.243 * +slave slave 127.0.0.1:6383 127.0.0.1 6383 @ mymaster 127.0.0.1 6382
4171:X 12 Feb 2022 20:58:27.243 * +slave slave 192.168.106.171:6382 192.168.106.171 6382 @ mymaster 127.0.0.1 6382
上述记录将6382端口的节点选为了主节点。
将宕机的主节点重启,启动后发现哨兵将重启的主节点转化成从节点提供主从服务。
[root@lk7 ~]# redis-server /usr/local/bin/master01.conf
[root@lk7 ~]# ps -ef | grep redis
root 2150 1 0 20:16 ? 00:00:02 redis-server *:6382
root 2159 1 0 20:16 ? 00:00:02 redis-server *:6383
root 4073 3302 0 20:53 pts/2 00:00:02 redis-sentinel *:26380 [sentinel]
root 4171 2380 0 20:55 pts/1 00:00:02 redis-sentinel *:26379 [sentinel]
root 4428 1 0 21:00 ? 00:00:00 redis-server *:6381
root 4437 3460 0 21:00 pts/3 00:00:00 grep --color=auto redis
[root@lk7 ~]# redis-cli -p 6381
127.0.0.1:6381> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:e36b191a8ef04ed4b0a2d0d86ec48e4997e9bfc3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6381> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:e36b191a8ef04ed4b0a2d0d86ec48e4997e9bfc3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6381> info replication
Error: Server closed the connection
127.0.0.1:6381> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6382
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:26334
slave_repl_offset:26334
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:d70d2186f2a5559546c77ec55e16abeaebb80532
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:26334
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:25210
repl_backlog_histlen:1125
127.0.0.1:6381>
宕机掉一个哨兵,当两个哨兵管理主从时,一个宕机,导致另一个的选举没有过半无法生效,quorum由此也可得出:最好启动奇数个哨兵,保证每次至少有过半的哨兵选举成功。
关闭服务的命令如下:
redis-cli -h hostname -p port shutdown
例如:
[root@lk7 bin]# redis-cli -p 26379 shutdown
[root@lk7 bin]# redis-cli -p 26380 shutdown
哨兵在停机后如何重新开启?
先启动3个主从节点
redis-server master01.conf
redis-server slave01.conf
redis-server slave02.conf
然后检查主从关系,分别登录每一个节点调用info replication查看。
127.0.0.1:6383> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6382
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_read_repl_offset:55841
slave_repl_offset:55841
master_link_down_since_seconds:22
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:31a6a3ecb08ff59eef2ccfd54dbf852563f73af6
master_replid2:3cee5b80db5a2a3954302ad119be2b74673f7a10
master_repl_offset:55841
second_repl_offset:52892
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:47736
repl_backlog_histlen:8106
此时主节点已经是6382端口了,将没有挂接的重新挂接主从
slaveof 192.168.106.171 6382
然后查看sentinel配置文件中配置的地址和端口是否和上面看到的Info信息一致。
如果哨兵配置文件和主从结构一致则不需要修改直接启动。
不一致,需要修改为相同的信息,如下:
sentinel monitor mymaster 127.0.0.1 6382 2
如果端口和启动的主从不一致,将端口修改后,把最后的配置内容删除,如下:
# Generated by CONFIG REWRITE
user default on nopass sanitize-payload ~* &* +@all
sentinel myid 110f88ebe35298d2a07ace06d18a29468fb36ad3
sentinel config-epoch mymaster 15
sentinel leader-epoch mymaster 15
sentinel current-epoch 15
sentinel known-replica mymaster 127.0.0.1 6381
sentinel known-replica mymaster 127.0.0.1 6383
sentinel known-sentinel mymaster 192.168.106.171 26380 89ac1bb631eb8ccfc9c854478dfa9114073ec760
sentinel known-replica mymaster 192.168.106.171 6381
sentinel known-replica mymaster 192.168.106.171 6383
sentinel known-replica mymaster 192.168.106.171 6382
然后保存。
启动哨兵:
redis-sentinel sentinel01.conf
redis-sentinel sentinel02.conf
完成启动之后,主从master一直处于down状态。
[root@lk7 ~]# redis-cli -p 6383
127.0.0.1:6383> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6383
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_read_repl_offset:300720
slave_repl_offset:300720
master_link_down_since_seconds:-1
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:98a5592593e5ce29a73982989eb57c1c03a9075d
master_replid2:8da7c2e0cc7626594adf7ef3a60c0aef75f46f18
master_repl_offset:300720
second_repl_offset:297918
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:292880
repl_backlog_histlen:7841
127.0.0.1:6383>
哨兵一直对主节点进行切换,节点都是正常启动的,但是在主从的状态中,主节点一直是down状态。
哨兵一直对主节点进行选举,但是恢复不了master结点的up状态。
读数据都没有问题,但是不能进行写操作。
请遇到过这种问题的大神给个解答,谢谢!