编写 DockerFile 构建 ZooKeeper 镜像

下载软件压缩包

1
2
├── jdk-8u201-linux-x64.tar.gz
└── apache-zookeeper-3.5.5.tar.gz

编写 DockerFile 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
FROM centos

MAINTAINER peter<peter@gmail.com>

ADD jdk-8u201-linux-x64.tar.gz /usr/local
ADD apache-zookeeper-3.5.5.tar.gz /usr/local

RUN yum -y update
RUN yum -y install vim net-tools telnet tree git wget curl

ENV work_path /usr/local

WORKDIR $work_path

# JDK
ENV JAVA_HOME /usr/local/jdk1.8.0_201
ENV JRE_HOME /usr/local/jdk1.8.0_201/jre
ENV CLASSPATH .:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib

# ZooKeeper
ENV ZOOKEEPER_HOME /usr/local/apache-zookeeper-3.5.5

# PATH
ENV PATH $PATH:$JAVA_HOME/bin:$JRE_HOME/bin:$ZOOKEEPER_HOME/bin

RUN cp $ZOOKEEPER_HOME/conf/zoo_sample.cfg $ZOOKEEPER_HOME/conf/zoo.cfg

EXPOSE 2181

CMD $ZOOKEEPER_HOME/bin/zkServer.sh start-foreground

构建 ZooKeeper 镜像,创建并启动容器

1
2
3
4
5
# 构建镜像
# docker build -f docker-file -t zookeeper:3.5.5 .

# 创建并启动容器
# docker run -d --name zookeeper -p 2181:2181 zookeeper:3.5.5

测试客户端连接 ZooKeeper 服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 连接ZooKeeper容器
# docker exec -it zookeeper /bin/bash

# 进入ZooKeeper的bin目录
# cd apache-zookeeper-3.5.5/bin

# 客户端连接ZooKeeper服务器端
$ ./zkCli.sh -server 127.0.0.1:2181

# 创建临时节点
[zk: 127.0.0.1:2181(CONNECTED) 1] create -e /test-node 123456

# 列出所有根节点
[zk: 127.0.0.1:2181(CONNECTED) 2] ls /
[test-node, zookeeper]

# 获取指定节点的值
[zk: 127.0.0.1:2181(CONNECTED) 3] get /test-node
123456
cZxid = 0x4
ctime = Fri Feb 22 02:27:43 UTC 2019
mZxid = 0x4
mtime = Fri Feb 22 02:27:43 UTC 2019
pZxid = 0x4
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x1000a766c5e0001
dataLength = 6
numChildren = 0

# 断开客户端连接
[zk: 127.0.0.1:2181(CONNECTED) 4] quit