当前位置: 首页 > news >正文

Docker容器化高可用架构部署方案(九)

08-Redis配置详解

本文档详细介绍Redis的配置,包括主节点和从节点的配置参数说明。

Redis架构

┌─────────────────────────────────────────────────────────┐ │ Redis集群架构 │ ├─────────────────────────────────────────────────────────┤ │ │ │ ┌──────────────┐ │ │ │ Redis Master │ 172.20.3.11 │ │ │ (Node1) │◄────────┐ │ │ └──────┬───────┘ │ │ │ │ │ │ │ ┌──────┴───────┐ ┌──────┴───────┐ │ │ │ Redis Slave │ │ Redis Slave │ │ │ │ (Node2) │ │ (Node3) │ │ │ │ 172.20.3.12 │ │ 172.20.3.13 │ │ │ └──────────────┘ └──────────────┘ │ │ │ │ ┌──────┬───────┐ ┌──────┬───────┐ ┌──────┬───────┐ │ │ │ Sentinel-01 │ │ Sentinel-02 │ │ Sentinel-03 │ │ │ │ 172.20.3.31 │ │ 172.20.3.32 │ │ 172.20.3.33 │ │ │ └──────────────┘ └──────────────┘ └──────────────┘ │ └─────────────────────────────────────────────────────────┘

主节点配置 (redis-master.conf)

cat > /opt/cluster-deploy/config/redis/redis-master.conf << 'EOF' bind 0.0.0.0 port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize no supervised no pidfile /var/run/redis/redis-server.pid loglevel notice logfile "" databases 16 always-show-logo no ​ save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir /data replica-serve-stale-data yes replica-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no replica-priority 100 ​ maxmemory 256mb maxmemory-policy allkeys-lru maxmemory-samples 5 ​ lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no replica-lazy-flush no ​ appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes aof-use-rdb-preamble yes ​ lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 ​ notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 stream-node-max-bytes 4096 stream-node-max-entries 100 ​ client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 ​ hz 10 dynamic-hz yes aof-rewrite-incremental-fsync yes rdb-save-incremental-fsync yes EOF

从节点配置 (redis-slave.conf)

从节点配置与主节点基本相同,但通过启动参数指定主节点:

cat > /opt/cluster-deploy/config/redis/redis-slave.conf << 'EOF' bind 0.0.0.0 port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize no supervised no pidfile /var/run/redis/redis-server.pid loglevel notice logfile "" databases 16 always-show-logo no ​ save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir /data replica-serve-stale-data yes replica-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no replica-priority 100 ​ maxmemory 256mb maxmemory-policy allkeys-lru maxmemory-samples 5 ​ lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no replica-lazy-flush no ​ appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes aof-use-rdb-preamble yes ​ lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 ​ notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 stream-node-max-bytes 4096 stream-node-max-entries 100 ​ client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 ​ hz 10 dynamic-hz yes aof-rewrite-incremental-fsync yes rdb-save-incremental-fsync yes EOF

关键配置项详解

1. 网络配置

bind 0.0.0.0 # 监听所有接口 port 6379 # 端口 tcp-backlog 511 # TCP积压队列 timeout 0 # 客户端空闲超时(0表示禁用) tcp-keepalive 300 # TCP保活检测间隔

2. 持久化配置

save 900 1 # 900秒内1次写操作则保存 save 300 10 # 300秒内10次写操作则保存 save 60 10000 # 60秒内10000次写操作则保存 stop-writes-on-bgsave-error yes # bgsave失败时停止写入 rdbcompression yes # RDB文件压缩 rdbchecksum yes # RDB文件校验 dbfilename dump.rdb # RDB文件名 dir /data # 数据目录

3. 主从复制配置

replica-serve-stale-data yes # 主节点失联时仍提供数据 replica-read-only yes # 从节点只读 replica-priority 100 # 优先级(影响Sentinel选举)

注意

  • 从节点的replicaof参数通过docker-compose启动参数指定

  • 优先级:Master(100) > Slave1(100) > Slave2(100),可通过调整实现主从切换

4. 内存管理

maxmemory 256mb # 最大内存 maxmemory-policy allkeys-lru # 内存满时的淘汰策略 maxmemory-samples 5 # LRU采样数

淘汰策略

策略说明
noeviction不淘汰,返回错误
allkeys-lru所有key使用LRU淘汰
volatile-lru仅过期key使用LRU淘汰
allkeys-random随机淘汰所有key

5. 日志配置

loglevel notice logfile ""

重要排错经验:Redis 7.x中active-rehashing已废弃,删除即可。

重要排错经验:容器中非root用户无法写日志文件,使用logfile ""输出到stdout。

Docker Compose配置

Node1 (Master)

redis-master: image: redis:7-alpine container_name: redis-master networks: cache-net: ipv4_address: 172.20.3.11 command: redis-server /etc/redis/redis.conf volumes: - redis-master-data:/data - ./config/redis/redis-master.conf:/etc/redis/redis.conf:ro environment: - REDIS_PASSWORD=YourStr0ng!Pass restart: unless-stopped healthcheck: test: ["CMD", "redis-cli", "-a", "YourStr0ng!Pass", "ping"] interval: 10s timeout: 5s retries: 3

Node2/Node3 (Slave)

redis-slave: image: redis:7-alpine container_name: redis-slave networks: cache-net: ipv4_address: 172.20.3.12 # Node2 # 172.20.3.13 (Node3) command: redis-server /etc/redis/redis.conf --replicaof 172.20.3.11 6379 volumes: - redis-slave-data:/data - ./config/redis/redis-slave.conf:/etc/redis/redis.conf:ro environment: - REDIS_PASSWORD=YourStr0ng!Pass restart: unless-stopped healthcheck: test: ["CMD", "redis-cli", "-a", "YourStr0ng!Pass", "ping"] interval: 10s timeout: 5s retries: 3

服务IP分配

节点角色IP容器名
Node1Master172.20.3.11redis-master
Node2Slave172.20.3.12redis-slave
Node3Slave172.20.3.13redis-slave

Redis 7.x兼容性注意事项

废弃的参数

以下参数在Redis 7.x中已废弃,使用会报错:

# 已废弃,删除 # active-rehashing yes

推荐配置

# 使用默认值即可,Redis 7.x已自动启用 # active-rehashing 参数已删除

验证命令

# 查看Redis容器 docker ps | grep redis # 测试连接 docker exec redis-master redis-cli -a 'YourStr0ng!Pass' ping docker exec redis-slave redis-cli -a 'YourStr0ng!Pass' -h 172.20.3.11 ping # 查看复制状态 docker exec redis-master redis-cli -a 'YourStr0ng!Pass' info replication # 查看从节点列表 docker exec redis-master redis-cli -a 'YourStr0ng!Pass' client list # 测试写入 docker exec redis-master redis-cli -a 'YourStr0ng!Pass' set test_key "hello" docker exec redis-slave redis-cli -a 'YourStr0ng!Pass' get test_key # 查看慢日志 docker exec redis-master redis-cli -a 'YourStr0ng!Pass' slowlog get 10 # 查看内存使用 docker exec redis-master redis-cli -a 'YourStr0ng!Pass' info memory

常见问题

Q1: 主从复制不工作

  • 检查网络连通性:ping 172.20.3.11

  • 检查防火墙:端口6379是否开放

  • 查看复制状态:INFO replication

Q2: 内存满导致写入失败

  • 检查内存使用:INFO memory

  • 调整maxmemory配置

  • 确认淘汰策略生效

Q3: 从节点只读

  • 这是正常行为,replica-read-only yes

  • 如需写入,临时修改:CONFIG SET replica-read-only no

下一步

  • 09-Sentinel配置详解.md - 了解Sentinel配置

  • 12-验证测试.md - 验证Redis集群

http://www.jsqmd.com/news/819369/

相关文章:

  • 基于MCP协议与微软Graph API构建安全可控的AI助手Outlook集成方案
  • ARM架构CPTR寄存器解析:虚拟化与安全控制
  • 知识入库:从文档加载到文本拆分
  • 运维系列【仅供参考】:彻底清除TortoiseSVN:从基础卸载到深度清理全指南
  • 杰理之先开广播再切换SPDIF光纤输入,会打印‘a’,无法播放和广播【篇】
  • 【权威实测报告】:对比12种生成场景下的真实Cost/Img,Midjourney API性价比跌破临界点?
  • AI驱动代码库优化:基于Claude Code的上下文工程与自动化重构实践
  • Copaw:专为算法竞赛设计的本地自动化测试与调试工具
  • CircuitPython库管理实战:从零构建嵌入式开发环境
  • 2026年AI学习指南:收藏这份靠谱进阶路径,轻松拉开差距!
  • 【shell编程知识点汇总】第九章 HTML 清洗、多行合并与条件替换
  • 说说Markdown为什么不会被HTML取代
  • KMS_VL_ALL_AIO:智能激活解决方案完全解析
  • 第6章:C++ Sanitizer全家桶实战
  • day22_深度学习入门与pytorch
  • 程序员的副业天花板:靠接私活实现年入百万的秘诀
  • AI智能体技能库开发指南:从原理到实战构建高效Agent应用
  • 苍穹外卖开发日记-微信登录
  • 2026年5月更新:美甲产业升级,甲片专用机定制厂家遴选全攻略 - 2026年企业推荐榜
  • PKSM终极指南:从菜鸟到宝可梦存档管理大师的完整路径
  • Dify插件重打包工具:标准化分发与一键部署实践
  • SPI长距离通信的时钟同步与信号完整性优化
  • 从零上手VibeCoding(ClaudeCode+DeepSeek V4.Pro)
  • 0. 深度学习课程大纲:
  • Redis 身份迷失
  • 从“边缘人”到香饽饽:35岁程序员的开源逆袭路
  • 《我的世界》Java版客户端模组开发:基于freedom-for-steve框架的底层定制实践
  • 【ElevenLabs有声书制作黄金法则】:20年音频工程师亲授,零基础7天交付商用级有声书
  • Node 版本升级后 Electron 原生模块编译失败怎么解决
  • AI工程化实战:从模型到服务的全链路部署与优化指南