Zookeeper群集无法在Docker

时间:2018-02-13 18:02:19

标签: docker apache-kafka docker-compose apache-zookeeper

我正在创建一个3节点Zookeeper集群和一个3节点Kafka集群。当我创建Zookeeper集群时,对于其他两个节点中的每一个,只创建了一个zookeeper节点,而另外两个节点以下面的错误退出 -

zookeeper-3_1  | 2018-02-13 17:38:22,310 [myid:] - INFO  [main:ServerCnxnFactory@117] - Using org.apache.zookeeper.server.NIOServerCnxnFactory as server connection factory
zookeeper-3_1  | 2018-02-13 17:38:22,322 [myid:] - INFO  [main:NIOServerCnxnFactory@89] - binding to port 0.0.0.0/0.0.0.0:2181
zookeeper-3_1  | 2018-02-13 17:38:22,323 [myid:] - ERROR [main:ZooKeeperServerMain@66] - Unexpected exception, exiting abnormally
zookeeper-3_1  | java.net.BindException: Address in use
zookeeper-3_1  |    at sun.nio.ch.Net.bind0(Native Method)
zookeeper-3_1  |    at sun.nio.ch.Net.bind(Net.java:433)
zookeeper-3_1  |    at sun.nio.ch.Net.bind(Net.java:425)
zookeeper-3_1  |    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
zookeeper-3_1  |    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
zookeeper-3_1  |    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:67)
zookeeper-3_1  |    at org.apache.zookeeper.server.NIOServerCnxnFactory.configure(NIOServerCnxnFactory.java:90)
zookeeper-3_1  |    at org.apache.zookeeper.server.ZooKeeperServerMain.runFromConfig(ZooKeeperServerMain.java:119)
zookeeper-3_1  |    at org.apache.zookeeper.server.ZooKeeperServerMain.initializeAndRun(ZooKeeperServerMain.java:89)
zookeeper-3_1  |    at org.apache.zookeeper.server.ZooKeeperServerMain.main(ZooKeeperServerMain.java:55)
zookeeper-3_1  |    at org.apache.zookeeper.server.quorum.QuorumPeerMain.initializeAndRun(QuorumPeerMain.java:119)
zookeeper-3_1  |    at org.apache.zookeeper.server.quorum.QuorumPeerMain.main(QuorumPeerMain.java:81)

我认为这是因为Zookeeper的所有节点都在使用端口2181.但在我的docker-compose.yml文件中,我为每个节点指定了不同的客户端端口,以及为zookeeper服务器指定了不同的端口。

我的docker-compose.yml代码是 -

version: '2'
services:
  zookeeper-1:
    image: zookeeper:latest
    environment:
      ZOOKEEPER_SERVER_ID: 1
      ZOOKEEPER_CLIENT_PORT: 22181
      ZOOKEEPER_TICK_TIME: 2000
      ZOOKEEPER_INIT_LIMIT: 5
      ZOOKEEPER_SYNC_LIMIT: 2
      ZOOKEEPER_SERVERS: localhost:22888:23888;localhost:32888:33888;localhost:42888:43888
    network_mode: host

  zookeeper-2:
    image: zookeeper:latest
    environment:
      ZOOKEEPER_SERVER_ID: 2
      ZOOKEEPER_CLIENT_PORT: 32181
      ZOOKEEPER_TICK_TIME: 2000
      ZOOKEEPER_INIT_LIMIT: 5
      ZOOKEEPER_SYNC_LIMIT: 2
      ZOOKEEPER_SERVERS: localhost:22888:23888;localhost:32888:33888;localhost:42888:43888
    network_mode: host  

  zookeeper-3:
    image: zookeeper:latest
    environment:
      ZOOKEEPER_SERVER_ID: 3
      ZOOKEEPER_CLIENT_PORT: 42181
      ZOOKEEPER_TICK_TIME: 2000
      ZOOKEEPER_INIT_LIMIT: 5
      ZOOKEEPER_SYNC_LIMIT: 2
      ZOOKEEPER_SERVERS: localhost:22888:23888;localhost:32888:33888;localhost:42888:43888
    network_mode: host    

  kafka1:
    image: abc/kafka:latest
    hostname: kafka1
    network_mode: host
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: localhost:22181,localhost:32181,localhost:42181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:19092
    depends_on:
      - zookeeper-1
      - zookeeper-2
      - zookeeper-3

  kafka2:
    image: abc/kafka:latest
    hostname: kafka2
    network_mode: host
    environment:
      KAFKA_BROKER_ID: 2
      KAFKA_ZOOKEEPER_CONNECT: localhost:22181,localhost:32181,localhost:42181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:29092
    depends_on:
      - zookeeper-1
      - zookeeper-2
      - zookeeper-3

  kafka3:
    image: abc/kafka:latest
    hostname: kafka3
    network_mode: host
    environment:
      KAFKA_BROKER_ID: 3
      KAFKA_ZOOKEEPER_CONNECT: localhost:22181,localhost:32181,localhost:42181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:39092
    depends_on:
      - zookeeper-1
      - zookeeper-2
      - zookeeper-3

我还把Dockerfile用于创建Kafka图像

FROM centos:7
MAINTAINER abc@xyz.com

ENV KAFKA_BIN=http://www-eu.apache.org/dist/kafka/0.11.0.2/kafka_2.11-0.11.0.2.tgz

RUN yum install -y wget java-1.8.0-openjdk \
    && cd /tmp && wget -q $KAFKA_BIN \
    && export K_TAR=/tmp/$(ls kafka* | head -1) \
    && mkdir -p /opt/apache/kafka/ && tar -zxf $K_TAR -C /opt/apache/kafka/ \
    && cd /opt/apache/kafka && ln -s $(ls) current \
    && rm -rf $K_TAR

ENV KAFKA_HOME /opt/apache/kafka/current
ENV PATH $PATH:$KAFKA_HOME/bin

ADD resources /home/kafka

RUN groupadd -r kafka \
    && useradd -r -g kafka kafka \
    && mkdir -p /home/kafka \
    && chown -R kafka:kafka /home/kafka \
    && chmod -R +x /home/kafka/scripts \
    && mkdir -p /var/log/kafka \
    && chown -R kafka:kafka /var/log/kafka \
    && mkdir -p /etc/kafka \
    && chown -R kafka:kafka /etc/kafka

USER kafka

有人可以帮我解决这个问题 - 为什么我在运行docker-compose up命令时无法启动3节点Zookeeper集群?谢谢

1 个答案:

答案 0 :(得分:2)

您正在尝试使用主机网络,然后您只能绑定一次地址。因此,您可能希望使用不同的端口或不使用主机网络并在必要时公开端口。

然后您可以将它们称为zookeeper-1,zookeeper-2 ...

对于kafka节点,如果他们想要访问zookeeper,他们应该通过zookeeper-1,zookeeper-2访问它们,也应该这样做... (我无法测试这个因为我无法构建abc / kafka图像,因为我错过了Dockerfile中的资源文件夹)