当前位置: 首页 > 文档资料 > Docker 简明教程 >

8.1.1 一阶:单主机Standalone模式

优质
小牛编辑
128浏览
2023-12-01

单主机 Standalone 模式

分析:zookeepr在单节点上是以Standalone方式运行,想必在看过本文之前读者也试验过,所以我这里也不会详细说明。其实区分单节点或多节点集群zookeeper最核心大配置就是zoo.cfg,若zoo.cfg文件中包含server,那么在启动zookeeper服务的时候就会以集群方式运行。下面列出单节点zoo.cfg的详细内容:

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zookeeper
clientPort=2181

在下载好的zookeeper中的conf文件夹下有一个zoo_sample.cfg,我们直接将其改为zoo.cfg即可。这里,我们会用2181端口来访问zookeeper服务。

接下来我们来创建zookeeper的镜像。

构建Dockerfile zookeeper需要安装配有java环境,因此,需要将java环境配置进去:

ADD jdk/  /var/jdk/
ENV JAVA_HOME  /var/jdk
ENV PATH       $JAVA_HOME/bin:$PATH

我们再将zookeeper添加进去,并暴露2181端口:

ENV ZK_HOME    /var/zookeeper
EXPOSE 2181 

这样就可以了,具体dockerfile文件内容如下:

# CentOS as base image
# version 0.3
# Author: liugang

# Base image to use, this must be set as the first line
FROM centos

MAINTAINER liugang [email protected]

#ADD JAVA ZOOKEEPER
ADD zookeeper.tar /var
ADD jdk/  /var/jdk/

#ADD ENV
ENV JAVA_HOME  /var/jdk
ENV ZK_HOME    /var/zookeeper
ENV PATH       $JAVA_HOME/bin:$PATH

# Commands to update the image
RUN source /etc/profile

#Expose
EXPOSE 2181 2888 3888

ENTRYPOINT ["/var/zookeeper/bin/zkServer.sh"]

CMD ["start-foreground"]

以上Dockerfile制作的镜像在集群下也可以使用。

构建镜像:

# docker build -t="zookeeper" .
Sending build context to Docker daemon 348.1 MB
Sending build context to Docker daemon 
Step 0 : FROM centos
 ---> a8415304daac
Step 1 : MAINTAINER liugang [email protected]
 ---> Running in 912028e3b1d4
 ---> e7574b19226e
Removing intermediate container 912028e3b1d4
Step 2 : ADD zookeeper.tar /var
 ---> e956ed3cb80e
Removing intermediate container 9e1954b57eeb
Step 3 : ADD jdk/ /var/jdk/
 ---> 327fede9154e
Removing intermediate container cb3551ca5abc
Step 4 : ENV JAVA_HOME /var/jdk
 ---> Running in 2070662c6cad
 ---> 395b3fab1a07
Removing intermediate container 2070662c6cad
Step 5 : ENV ZK_HOME /var/zookeeper
 ---> Running in d535dffc8faf
 ---> d05e2d002c45
Removing intermediate container d535dffc8faf
Step 6 : ENV PATH $JAVA_HOME/bin:$PATH
 ---> Running in c9940b535a9d
 ---> 494ed0d313e1
Removing intermediate container c9940b535a9d
Step 7 : RUN source /etc/profile
 ---> Running in 4477bc347f82
 ---> 040be16683cc
Removing intermediate container 4477bc347f82
Step 8 : RUN mv /var/zookeeper/conf/zoo_sample.cfg /var/zookeeper/conf/zoo.cfg
 ---> Running in e8480d49b717
 ---> b21de36d6251
Removing intermediate container e8480d49b717
Step 9 : EXPOSE 2181 2888 3888
 ---> Running in a840e7759057
 ---> c4733cfba9d1
Removing intermediate container a840e7759057
Step 10 : ENTRYPOINT /var/zookeeper/bin/zkServer.sh
 ---> Running in 6d6ec643f542
 ---> b4cdf9db68cc
Removing intermediate container 6d6ec643f542
Step 11 : CMD start-foreground
 ---> Running in 2d3eedc2c196
 ---> df307fb18230
Removing intermediate container 2d3eedc2c196
Successfully built df307fb18230

接下来测试,我们利用创建好的容器镜像运行一个容器:

# docker  run -d -P --name test zookeeper
620bde126a2f945e62de49adf7592ded71eec86041e441cb618bca198b9d9d55
# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                                                                       NAMES
620bde126a2f        zookeeper           "/var/zookeeper/bin/   6 seconds ago       Up 4 seconds        0.0.0.0:32773->2181/tcp, 0.0.0.0:32772->2888/tcp, 0.0.0.0:32771->3888/tcp   test      

已经运行成功,我们可以通过telnet测试:

# telnet 0.0.0.0 32773
Trying 0.0.0.0...
Connected to 0.0.0.0.
Escape character is '^]'.
stat
Zookeeper version: 3.4.6-1569965, built on 02/20/2014 09:09 GMT
Clients:
 /172.17.42.1:53747[0](queued=0,recved=1,sent=0)

Latency min/avg/max: 0/0/0
Received: 1
Sent: 0
Connections: 1
Outstanding: 0
Zxid: 0x0
Mode: standalone
Node count: 4
Connection closed by foreign host.

可以看到,在容器内部,zookeeper是以standalone方式运行的。