Docker部署Bitnami Redis Cluster遇哈希槽分配等问题求助
我正在使用Docker镜像bitnami/redis-cluster:6.2.7配置Redis集群,对应的docker-compose.yml配置如下:
version: '3.8' services: redis-node-0: image: bitnami/redis-cluster:6.2.7 volumes: - redis-node-data-0:/bitnami/redis/data environment: - ALLOW_EMPTY_PASSWORD=yes - REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-1: image: bitnami/redis-cluster:6.2.7 volumes: - redis-node-data-1:/bitnami/redis/data environment: - ALLOW_EMPTY_PASSWORD=yes - REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-2: image: bitnami/redis-cluster:6.2.7 volumes: - redis-node-data-2:/bitnami/redis/data ports: - 6379:6379 depends_on: - redis-node-0 - redis-node-1 environment: - ALLOW_EMPTY_PASSWORD=yes - REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 - REDIS_CLUSTER_REPLICAS=1 - REDIS_CLUSTER_CREATOR=yes volumes: redis-node-data-0: driver: local redis-node-data-1: driver: local redis-node-data-2: driver: local networks: default: name: local_network
Docker容器已正常运行,docker ps输出如下:
| CONTAINER ID | IMAGE | COMMAND | CREATED | STATUS | PORTS | NAMES |
|---|---|---|---|---|---|---|
| bea9a7c52eba | bitnami/redis-cluster:6.2.7 | "/opt/bitnami/script…" | 12分钟前 | 12分钟前 | 0.0.0.0:6379->6379/tcp | local-redis-node-2-1 |
| 63c08f1330e0 | bitnami/redis-cluster:6.2.7 | "/opt/bitnami/script…" | 12分钟前 | 12分钟前 | 6379/tcp | local-redis-node-1-1 |
| e1b163d75254 | bitnami/redis-cluster:6.2.7 | "/opt/bitnami/script…" | 12分钟前 | 12分钟前 | 6379/tcp | local-redis-node-0-1 |
我将local-redis-node-2-1设置为REDIS_CLUSTER_CREATOR,由它负责初始化集群,因此所有操作都在该节点执行。
进入容器执行docker exec -it local-redis-node-2-1 redis-cli后,执行set a 1时出现错误:(error) CLUSTERDOWN Hash slot not serve,cluster slots输出为(empty array)。
我尝试执行docker exec -it local-redis-node-2-1 redis-cli --cluster fix localhost:6379,但仅将16383个槽中的全部分配给local-redis-node-2-1,未分配给其他节点,执行后cluster slots输出如下:
127.0.0.1:6379> cluster slots 1) 1) (integer) 0 2) (integer) 16383 3) 1) "172.18.0.9" 2) (integer) 6379 3) "819770cf8b39793517efa10b9751083c854e15d7"
现寻求以下帮助:
- 如何通过docker-compose.yml自动设置集群哈希槽?
- 当前docker-compose.yml配置下,只读副本是否能正常工作,是否有遗漏配置?
- 解决哈希槽分配问题后,Redis集群能否正常运行?
各节点日志
local-redis-node-2-1日志
redis-cluster 00:47:45.70 redis-cluster 00:47:45.73 Welcome to the Bitnami redis-cluster container redis-cluster 00:47:45.76 Subscribe to project updates by watching https://github.com/bitnami/containers redis-cluster 00:47:45.78 Submit issues and feature requests at https://github.com/bitnami/containers/issues redis-cluster 00:47:45.80 redis-cluster 00:47:45.83 INFO ==> ** Starting Redis setup ** redis-cluster 00:47:46.00 WARN ==> You set the environment variable ALLOW_EMPTY_PASSWORD=yes. For safety reasons, do not use this flag in a production environment. redis-cluster 00:47:46.05 INFO ==> Initializing Redis redis-cluster 00:47:46.29 INFO ==> Setting Redis config file Changing old IP 172.18.0.8 by the new one 172.18.0.8 Changing old IP 172.18.0.6 by the new one 172.18.0.6 Changing old IP 172.18.0.9 by the new one 172.18.0.9 redis-cluster 00:47:47.30 INFO ==> ** Redis setup finished! ** 1:C 10 Dec 2022 00:47:47.579 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1:C 10 Dec 2022 00:47:47.580 # Redis version=6.2.7, bits=64, commit=00000000, modified=0, pid=1, just started 1:C 10 Dec 2022 00:47:47.580 # Configuration loaded 1:M 10 Dec 2022 00:47:47.584 * monotonic clock: POSIX clock_gettime 1:M 10 Dec 2022 00:47:47.588 * Node configuration loaded, I'm 819770cf8b39793517efa10b9751083c854e15d7 1:M 10 Dec 2022 00:47:47.595 # A key '__redis__compare_helper' was added to Lua globals which is not on the globals allow list nor listed on the deny list. 1:M 10 Dec 2022 00:47:47.598 * Running mode=cluster, port=6379. 1:M 10 Dec 2022 00:47:47.599 # Server initialized 1:M 10 Dec 2022 00:47:47.612 * Ready to accept connections 1:M 10 Dec 2022 00:47:49.673 # Cluster state changed: ok
local-redis-node-1-1日志
redis-cluster 00:47:45.43 redis-cluster 00:47:45.46 Welcome to the Bitnami redis-cluster container redis-cluster 00:47:45.48 Subscribe to project updates by watching https://github.com/bitnami/containers redis-cluster 00:47:45.51 Submit issues and feature requests at https://github.com/bitnami/containers/issues redis-cluster 00:47:45.54 redis-cluster 00:47:45.56 INFO ==> ** Starting Redis setup ** redis-cluster 00:47:45.73 WARN ==> You set the environment variable ALLOW_EMPTY_PASSWORD=yes. For safety reasons, do not use this flag in a production environment. redis-cluster 00:47:45.79 INFO ==> Initializing Redis redis-cluster 00:47:46.00 INFO ==> Setting Redis config file Changing old IP 172.18.0.8 by the new one 172.18.0.8 Changing old IP 172.18.0.6 by the new one 172.18.0.6 Changing old IP 172.18.0.9 by the new one 172.18.0.9 redis-cluster 00:47:47.10 INFO ==> ** Redis setup finished! ** 1:C 10 Dec 2022 00:47:47.387 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1:C 10 Dec 2022 00:47:47.388 # Redis version=6.2.7, bits=64, commit=00000000, modified=0, pid=1, just started 1:C 10 Dec 2022 00:47:47.388 # Configuration loaded 1:M 10 Dec 2022 00:47:47.392 * monotonic clock: POSIX clock_gettime 1:M 10 Dec 2022 00:47:47.395 * Node configuration loaded, I'm 2ab0b8db952cc101f7873cdcf8cf691f8f6bae7b 1:M 10 Dec 2022 00:47:47.403 # A key '__redis__compare_helper' was added to Lua globals which is not on the globals allow list nor listed on the deny list. 1:M 10 Dec 2022 00:47:47.406 * Running mode=cluster, port=6379. 1:M 10 Dec 2022 00:47:47.407 # Server initialized 1:M 10 Dec 2022 00:47:47.418 * Ready to accept connections 1:M 10 Dec 2022 00:56:02.716 # New configEpoch set to 1 1:M 10 Dec 2022 00:56:41.943 # Cluster state changed: ok
local-redis-node-0-1日志
redis-cluster 00:47:45.43 redis-cluster 00:47:45.46 Welcome to the Bitnami redis-cluster container redis-cluster 00:47:45.49 Subscribe to project updates by watching https://github.com/bitnami/containers redis-cluster 00:47:45.51 Submit issues and feature requests at https://github.com/bitnami/containers/issues redis-cluster 00:47:45.54 redis-cluster 00:47:45.56 INFO ==> ** Starting Redis setup ** redis-cluster 00:47:45.73 WARN ==> You set the environment variable ALLOW_EMPTY_PASSWORD=yes. For safety reasons, do not use this flag in a production environment. redis-cluster 00:47:45.79 INFO ==> Initializing Redis redis-cluster 00:47:46.00 INFO ==> Setting Redis config file Changing old IP 172.18.0.8 by the new one 172.18.0.8 Changing old IP 172.18.0.6 by the new one 172.18.0.6 Changing old IP 172.18.0.9 by the new one 172.18.0.9 redis-cluster 00:47:47.11 INFO ==> ** Redis setup finished! ** 1:C 10 Dec 2022 00:47:47.387 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1:C 10 Dec 2022 00:47:47.388 # Redis version=6.2.7, bits=64, commit=00000000, modified=0, pid=1, just started 1:C 10 Dec 2022 00:47:47.388 # Configuration loaded 1:M 10 Dec 2022 00:47:47.391 * monotonic clock: POSIX clock_gettime 1:M 10 Dec 2022 00:47:47.395 * Node configuration loaded, I'm 5ffeca48faa750a5f47c76639598fdb9b7b8b720 1:M 10 Dec 2022 00:47:47.402 # A key '__redis__compare_helper' was added to Lua globals which is not on the globals allow list nor listed on the deny list. 1:M 10 Dec 2022 00:47:47.405 * Running mode=cluster, port=6379. 1:M 10 Dec 2022 00:47:47.405 # Server initialized 1:M 10 Dec 2022 00:47:47.415 * Ready to accept connections
1. 如何通过docker-compose.yml自动设置集群哈希槽?
你的配置里虽然设置了REDIS_CLUSTER_CREATOR=yes和REDIS_CLUSTER_REPLICAS=1,但Bitnami的Redis Cluster镜像需要满足至少3个主节点才能自动完成哈希槽分配(Redis集群要求主节点数量≥3才能保证高可用和槽均分)。你现在只有3个节点,但设置了REDIS_CLUSTER_REPLICAS=1,意味着需要(主节点数)*(副本数+1)=3*2=6个节点,当前节点数量不足,导致创建者节点无法自动完成槽分配。
修改方案:
- 若要使用3个主节点(无副本),将
REDIS_CLUSTER_REPLICAS设为0,同时确保所有节点的REDIS_NODES正确配置,且创建者节点的REDIS_CLUSTER_CREATOR=yes。 - 若要保留1个副本,需要扩展到6个节点(3主3从),在docker-compose.yml中添加3个从节点服务,每个从节点的
REDIS_NODES包含所有6个节点,镜像会自动分配主从关系。
如果不想调整节点数量,可在创建者节点手动执行槽分配命令:
docker exec -it local-redis-node-2-1 redis-cli --cluster create \ redis-node-0:6379 redis-node-1:6379 redis-node-2:6379 \ --cluster-replicas 0
执行时输入yes确认分配,即可将16384个槽均分给3个主节点。
2. 当前docker-compose.yml配置下,只读副本是否能正常工作,是否有遗漏配置?
当前配置无法正常工作,问题在于:
REDIS_CLUSTER_REPLICAS=1要求每个主节点对应1个副本,但你只有3个节点,不足以满足3主3从的结构,镜像无法自动分配副本关系。- 非创建者节点没有指定主节点,Bitnami镜像在节点数不足时无法自动完成主从关联。
如果要配置副本,需要:
- 扩展到6个节点,添加3个从节点服务,示例:
redis-node-3: image: bitnami/redis-cluster:6.2.7 volumes: - redis-node-data-3:/bitnami/redis/data environment: - ALLOW_EMPTY_PASSWORD=yes - REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5
(同理添加redis-node-4、redis-node-5,

