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
相关推荐
MetaLite1 小时前
SpringBoot整合FastJson2数据脱敏-接口日志与失败降级
java·spring boot·后端
xixingzhe21 小时前
SpringBoot 接口缓存实现
spring boot·后端·缓存
名字还没想好☜2 小时前
Go 时间格式化为什么用 2006-01-02:time.Format/Parse 的参考时间、时区与解析踩坑
开发语言·后端·golang·go
jyOverQ5 小时前
RabbitMQ 消息积压怎么办?Prefetch、消费者并发与扩容
分布式·后端·rabbitmq
东风破_12 小时前
从跨域到 WebSocket:前端跨域方案、SSE 与双向实时通信详解
前端·后端
行百里er13 小时前
Gateway 上别硬塞 MVC:DeferredImportSelector 看菜下锅
spring boot·后端·开源
名字还没想好☜14 小时前
Java 遍历时删元素抛 ConcurrentModificationException:fail-fast 原理与三种正确删法
后端
IT_陈寒14 小时前
React Hooks闭包陷阱差点让我加班到凌晨
前端·人工智能·后端
码事漫谈15 小时前
本体论,彻底懂
后端
创新技术阁15 小时前
FastapiAdmin 系统日志体系与核心配置参数详解
前端·后端·fastapi