下面是一个用于搭建 Redis 集群的 docker-compose.yml 示例文件,它会启动 6 个 Redis 节点(3 主节点 + 3 从节点)来构成一个最小的 Redis 集群。
同一个容器内网通讯没问题,但是你要是需要暴露到外网你需要用第二个yml
内网的
yml
version: '3.8'
services:
redis-node1:
image: redis:7.0
container_name: redis-node1
command: ["redis-server", "--port", "6379", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes", "--cluster-announce-ip", "redis-node1", "--cluster-announce-port", "6379", "--cluster-announce-bus-port", "16379"]
ports:
- "6379:6379"
- "16379:16379"
volumes:
- ./data/redis-node1:/data
networks:
- redis-cluster
redis-node2:
image: redis:7.0
container_name: redis-node2
command: ["redis-server", "--port", "6379", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes", "--cluster-announce-ip", "redis-node2", "--cluster-announce-port", "6379", "--cluster-announce-bus-port", "16379"]
ports:
- "6380:6379"
- "16380:16379"
volumes:
- ./data/redis-node2:/data
networks:
- redis-cluster
redis-node3:
image: redis:7.0
container_name: redis-node3
command: ["redis-server", "--port", "6379", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes", "--cluster-announce-ip", "redis-node3", "--cluster-announce-port", "6379", "--cluster-announce-bus-port", "16379"]
ports:
- "6381:6379"
- "16381:16379"
volumes:
- ./data/redis-node3:/data
networks:
- redis-cluster
redis-node4:
image: redis:7.0
container_name: redis-node4
command: ["redis-server", "--port", "6379", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes", "--cluster-announce-ip", "redis-node4", "--cluster-announce-port", "6379", "--cluster-announce-bus-port", "16379"]
ports:
- "6382:6379"
- "16382:16379"
volumes:
- ./data/redis-node4:/data
networks:
- redis-cluster
redis-node5:
image: redis:7.0
container_name: redis-node5
command: ["redis-server", "--port", "6379", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes", "--cluster-announce-ip", "redis-node5", "--cluster-announce-port", "6379", "--cluster-announce-bus-port", "16379"]
ports:
- "6383:6379"
- "16383:16379"
volumes:
- ./data/redis-node5:/data
networks:
- redis-cluster
redis-node6:
image: redis:7.0
container_name: redis-node6
command: ["redis-server", "--port", "6379", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes", "--cluster-announce-ip", "redis-node6", "--cluster-announce-port", "6379", "--cluster-announce-bus-port", "16379"]
ports:
- "6384:6379"
- "16384:16379"
volumes:
- ./data/redis-node6:/data
networks:
- redis-cluster
networks:
redis-cluster:
driver: bridge
外网的
yml
version: '3.3'
services:
redis-node1:
image: redis
container_name: redis-node1
command: ["redis-server", "--port", "6379", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes", "--cluster-announce-ip", "你的外网ip或者域名", "--cluster-announce-port", "6379", "--cluster-announce-bus-port", "16379"]
ports:
- "26379:6379"
- "16379:16379"
volumes:
- ./data/redis-node1:/data
networks:
- redis-cluster-compose
redis-node2:
image: redis
container_name: redis-node2
command: ["redis-server", "--port", "6379", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes", "--cluster-announce-ip", "你的外网ip或者域名", "--cluster-announce-port", "6380", "--cluster-announce-bus-port", "16380"]
ports:
- "26380:6379"
- "16380:16379"
volumes:
- ./data/redis-node2:/data
networks:
- redis-cluster-compose
redis-node3:
image: redis
container_name: redis-node3
command: ["redis-server", "--port", "6379", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes", "--cluster-announce-ip", "你的外网ip或者域名", "--cluster-announce-port", "6381", "--cluster-announce-bus-port", "16381"]
ports:
- "26381:6379"
- "16381:16379"
volumes:
- ./data/redis-node3:/data
networks:
- redis-cluster-compose
redis-node4:
image: redis
container_name: redis-node4
command: ["redis-server", "--port", "6379", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes", "--cluster-announce-ip", "你的外网ip或者域名", "--cluster-announce-port", "6382", "--cluster-announce-bus-port", "16382"]
ports:
- "26382:6379"
- "16382:16379"
volumes:
- ./data/redis-node4:/data
networks:
- redis-cluster-compose
redis-node5:
image: redis
container_name: redis-node5
command: ["redis-server", "--port", "6379", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes", "--cluster-announce-ip", "你的外网ip或者域名", "--cluster-announce-port", "6383", "--cluster-announce-bus-port", "16383"]
ports:
- "26383:6379"
- "16383:16379"
volumes:
- ./data/redis-node5:/data
networks:
- redis-cluster-compose
redis-node6:
image: redis
container_name: redis-node6
command: ["redis-server", "--port", "6379", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes", "--cluster-announce-ip", "你的外网ip或者域名", "--cluster-announce-port", "6384", "--cluster-announce-bus-port", "16384"]
ports:
- "26384:6379"
- "16384:16379"
volumes:
- ./data/redis-node6:/data
networks:
- redis-cluster-compose
networks:
redis-cluster-compose:
driver: bridge
使用步骤:
- 创建目录:
在你想放置 docker-compose.yml 文件的目录下创建数据目录,例如:
bash
mkdir -p ./data/redis-node1 ./data/redis-node2 ./data/redis-node3 ./data/redis-node4 ./data/redis-node5 ./data/redis-node6
- 启动集群:
在 docker-compose.yml 文件所在的目录中运行:
bash
docker-compose up -d
- 初始化 Redis 集群: 启动所有 Redis 容器后,你需要使用 redis-cli 创建集群:
bash
docker exec -it redis-node1 redis-cli --cluster create \
redis-node1:6379 redis-node2:6379 redis-node3:6379 \
redis-node4:6379 redis-node5:6379 redis-node6:6379 \
--cluster-replicas 1
- 验证 Redis 集群:
你可以通过以下命令来验证 Redis 集群是否成功创建:
bash
docker exec -it redis-node1 redis-cli -c cluster nodes
这会列出集群中的所有节点及其状态。
搭建中遇到的问题
Node redis-node2:6379 replied with error:
ERR Invalid node address specified: redis-node1:6379
问题的原因可能是 redis-cli --cluster create 命令中的节点地址 redis-node1:6379 这样的主机名在 Redis 集群内部可能无法被解析 。这种情况通常发生在 Docker 容器中,因为每个容器有自己的网络配置,而 Redis 集群要求节点间能通过 IP 地址相互通信。
要解决这个问题,可以尝试以下步骤:
**解决方法:**替换成容器的内网ip就行了
获取IP方法
bash
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis-node1
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis-node2
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis-node3
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis-node4
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis-node5
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis-node6
重要提示:
- Redis 集群使用多个节点,因此你需要确保每个节点的端口(如 6379, 6380, 6381 等)在宿主机上可用。
- 如果需要持久化数据,确保在 ./data 目录下有写权限,并且这些目录可以正确映射到容器中的 /data 路径。