文章目录
1、为哨兵模式准备配置文件
bash
复制代码
[root@localhost redis]# ll
总用量 244
drwxr-xr-x. 2 root root 150 12月 6 2023 bin
-rw-r--r--. 1 root root 89 6月 24 14:16 dump6379.rdb
-rw-r--r--. 1 root root 173 6月 24 14:18 dump6380.rdb
-rw-r--r--. 1 root root 89 6月 24 14:19 dump6381.rdb
-rw-r--r--. 1 root root 89 12月 20 2023 dump.rdb
-rw-r--r--. 1 root root 207 6月 24 12:33 redis_6379.conf
-rw-r--r--. 1 root root 207 6月 24 12:47 redis_6380.conf
-rw-r--r--. 1 root root 207 6月 24 12:52 redis_6381.conf
-rw-r--r--. 1 root root 106547 6月 24 13:59 redis_common.conf
-rw-r--r--. 1 root root 106546 12月 6 2023 redis.conf
[root@localhost redis]# vim sentinel.conf
bash
复制代码
sentinel monitor mymaster 192.168.74.148 6379 1
2、启动哨兵
bash
复制代码
[root@localhost redis]# /usr/local/redis/bin/redis-sentinel /usr/local/redis/sentinel.conf
7984:X 24 Jun 2024 15:59:36.245 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7984:X 24 Jun 2024 15:59:36.245 # Redis version=7.0.10, bits=64, commit=00000000, modified=0, pid=7984, just started
7984:X 24 Jun 2024 15:59:36.245 # Configuration loaded
7984:X 24 Jun 2024 15:59:36.245 * Increased maximum number of open files to 10032 (it was originally set to 1024).
7984:X 24 Jun 2024 15:59:36.245 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 7.0.10 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-' | PID: 7984
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
7984:X 24 Jun 2024 15:59:36.246 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
7984:X 24 Jun 2024 15:59:36.264 * Sentinel new configuration saved on disk
7984:X 24 Jun 2024 15:59:36.264 # Sentinel ID is 5d1670addf29afbffc1cf278f58a91b672f6b96c
7984:X 24 Jun 2024 15:59:36.264 # +monitor master mymaster 192.168.74.148 6379 quorum 1
7984:X 24 Jun 2024 15:59:36.265 * +slave slave 192.168.74.148:6380 192.168.74.148 6380 @ mymaster 192.168.74.148 6379
7984:X 24 Jun 2024 15:59:36.267 * Sentinel new configuration saved on disk
7984:X 24 Jun 2024 15:59:36.267 * +slave slave 192.168.74.148:6381 192.168.74.148 6381 @ mymaster 192.168.74.148 6379
7984:X 24 Jun 2024 15:59:36.271 * Sentinel new configuration saved on disk
3、主机6379宕机
bash
复制代码
[root@localhost ~]# /usr/local/redis/bin/redis-cli -p 6379 shutdown
[root@localhost ~]#
3.4、查看sentinel控制台日志
bash
复制代码
7984:X 24 Jun 2024 16:10:27.455 # +sdown master mymaster 192.168.74.148 6379
7984:X 24 Jun 2024 16:10:27.455 # +odown master mymaster 192.168.74.148 6379 #quorum 1/1
7984:X 24 Jun 2024 16:10:27.455 # +new-epoch 1
7984:X 24 Jun 2024 16:10:27.455 # +try-failover master mymaster 192.168.74.148 6379
7984:X 24 Jun 2024 16:10:27.460 * Sentinel new configuration saved on disk
7984:X 24 Jun 2024 16:10:27.460 # +vote-for-leader 5d1670addf29afbffc1cf278f58a91b672f6b96c 1
7984:X 24 Jun 2024 16:10:27.460 # +elected-leader master mymaster 192.168.74.148 6379
7984:X 24 Jun 2024 16:10:27.460 # +failover-state-select-slave master mymaster 192.168.74.148 6379
7984:X 24 Jun 2024 16:10:27.561 # +selected-slave slave 192.168.74.148:6380 192.168.74.148 6380 @ mymaster 192.168.74.148 6379
7984:X 24 Jun 2024 16:10:27.561 * +failover-state-send-slaveof-noone slave 192.168.74.148:6380 192.168.74.148 6380 @ mymaster 192.168.74.148 6379
7984:X 24 Jun 2024 16:10:27.620 * +failover-state-wait-promotion slave 192.168.74.148:6380 192.168.74.148 6380 @ mymaster 192.168.74.148 6379
7984:X 24 Jun 2024 16:10:27.904 * Sentinel new configuration saved on disk
7984:X 24 Jun 2024 16:10:27.904 # +promoted-slave slave 192.168.74.148:6380 192.168.74.148 6380 @ mymaster 192.168.74.148 6379
7984:X 24 Jun 2024 16:10:27.904 # +failover-state-reconf-slaves master mymaster 192.168.74.148 6379
7984:X 24 Jun 2024 16:10:27.992 * +slave-reconf-sent slave 192.168.74.148:6381 192.168.74.148 6381 @ mymaster 192.168.74.148 6379
7984:X 24 Jun 2024 16:10:28.935 * +slave-reconf-inprog slave 192.168.74.148:6381 192.168.74.148 6381 @ mymaster 192.168.74.148 6379
7984:X 24 Jun 2024 16:10:28.935 * +slave-reconf-done slave 192.168.74.148:6381 192.168.74.148 6381 @ mymaster 192.168.74.148 6379
7984:X 24 Jun 2024 16:10:28.998 # +failover-end master mymaster 192.168.74.148 6379
7984:X 24 Jun 2024 16:10:28.998 # +switch-master mymaster 192.168.74.148 6379 192.168.74.148 6380
7984:X 24 Jun 2024 16:10:28.998 * +slave slave 192.168.74.148:6381 192.168.74.148 6381 @ mymaster 192.168.74.148 6380
7984:X 24 Jun 2024 16:10:28.998 * +slave slave 192.168.74.148:6379 192.168.74.148 6379 @ mymaster 192.168.74.148 6380
7984:X 24 Jun 2024 16:10:29.002 * Sentinel new configuration saved on disk
7984:X 24 Jun 2024 16:10:59.012 # +sdown slave 192.168.74.148:6379 192.168.74.148 6379 @ mymaster 192.168.74.148 6380
此时发现推选6380为主机
3.5、查看6380主从信息
bash
复制代码
127.0.0.1:6380> info replication
Error: Broken pipe
not connected>
需要重新启动6380客户端连接redis,因为原先的主机6379发生宕机
bash
复制代码
[root@localhost ~]# /usr/local/redis/bin/redis-cli -p 6380
127.0.0.1:6380> ping
PONG
127.0.0.1:6380> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.74.148,port=6381,state=online,offset=108933,lag=0
master_failover_state:no-failover
master_replid:78a6a3071e3ec590e70826d029320ccd1f91e0fa
master_replid2:bad8e16c55e217d4cab9aff4598fd83f84b74336
master_repl_offset:108933
second_repl_offset:52335
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:169
repl_backlog_histlen:108765
127.0.0.1:6380>
4、复活6379
bash
复制代码
[root@localhost ~]# /usr/local/redis/bin/redis-server /usr/local/redis/redis_6379.conf
[root@localhost ~]# /usr/local/redis/bin/redis-cli -p 6379
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.74.148
master_port:6380
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:138273
slave_repl_offset:138273
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:78a6a3071e3ec590e70826d029320ccd1f91e0fa
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:138273
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:128045
repl_backlog_histlen:10229
127.0.0.1:6379>
此时复活的6379变成6380的从机也就是奴隶
4.1、再次查看sentinel控制台日志
bash
复制代码
7984:X 24 Jun 2024 16:27:50.950 # -sdown slave 192.168.74.148:6379 192.168.74.148 6379 @ mymaster 192.168.74.148 6380
7984:X 24 Jun 2024 16:28:00.960 * +convert-to-slave slave 192.168.74.148:6379 192.168.74.148 6379 @ mymaster 192.168.74.148 6380