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 | 容器名 |
|---|---|---|---|
| Node1 | Master | 172.20.3.11 | redis-master |
| Node2 | Slave | 172.20.3.12 | redis-slave |
| Node3 | Slave | 172.20.3.13 | redis-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集群
