docker安装zookeeper集群

  1. 更改docker镜像源
    vi /etc/default/docker
    DOCKER_OPTS=”–registry-mirror=http://aad0405c.m.daocloud.io”
  2. 拉去zookeeper镜像
    docker pull garland/zookeeper //不要使用官方的zookeeper;garland/zookeeper镜像有些额外配置,对新手来说非常友好
  3. 启动集群
docker run -d \
 --name=zk1 \
 --net=host \
 -e SERVER_ID=1 \
 -e ADDITIONAL_ZOOKEEPER_1=server.1=localhost:2888:3888 \
 -e ADDITIONAL_ZOOKEEPER_2=server.2=localhost:2889:3889 \
 -e ADDITIONAL_ZOOKEEPER_3=server.3=localhost:2890:3890 \
 -e ADDITIONAL_ZOOKEEPER_4=clientPort=2181 \
 garland/zookeeper
docker run -d \
 --name=zk2 \
 --net=host \
 -e SERVER_ID=2 \
 -e ADDITIONAL_ZOOKEEPER_1=server.1=localhost:2888:3888 \
 -e ADDITIONAL_ZOOKEEPER_2=server.2=localhost:2889:3889 \
 -e ADDITIONAL_ZOOKEEPER_3=server.3=localhost:2890:3890 \
 -e ADDITIONAL_ZOOKEEPER_4=clientPort=2182 \
 garland/zookeeper

docker run -d \
 --name=zk3 \
 --net=host \
 -e SERVER_ID=3 \
 -e ADDITIONAL_ZOOKEEPER_1=server.1=localhost:2888:3888 \
 -e ADDITIONAL_ZOOKEEPER_2=server.2=localhost:2889:3889 \
 -e ADDITIONAL_ZOOKEEPER_3=server.3=localhost:2890:3890 \
 -e ADDITIONAL_ZOOKEEPER_4=clientPort=2183 \
 garland/zookeeper

–net=host指令指定改容器使用host网络模式,那么这个容器将不会获得一个独立的Network Namespace,而是和宿主机共用一个Network Namespace。容器将不会虚拟出自己的网卡,配置自己的IP等,而是使用使用宿主机的IP和端口,不用任何NAT转换,就如直接跑在宿主机中的进程一样。但是,容器的其他方面,如文件系统、进程列表等还是和宿主机隔离的。

-e 指定镜像创建完毕执行的命令的参数, garland/zookeeper镜像创建完容器会执行/opt/run.sh,-e指定的参数就是run.sh运行时的参数,run.sh内容如下:

#!/bin/sh
ZOO_CFG="/opt/zookeeper/conf/zoo.cfg"
# Output server ID
echo "server id (myid): ${SERVER_ID}"
echo "${SERVER_ID}" > /tmp/zookeeper/myid
# Add additional ZooKeeper servers into the zoo.cfg file
echo "${ADDITIONAL_ZOOKEEPER_1}" >> ${ZOO_CFG}
echo "${ADDITIONAL_ZOOKEEPER_2}" >> ${ZOO_CFG}
echo "${ADDITIONAL_ZOOKEEPER_3}" >> ${ZOO_CFG}
echo "${ADDITIONAL_ZOOKEEPER_4}" >> ${ZOO_CFG}
echo "${ADDITIONAL_ZOOKEEPER_5}" >> ${ZOO_CFG}
echo "${ADDITIONAL_ZOOKEEPER_6}" >> ${ZOO_CFG}
echo "${ADDITIONAL_ZOOKEEPER_7}" >> ${ZOO_CFG}
echo "${ADDITIONAL_ZOOKEEPER_8}" >> ${ZOO_CFG}
echo "${ADDITIONAL_ZOOKEEPER_9}" >> ${ZOO_CFG}
echo "${ADDITIONAL_ZOOKEEPER_10}" >> ${ZOO_CFG}
# Start Zookeeper
/opt/zookeeper/bin/zkServer.sh start-foreground
  1. 由于是使用host网络模式,所以在连接zookeeper时可以直接使用宿主机的IP,如
    zkCli.cmd -server 192.168.74.131:2181
       zkCli.cmd -server 192.168.74.131:2182
       zkCli.cmd -server 192.168.74.131:2183
  1. 附上garland/zookeeper Darkfile文件
  # DOCKER-VERSION 1.0.1
# VERSION        0.5
# SOURCE         https://github.com/jplock/docker-zookeeper

FROM debian:jessie
MAINTAINER Justin Plock <justin@plock.net>

RUN apt-get update && apt-get install -y openjdk-7-jre-headless wget
RUN wget -q -O - http://apache.mirrors.pair.com/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz | tar -xzf - -C /opt \
    && mv /opt/zookeeper-3.4.6 /opt/zookeeper \
    && cp /opt/zookeeper/conf/zoo_sample.cfg /opt/zookeeper/conf/zoo.cfg \
    && mkdir -p /tmp/zookeeper

ENV JAVA_HOME /usr/lib/jvm/java-7-openjdk-amd64

ADD ./run.sh /opt/run.sh
RUN chmod 777 /opt/run.sh

EXPOSE 2181 2888 3888

WORKDIR /opt/zookeeper

VOLUME ["/opt/zookeeper/conf", "/tmp/zookeeper"]

CMD ["/opt/run.sh"]
  1. 附上一些常用命令
    1. 停用全部运行中的容器:
      docker stop $(docker ps -q)
    2. 删除全部容器:
      docker rm $(docker ps -aq)
    3. 一条命令实现停用并删除容器:
      docker stop $(docker ps -q) & docker rm $(docker ps -aq)
    4. 将宿主机2181端口和容器docker端口对应,并将容器命名为zk1
      docker run -d -p 2181:2181 –name zk1 zookeeper
    5. 使用zkCli.cmd连接该zookeeper验证是否部署成功
      zkCli.cmd -server 宿主机IP:2181
      docker run -it -link=zk1:zkCli zookeeper zkCli.sh -server 192.168.74.131
    6. docker logs -t –tail=20 容器ID 查看容器日志

参考文件: https://github.com/denverdino/aliyungo/wiki/Zookeeper-cluster-with-Docker

创作不易,转载请注明文章来源为www.full-satcker.com:邢桂帅 » docker安装zookeeper集群

赞 (0)
分享到:更多 ()

相关推荐

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址