目标
- 使用
host 网络模式;
- 增加 Redis 密码认证(
requirepass / masterauth);
- 开启 RDB + AOF 双重持久化机制;
- 把持久化文件(
appendonly.aof, dump.rdb, nodes.conf 等)保存在宿主机挂载目录中,确保数据持久。
1.目录结构
redis-cluster/
├── docker-compose.yml
├── nodes/
│ ├── 7001/redis.conf
│ ├── ...
├── data/
│ ├── 7001/
│ ├── ...
每个 Redis 实例挂载独立的持久化目录到 /data,Redis 会把 AOF、RDB、nodes.conf 等文件放进来。
2.docker-compose.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| services: redis-7001: image: redis:7.0.12 container_name: redis-7001 restart: always network_mode: host volumes: - ./data/7001:/data - ./nodes/7001/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
redis-7002: image: redis:7.0.12 container_name: redis-7002 restart: always network_mode: host volumes: - ./data/7002:/data - ./nodes/7002/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
redis-7003: image: redis:7.0.12 container_name: redis-7003 restart: always network_mode: host volumes: - ./data/7003:/data - ./nodes/7003/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
redis-7004: image: redis:7.0.12 container_name: redis-7004 restart: always network_mode: host volumes: - ./data/7004:/data - ./nodes/7004/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
redis-7005: image: redis:7.0.12 container_name: redis-7005 restart: always network_mode: host volumes: - ./data/7005:/data - ./nodes/7005/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
redis-7006: image: redis:7.0.12 container_name: redis-7006 restart: always network_mode: host volumes: - ./data/7006:/data - ./nodes/7006/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
|
3.redis.conf 示例(nodes/7001/redis.conf)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| port 7001 bind 0.0.0.0 protected-mode no
cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 cluster-announce-ip 127.0.0.1 cluster-announce-port 7001 cluster-announce-bus-port 17001
requirepass YourStrongPassword masterauth YourStrongPassword
appendonly yes appendfilename "appendonly.aof" appendfsync everysec
save 900 1 save 300 10 save 60 10000 dbfilename dump.rdb
dir /data
|
⚠️ 注意:dir /data 这行非常关键,Redis 会在此目录下生成 appendonly.aof、dump.rdb、nodes.conf,确保该目录在 docker-compose 中有挂载。
4.初始化集群
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #!/bin/bash
docker-compose up -d
echo "等待 Redis 节点启动..." sleep 5
echo "创建 Redis 集群(带认证)..." yes yes | docker run -i --rm --net=host redis:7.0.12 \ redis-cli --cluster create \ 192.168.0.119:7001 \ 192.168.0.119:7002 \ 192.168.0.119:7003 \ 192.168.0.119:7004 \ 192.168.0.119:7005 \ 192.168.0.119:7006 \ --cluster-replicas 1 \ -a xxx
|
5.数据验证
创建数据:
1
| redis-cli -a YourStrongPassword -p 7001 set test123 hello
|
查看宿主机的持久化文件: