Docker中搭建zookeeper集群

阿凡达2018-07-10 12:21

1.获取官方镜像

从dockerhub获取官方的zookeeper镜像:

docker pull zookeeper

2.了解镜像内容

拉取完镜像后,通过

docker inspect zookeeper

我们可以查看到关于该镜像的一些基本信息:

......
"Env": [
    "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/jvm/java-1.8-openjdk/jre/bin:/usr/lib/jvm/java-1.8-openjdk/bin:/zookeeper-3.4.10/bin",
    "LANG=C.UTF-8",
    "JAVA_HOME=/usr/lib/jvm/java-1.8-openjdk/jre",
    "JAVA_VERSION=8u131",
    "JAVA_ALPINE_VERSION=8.131.11-r2",
    "ZOO_USER=zookeeper",
    "ZOO_CONF_DIR=/conf",
    "ZOO_DATA_DIR=/data",
    "ZOO_DATA_LOG_DIR=/datalog",
    "ZOO_PORT=2181",
    "ZOO_TICK_TIME=2000",
    "ZOO_INIT_LIMIT=5",
    "ZOO_SYNC_LIMIT=2",
    "ZOO_MAX_CLIENT_CNXNS=60",
    "ZOOCFGDIR=/conf"
],
"Cmd": [
    "zkServer.sh",
    "start-foreground"
],
"Volumes": {
    "/data": {},
    "/datalog": {}
},
"WorkingDir": "/zookeeper-3.4.10",
"Entrypoint": [
    "/docker-entrypoint.sh"
],
......

即,该zookeeper的版本是3.4.10,conf目录在/conf,基于该镜像启动的容器的entrypoint为

/docker-entrypoint.sh

默认传入的参数为

zkServer.sh start-foreground

基于该镜像启动一个容器:

docker run -d zookeeper

然后进入容器后,我们可以查看docker-entrypoint.sh的基本内容:

#!/bin/bash

set -e

# Allow the container to be started with `--user`
if [ "$1" = 'zkServer.sh' -a "$(id -u)" = '0' ]; then
    chown -R "$ZOO_USER" "$ZOO_DATA_DIR" "$ZOO_DATA_LOG_DIR"
    exec su-exec "$ZOO_USER" "$0" "$@"
fi

# Generate the config only if it doesn't exist
if [ ! -f "$ZOO_CONF_DIR/zoo.cfg" ]; then
    CONFIG="$ZOO_CONF_DIR/zoo.cfg"

    echo "clientPort=$ZOO_PORT" >> "$CONFIG"
    echo "dataDir=$ZOO_DATA_DIR" >> "$CONFIG"
    echo "dataLogDir=$ZOO_DATA_LOG_DIR" >> "$CONFIG"

    echo "tickTime=$ZOO_TICK_TIME" >> "$CONFIG"
    echo "initLimit=$ZOO_INIT_LIMIT" >> "$CONFIG"
    echo "syncLimit=$ZOO_SYNC_LIMIT" >> "$CONFIG"

    echo "maxClientCnxns=$ZOO_MAX_CLIENT_CNXNS" >> "$CONFIG"

    for server in $ZOO_SERVERS; do
        echo "$server" >> "$CONFIG"
    done
fi

# Write myid only if it doesn't exist
if [ ! -f "$ZOO_DATA_DIR/myid" ]; then
    echo "${ZOO_MY_ID:-1}" > "$ZOO_DATA_DIR/myid"
fi

exec "$@"

可以看到,该脚本主要是用于设置启动zookeeper的用户,以及创建zoo.cfg配置文件。

看到其中有一段是:

for server in $ZOO_SERVERS; do
        echo "$server" >> "$CONFIG"
done
# Write myid only if it doesn't exist
if [ ! -f "$ZOO_DATA_DIR/myid" ]; then
    echo "${ZOO_MY_ID:-1}" > "$ZOO_DATA_DIR/myid"
fi

即只要传入ZOO_SERVERS参数,我们就可以设置各个zookeeper server节点的host信息,设置ZOO_MY_ID参数就可以写入本节点的serverID。

这一段脚本的作用使得该镜像可以很好地适配zk的不同模式:单机模式和集群模式(伪集群模式)。

3.在宿主机上搭建多节点集群模式的zookeeper

基于上述调研,我们可以明确搭建一个多节点的zookeeper集群需要的步骤如下:

(1) 获取zookeeper官方镜像

(2) 连通不同zk server节点所在容器之间的网络

(3) 对每个zk server节点设置相同的zoo.cfg,传入集群中各节点的host信息

(4) 设置各个zk server节点的ServerID(修改各自的$dataDir/myid文件)

(5) 启动集群, 并将端口2181映射到宿主机。

基于上述步骤,整合成如下的shell脚本:

#!/bin/bash
#Get zookeeper image
zkimage=`docker images | grep zookeeper | awk {'print $1'}`
if [ -n "$zkimage" ]
then
    echo 'The zookeeper image is already existed.'
else
    echo 'Pull the latest zookeeper image.'
    docker pull zookeeper
fi

#Create network for zookeeper containers
zknet=`docker network ls | grep zknetwork | awk {'print $2'}`
if [ -n "$zknet" ]
then
    echo 'The zknetwork is already existed.'
else
    echo 'Create zknetwork.'
    docker network create zknetwork
fi

#Start zookeeper cluster
echo 'Start 3 zookeeper servers.'

ZOO_SERVERS="server.1=zkServer1:2888:3888 server.2=zkServer2:2888:3888 server.3=zkServer3:2888:3888"

docker run -d -e ZOO_SERVERS="$ZOO_SERVERS" -e ZOO_MY_ID=1 --name zkServer1 --net zknetwork -p 2181:2181 zookeeper
docker run -d -e ZOO_SERVERS="$ZOO_SERVERS" -e ZOO_MY_ID=2 --name zkServer2 --net zknetwork -p 2182:2181 zookeeper
docker run -d -e ZOO_SERVERS="$ZOO_SERVERS" -e ZOO_MY_ID=3 --name zkServer3 --net zknetwork -p 2183:2181 zookeeper

执行该shell脚本,便可启动一个新的zk集群。

root@hadoop985:~/docker/zookeeper-docker# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                        NAMES
bfc84ce7aa1d        zookeeper           "/docker-entrypoin..."   36 minutes ago      Up 36 minutes       2888/tcp, 3888/tcp, 0.0.0.0:2183->2181/tcp   zkServer3
18b6b1d9987c        zookeeper           "/docker-entrypoin..."   36 minutes ago      Up 36 minutes       2888/tcp, 3888/tcp, 0.0.0.0:2182->2181/tcp   zkServer2
0b6d1b69bb05        zookeeper           "/docker-entrypoin..."   36 minutes ago      Up 36 minutes       2888/tcp, 0.0.0.0:2181->2181/tcp, 3888/tcp   zkServer1

本文来自网易实践者社区,经作者倪志风授权发布。