使用Docker的Zookeeper群集模式出错?

时间:2017-06-05 10:30:56

标签: docker docker-compose apache-zookeeper

我正在尝试使用不同主机上的docker部署zookeeper群集。我使用下一个docker-compose,我在host2中的host1 zoo2和host3中的zoo3中运行zoo1。

version: '2'
services:
    zoo1:
        image: 31z4/zookeeper
        restart: always
        ports:
            - 2181:2181
            - 2888:2888
            - 3888:3888
        environment:
            ZOO_MY_ID: 1
            ZOO_SERVERS: server.1=host1-IP:2888:3888 server.2=host2-IP:2888:3888 server.3=host3-IP:2888:3888

    zoo2:
        image: 31z4/zookeeper
        restart: always
        ports:
            - 2182:2181
            - 2888:2888
            - 3888:3888
        environment:
            ZOO_MY_ID: 2
            ZOO_SERVERS: server.1=host1-IP:2888:3888 server.2=host2-IP:2888:3888 server.3=host3-IP:2888:3888

    zoo3:
        image: 31z4/zookeeper
        restart: always
        ports:
            - 2183:2181
            - 2888:2888
            - 3888:3888
        environment:
            ZOO_MY_ID: 3
            ZOO_SERVERS: server.1=host1-IP:2888:3888 server.2=host2-IP:2888:3888 server.3=host3-IP:2888:3888

代码来自:https://github.com/31z4/zookeeper-docker

我只是尝试使用下一个docker-compose,并将zoo1,zoo2和zoo3添加到/ etc / hosts文件中:

version: '2'
services:
    zoo1:
        image: 31z4/zookeeper
        restart: always
        ports:
            - 2181:2181
            - 2888:2888
            - 3888:3888
        environment:
            ZOO_MY_ID: 1
            ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888

    zoo2:
        image: 31z4/zookeeper
        restart: always
        ports:
            - 2182:2181
            - 2888:2888
            - 3888:3888
        environment:
            ZOO_MY_ID: 2
            ZOO_SERVERS: server.1=zooo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888

    zoo3:
        image: 31z4/zookeeper
        restart: always
        ports:
            - 2183:2181
            - 2888:2888
            - 3888:3888
        environment:
            ZOO_MY_ID: 3
            ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888

但如果检查状态,则无效:

./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /conf/zoo.cfg
Error contacting service. It is probably not running.

1 个答案:

答案 0 :(得分:1)

公开端口的左侧部分是主机的本地端口,只能使用一次。此外,您不需要公开容器互连的端口,因为它们位于同一网络中。

因此请删除左侧部分2888:2888,将其保留为:2888,然后docker将在主机中找到一个空闲端口(使用docker ps查找它)。如果您不需要暴露端口,请直接删除ports指令。

version: '2'
services:
    zoo1:
        image: 31z4/zookeeper
        restart: always
        ports:
            - 2181
            - 2888
            - 3888
        environment:
            ZOO_MY_ID: 1
            ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888

    zoo2:
        image: 31z4/zookeeper
        restart: always
        ports:
            - 2181
            - 2888
            - 3888
        environment:
            ZOO_MY_ID: 2
            ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888

    zoo3:
        image: 31z4/zookeeper
        restart: always
        ports:
            - 2181
            - 2888
            - 3888
        environment:
            ZOO_MY_ID: 3
            ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888