文章目录
1.Dockerfile
1.1 环境准备
apache-zookeeper-3.6.1-bin.tar.gz
jdk-8u121-linux-x64.tar.gz
zookeeper.DockerFile
# 基础镜像 生成的镜像作为基础镜像
FROM centos
# 指定维护者的信息
MAINTAINER tanggaomeng<tanggaomeng@inspur.com>
# 复制并解压
ADD jdk-8u121-linux-x64.tar.gz /usr/local/
ADD apache-zookeeper-3.6.1-bin.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
# java
ENV JAVA_HOME /usr/local/jdk1.8.0_121
ENV JRE_HOME /usr/local/jdk1.8.0_121/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.6.1-bin
# 环境变量设置
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
1.2 构建镜像
docker build -f zookeeper.DockerFile -t zookeeper:3.6.1 .
1.3 创建并启动容器
docker run -d --name zookeeper -p 2181:2181 zookeeper:3.6.1
1.4 连接ZooKeeper容器
docker exec -it zookeeper /bin/bash
1.5 进入ZooKeeper的bin目录
cd apache-zookeeper-3.6.1/bin
1.6 客户端连接ZooKeeper服务器端
./zkCli.sh -server 127.0.0.1:2181
1.7 创建节点
[zk: 127.0.0.1:2181(CONNECTED) 1] create -e /test-node 123456
1.8 列出所有根节点
[zk: 127.0.0.1:2181(CONNECTED) 2] ls /
[test-node, zookeeper]
1.9 获取指定节点的值
[zk: 127.0.0.1:2181(CONNECTED) 3] get /test-node
1.10 断开客户端连接
[zk: 127.0.0.1:2181(CONNECTED) 4] quit