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
相关推荐
VX:Fegn08953 分钟前
计算机毕业设计|基于springboot + vue乡村振兴服务系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
何中应23 分钟前
Bean的三种注入方式
开发语言·spring boot·后端·spring
席万里31 分钟前
基于Flask框架实现的一个在线考试系统
后端·python·flask
王中阳Go1 小时前
12 Go Eino AI应用开发实战 | 消息队列架构
人工智能·后端·go
沐森1 小时前
Rust 的CPU和IO操作
后端
Lucky_Turtle1 小时前
【Springboot】解决PageHelper在实体转Vo下出现total数据问题
java·spring boot·后端
無量1 小时前
AI工程化实践指南:从入门到落地
后端·ai编程
golang学习记1 小时前
Jetbrains 这个知名软件十年了!
后端
老华带你飞1 小时前
志愿者服务管理|基于springboot 志愿者服务管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·spring
知其然亦知其所以然1 小时前
程序员的最强外挂:用 Spring AI 解锁智谱 AI 画图能力
后端·spring·程序员