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随机淘汰所有key5. 日志配置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_PASSWORDYourStr0ng!Pass restart: unless-stopped healthcheck: test: [CMD, redis-cli, -a, YourStr0ng!Pass, ping] interval: 10s timeout: 5s retries: 3Node2/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_PASSWORDYourStr0ng!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-masterNode2Slave172.20.3.12redis-slaveNode3Slave172.20.3.13redis-slaveRedis 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 replicationQ2: 内存满导致写入失败检查内存使用INFO memory调整maxmemory配置确认淘汰策略生效Q3: 从节点只读这是正常行为replica-read-only yes如需写入临时修改CONFIG SET replica-read-only no下一步09-Sentinel配置详解.md - 了解Sentinel配置12-验证测试.md - 验证Redis集群