You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在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_SERVERS values: Each node's ZOO_SERVERS must list all cluster members, not just a partial set with 0.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 deploy section 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

  1. Clean up any previous stacks (if you tested earlier):
    docker stack rm zk-cluster
    
  2. Double-check your overlay network exists (you already created this, but confirm):
    docker network inspect kafkaNetwork
    
  3. Deploy the stack to Swarm:
    docker stack deploy -c docker-compose.yml zk-cluster
    
  4. 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
      
    Within a minute, you should see one node as Leader and the other as Follower—that means the cluster is working.

Why This Works

  • The corrected ZOO_SERVERS lets each node resolve the other via the overlay network's built-in DNS.
  • The deploy sections 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:28:09