Redis-哨兵模式-主机宕机-推选新主机的过程

文章目录

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
相关推荐
dazhong201217 分钟前
PLSQL 客户端连接 Oracle 数据库配置
数据库·oracle
了一li2 小时前
Qt中的QProcess与Boost.Interprocess:实现多进程编程
服务器·数据库·qt
码农君莫笑3 小时前
信管通低代码信息管理系统应用平台
linux·数据库·windows·低代码·c#·.net·visual studio
别致的影分身3 小时前
使用C语言连接MySQL
数据库·mysql
京东零售技术5 小时前
“慢”增长时代的企业数据体系建设:超越数据中台
数据库
sdaxue.com5 小时前
帝国CMS:如何去掉帝国CMS登录界面的认证码登录
数据库·github·网站·帝国cms·认证码
o(╥﹏╥)6 小时前
linux(ubuntu )卡死怎么强制重启
linux·数据库·ubuntu·系统安全
海海不掉头发6 小时前
苍穹外卖-day05redis 缓存的学习
学习·缓存
阿里嘎多学长6 小时前
docker怎么部署高斯数据库
运维·数据库·docker·容器
Yuan_o_6 小时前
Linux 基本使用和程序部署
java·linux·运维·服务器·数据库·后端