一、Redis主从复制(Replication)基础
1. 核心作用
- 数据冗余:主节点数据实时同步到从节点,实现数据备份
- 读写分离:主节点处理写请求,从节点处理读请求(提升读性能)
- 故障转移基础:为哨兵模式提供节点状态监控的基础架构
![图片[1]_Redis主从+哨兵集群部署详解_知途无界](https://zhituwujie.com/wp-content/uploads/2025/10/d2b5ca33bd20251010093437.png)
2. 数据同步原理
- 全量同步(首次连接或数据丢失时):
- 从节点发送
PSYNC ? -1请求 - 主节点生成 RDB 快照并发送给从节点
- 主节点缓存期间的写命令(buffer)
- 从节点加载 RDB 后,接收并执行缓存命令
- 从节点发送
- 增量同步(正常运行时):
- 主节点通过
replication feed发送写命令 - 从节点按顺序执行命令保持数据一致
- 主节点通过
3. 部署拓扑示例(1主2从)
主节点(Master): 192.168.1.10:6379
从节点1(Slave1): 192.168.1.11:6379
从节点2(Slave2): 192.168.1.12:6379
二、主从复制部署步骤
1. 环境准备(以3台服务器为例)
| 节点 | IP地址 | 角色 | Redis端口 |
|---|---|---|---|
| 主节点 | 192.168.1.10 | Master | 6379 |
| 从节点1 | 192.168.1.11 | Slave | 6379 |
| 从节点2 | 192.168.1.12 | Slave | 6379 |
2. 主节点配置(redis.conf)
# 基础配置
bind 0.0.0.0
port 6379
daemonize yes
pidfile /var/run/redis_6379.pid
logfile "/var/log/redis_6379.log"
dir /data/redis/data
# 主从相关(默认配置无需修改)
# replica-serve-stale-data yes # 从节点可提供过期数据
# replica-read-only yes # 从节点只读(建议开启)
3. 从节点配置(关键参数)
# 基础配置(同主节点)
bind 0.0.0.0
port 6379
daemonize yes
...
# 核心从节点配置
replicaof 192.168.1.10 6379 # Redis 5.0+ 语法(旧版用 slaveof)
replica-read-only yes # 强制从节点只读
# replica-priority 100 # 哨兵选举优先级(默认100,值越小优先级越高)
4. 启动与验证
# 所有节点启动Redis
redis-server /path/to/redis.conf
# 主节点查看角色
redis-cli -h 192.168.1.10 -p 6379 info replication
# 输出应包含:role:master, connected_slaves:2
# 从节点验证数据同步
redis-cli -h 192.168.1.11 -p 6379 info replication
# 输出应包含:role:slave, master_host:192.168.1.10, master_port:6379
5. 数据同步测试
# 在主节点写入数据
redis-cli -h 192.168.1.10 -p 6379 set test_key "hello_redis"
# 在从节点读取验证
redis-cli -h 192.168.1.11 -p 6379 get test_key # 应返回 "hello_redis"
三、哨兵模式(Sentinel)部署
1. 核心功能
- 自动故障检测:持续监控主从节点健康状态
- 自动故障转移:主节点宕机时自动选举新主节点
- 配置中心:动态更新客户端连接信息
2. 哨兵集群拓扑(推荐3节点)
| 哨兵节点 | IP地址 | 端口 |
|---|---|---|
| Sentinel1 | 192.168.1.10 | 26379 |
| Sentinel2 | 192.168.1.11 | 26379 |
| Sentinel3 | 192.168.1.12 | 26379 |
3. 哨兵配置文件(sentinel.conf)
# 基础配置
port 26379
daemonize yes
pidfile /var/run/redis-sentinel_26379.pid
logfile "/var/log/redis-sentinel_26379.log"
dir /data/redis/sentinel
# 核心监控配置
sentinel monitor mymaster 192.168.1.10 6379 2
# 参数说明:
# mymaster - 自定义主节点名称(集群唯一标识)
# 192.168.1.10 6379 - 主节点IP和端口
# 2 - 判定主节点客观下线的哨兵投票数(建议为哨兵总数/2 + 1)
# 超时与故障转移参数
sentinel down-after-milliseconds mymaster 5000 # 5秒无响应判定主观下线
sentinel failover-timeout mymaster 60000 # 故障转移超时时间(毫秒)
sentinel parallel-syncs mymaster 1 # 故障转移后同时同步的从节点数
# 保护模式(生产环境建议关闭)
protected-mode no
4. 关键参数详解
| 参数 | 作用 | 推荐值 |
|---|---|---|
sentinel monitor | 定义监控的主节点 | 必须配置 |
down-after-milliseconds | 主观下线判定时间 | 5000-10000ms |
quorum (monitor第三个参数) | 客观下线投票数 | 哨兵数/2 + 1 |
failover-timeout | 故障转移超时 | 30000-60000ms |
parallel-syncs | 新主节点同步从节点数 | 1(避免主节点压力过大) |
5. 启动哨兵集群
# 在所有哨兵节点启动
redis-sentinel /path/to/sentinel.conf
# 或使用redis-server启动(兼容模式)
redis-server /path/to/sentinel.conf --sentinel
6. 验证哨兵状态
# 查看哨兵监控信息
redis-cli -p 26379 sentinel master mymaster
# 关键输出:
# name: mymaster
# ip: 192.168.1.10
# port: 6379
# slaves: 2
# sentinels: 3
# status: ok
# 查看所有哨兵节点
redis-cli -p 26379 sentinel sentinels mymaster
# 模拟主节点故障(测试故障转移)
# 1. 停止主节点Redis服务
# 2. 观察哨兵日志(约5-10秒后开始故障转移)
# 3. 重新查看主节点信息(原从节点晋升为主节点)
四、完整架构部署流程(主从+哨兵)
1. 架构拓扑示例
主节点(Master): 192.168.1.10:6379
从节点1(Slave1): 192.168.1.11:6379
从节点2(Slave2): 192.168.1.12:6379
哨兵1: 192.168.1.10:26379
哨兵2: 192.168.1.11:26379
哨兵3: 192.168.1.12:26379
2. 部署步骤
- 部署主从集群(按前述步骤配置主从复制)
- 部署哨兵集群(每个节点部署sentinel.conf)
- 配置互信(确保哨兵能访问所有Redis节点)
- 验证高可用:
- 手动停止主节点,观察哨兵自动选举新主节点
- 恢复原主节点,观察其自动变为从节点
3. 客户端连接方式
# Python示例(使用redis-py)
from redis.sentinel import Sentinel
sentinel = Sentinel(
[('192.168.1.10', 26379),
('192.168.1.11', 26379),
('192.168.1.12', 26379)],
socket_timeout=0.5,
sentinel_kwargs={'password': 'your_sentinel_password'}
)
# 获取当前主节点
master = sentinel.master_for('mymaster', socket_timeout=0.5)
master.set('foo', 'bar')
# 获取从节点(读操作)
slave = sentinel.slave_for('mymaster', socket_timeout=0.5)
print(slave.get('foo'))
五、常见问题与优化
1. 数据同步延迟
- 现象:从节点数据落后于主节点
- 解决方案:
- 优化网络带宽(主从节点间建议千兆网络)
- 减少大Key和批量操作
- 监控
repl_offset差异(info replication)
2. 哨兵脑裂问题
- 风险:网络分区导致多个主节点同时存在
- 防护措施:
# 在哨兵配置中添加 sentinel min-slaves-to-write 1 # 至少1个从节点正常才允许写 sentinel min-slaves-max-lag 10 # 从节点延迟不超过10秒
3. 故障转移优化
- 参数调优:
sentinel down-after-milliseconds mymaster 3000 # 更快的故障检测 sentinel failover-timeout mymaster 30000 # 缩短转移超时
4. 生产环境建议
- 监控告警:集成Prometheus + Grafana监控Redis和哨兵状态
- 多机房部署:跨机房部署时注意网络延迟和分区风险
- 定期演练:模拟主节点故障测试自动转移流程
六、架构演进建议
对于更高要求的场景,可升级到 Redis Cluster(官方分布式方案):
- 支持数据分片(16384个槽位)
- 自动故障转移 + 客户端路由
- 无中心化架构(对比哨兵更健壮)
但主从+哨兵模式仍是中小规模应用的经典高可用方案,具备部署简单、运维成本低的优势。
© 版权声明
文中内容均来源于公开资料,受限于信息的时效性和复杂性,可能存在误差或遗漏。我们已尽力确保内容的准确性,但对于因信息变更或错误导致的任何后果,本站不承担任何责任。如需引用本文内容,请注明出处并尊重原作者的版权。
THE END

























暂无评论内容