在Docker Swarm中部署多节点ZooKeeper遇到问题求助
Fixing Your 2-Node ZooKeeper Cluster on Docker Swarm
Let's break down the issues in your current setup and get your ZooKeeper nodes talking to each other properly. I've worked through similar Swarm/ZooKeeper configs before, so here's what needs fixing:
Key Issues in Your Current Config
- Incorrect
ZOO_SERVERSvalues: Each node'sZOO_SERVERSmust list all cluster members, not just a partial set with0.0.0.0. In an overlay network, ZooKeeper uses container hostnames for inter-node communication, so you need to reference each node by its service hostname. - Missing Swarm deploy configurations: Since you're using Docker Swarm, each service needs a
deploysection to ensure Swarm manages the containers properly (even for single-replica services like each ZooKeeper node). - Optional: Cluster timeout tuning: Adding ZooKeeper's init/sync limits helps account for slower container startup times in Swarm environments.
Corrected docker-compose.yml
version: '3.8' services: zoo1: image: zookeeper restart: unless-stopped hostname: zoo1 ports: - "2181:2181" environment: ZOO_MY_ID: 1 # List all cluster nodes explicitly ZOO_SERVERS: server.1=zoo1:2888:3888;server.2=zoo2:2888:3888 # Give cluster time to form on startup ZOO_INIT_LIMIT: 5 ZOO_SYNC_LIMIT: 2 volumes: - ./full-stack/zoo1/data:/data - ./full-stack/zoo1/datalog:/datalog deploy: replicas: 1 placement: constraints: [node.role == manager] # Keep on manager for initial testing networks: - kafka_network zoo2: image: zookeeper restart: unless-stopped hostname: zoo2 ports: - "2182:2181" environment: ZOO_MY_ID: 2 # Match the server list from zoo1 ZOO_SERVERS: server.1=zoo1:2888:3888;server.2=zoo2:2888:3888 ZOO_INIT_LIMIT: 5 ZOO_SYNC_LIMIT: 2 volumes: - ./full-stack/zoo2/data:/data - ./full-stack/zoo2/datalog:/datalog deploy: replicas: 1 placement: constraints: [node.role == manager] networks: - kafka_network visualizer: image: dockersamples/visualizer:stable volumes: - "/var/run/docker.sock:/var/run/docker.sock" ports: - "8080:8080" deploy: placement: constraints: [node.role == manager] networks: - kafka_network networks: kafka_network: external: true name: kafkaNetwork
Deployment Steps
- Clean up any previous stacks (if you tested earlier):
docker stack rm zk-cluster - Double-check your overlay network exists (you already created this, but confirm):
docker network inspect kafkaNetwork - Deploy the stack to Swarm:
docker stack deploy -c docker-compose.yml zk-cluster - Verify cluster health:
- Check if containers are running:
docker stack ps zk-cluster - Log into a ZooKeeper container to check its role:
docker exec -it $(docker ps -q -f name=zk-cluster_zoo1) zkServer.sh status
Leaderand the other asFollower—that means the cluster is working. - Check if containers are running:
Why This Works
- The corrected
ZOO_SERVERSlets each node resolve the other via the overlay network's built-in DNS. - The
deploysections tell Swarm how to manage each service, ensuring they stay running on the intended nodes. - The timeout settings give ZooKeeper enough time to discover peers and form the cluster, avoiding startup failures common in containerized environments.
内容的提问来源于stack exchange,提问作者Joshua
相关产品推荐
相关产品推荐

