Redis哨兵

准备

服务器准备

  • 3个哨兵:自动监控和维护集群,不存放数据
  • 1主2从:用于数据读取和存放

哨兵集群配置

sh 复制代码
# 创建一个哨兵集群的配置文件夹
[root@node1 ~]# mkdir /usr/local/redis/sentinel

创建哨兵集群所使用的配置文件

sh 复制代码
[root@node1 ~]# vim /usr/local/redis/sentinel/sential26379.conf

配置文件内容如下
# sentinel节点1配置
[root@node1 ~]# cat /usr/local/redis/sentinel/sential26379.conf
bind 0.0.0.0
daemonize yes
protected-mode no
port 26379
logfile "/usr/local/redis/log/sentinel26379.log"
pidfile /var/run/redis-sentinel26379.pid
dir /usr/local/redis/data
sentinel monitor mymaster 192.168.202.129 6379 2
sentinel auth-pass mymaster 123456

# sentinel节点2配置
[root@node1 ~]# cat /usr/local/redis/sentinel/sential26380.conf
bind 0.0.0.0
daemonize yes
protected-mode no
port 26380
logfile "/usr/local/redis/log/sentinel26380.log"
pidfile /var/run/redis-sentinel26380.pid
dir /usr/local/redis/data
sentinel monitor mymaster 192.168.202.129 6379 2
sentinel auth-pass mymaster 123456

# sentinel节点3配置
[root@node1 ~]# cat /usr/local/redis/sentinel/sential26381.conf
bind 0.0.0.0
daemonize yes
protected-mode no
port 26381
logfile "/usr/local/redis/log/sentinel26381.log"
pidfile /var/run/redis-sentinel26381.pid
dir /usr/local/redis/data
sentinel monitor mymaster 192.168.202.129 6379 2
sentinel auth-pass mymaster 123456

启动一个一主两从的redis集群

参考链接

注意,主从密码配置成一样的,要不然从机或者主机身份变化时候需要拷贝数据的时候会出现验证不通过的情况。

启动sentinel集群

本示例使用了一台机器启动了三个sentinel实例

sh 复制代码
[root@node1 ~]# redis-sentinel /usr/local/redis/sentinel/sential26379.conf --sentinel
[root@node1 ~]# redis-sentinel /usr/local/redis/sentinel/sential26380.conf --sentinel
[root@node1 ~]# redis-sentinel /usr/local/redis/sentinel/sential26381.conf --sentinel

可以看到redis配置文件发生了变化

sh 复制代码
# Generated by CONFIG REWRITE
save 3600 1
save 300 100
save 60 10000
latency-tracking-info-percentiles 50 99 99.9
user default on sanitize-payload #8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92 ~* &* +@all

模拟master挂了

将现在的master关闭

sh 复制代码
[root@node1 ~]# redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.202.130,port=6379,state=online,offset=641582,lag=0
slave1:ip=192.168.202.131,port=6379,state=online,offset=641727,lag=0
master_failover_state:no-failover
master_replid:07c3db3aea823b87fb9e477208f8afa1377af2ec
master_replid2:ed614ba98a153f419741e75e97233f088ee7ee0b
master_repl_offset:641727
second_repl_offset:11999
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:11999
repl_backlog_histlen:629729
127.0.0.1:6379> shutdown
not connected> 

可以看到原来的slave节点变为了master节点

sh 复制代码
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.202.130,port=6379,state=online,offset=658546,lag=1
master_failover_state:no-failover
master_replid:5fc98adc5e69818e9e9374d79cef9e804d24088f
master_replid2:07c3db3aea823b87fb9e477208f8afa1377af2ec
master_repl_offset:658546
second_repl_offset:643664
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:11747
repl_backlog_histlen:646800
相关推荐
追风筝的人er30 分钟前
SpringBoot+Vue3 企业考勤如何处理法定假期?节假日方案、调休补班与工作日判断链路拆解
前端·vue.js·后端
金銀銅鐵1 小时前
[git] 如何丢弃对一个文件的改动?
git·后端
橘子海全栈攻城狮2 小时前
【最新源码】养老院系统管理A013
java·spring boot·后端·web安全·微信小程序
smallyoung2 小时前
具有反思能力的 Agentic RAG 实战:用 LangChain4j 实现 CRAG 纠错检索
人工智能·后端
EthanYuan2 小时前
💡RAG实践:从云知识库迁移到PostgreSQL ,并使用PGVector实现向量存储
后端
直奔標竿2 小时前
Java开发者AI转型第二十六课!Spring AI 个人知识库实战(五)——联网搜索增强实战
java·开发语言·人工智能·spring boot·后端·spring
等风来_shy3 小时前
如何写好一个 Skill
后端
ailab3 小时前
研发人员如何写好 AI 提示词:从“问问题”到“驱动研发闭环”
后端
ltl3 小时前
【大模型基础设施工程】25:大模型基础设施未来
后端
ltl3 小时前
【大模型基础设施工程】二十四:成本、合规与安全
后端