基于Docker安装zookeeper

单节点安装

查询zookeeper镜像

  • 语法:docker search zookeeper
  • 示例:
[root@izbp13xko46hud9vfr5s94z /]# docker search zookeeper 
NAME                                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
zookeeper                           Apache ZooKeeper is an open-source server wh…   433                 [OK]                
jplock/zookeeper                    Builds a docker image for Zookeeper version161                                     [OK]
mesoscloud/zookeeper                ZooKeeper                                       73                                      [OK]
wurstmeister/zookeeper                                                              56                                      [OK]
confluentinc/cp-zookeeper           Official Confluent Docker Image for Zookeeper   28                                      
mbabineau/zookeeper-exhibitor                                                       23                                      [OK]
digitalwonderland/zookeeper         Latest Zookeeper - clusterable                  18                                      [OK]
confluent/zookeeper                                                                 12                                      [OK]
tobilg/zookeeper-webui              Docker image for using `zk-web` as ZooKeeper…   10                                      [OK]
debezium/zookeeper                  Zookeeper image required when running the De…   8                                       [OK]
hyperledger/fabric-zookeeper        Fabric Zookeeper docker image for Hyperledge…   8                                       
jeygeethan/zookeeper-cluster        A simple zookeeper cluster using docker imag…   7                                       [OK]
springxd/zookeeper                  A Docker image that can run a ZooKeeper serv…   6                                       [OK]
elevy/zookeeper                     ZooKeeper configured to execute an ensemble …   6                                       [OK]
31z4/zookeeper                      Dockerized Apache Zookeeper.                    5                                       [OK]
thefactory/zookeeper-exhibitor      Exhibitor-managed ZooKeeper with S3 backups …   5                                       [OK]
bitnami/zookeeper                   ZooKeeper is a centralized service for distr…   4                                       [OK]
paulbrown/zookeeper                 Zookeeper on Kubernetes (PetSet)                1                                       [OK]
openshift/zookeeper-346-fedora20    ZooKeeper 3.4.6 with replication support        1                                       
ciscocloud/zookeeper                                                                1                                       [OK]
jamiecressey89/marathon-zookeeper   Zookeeper image that uses Marathon's API for0                                       [OK]
duffqiu/zookeeper-cli                                                               0                                       [OK]
phenompeople/zookeeper              Apache ZooKeeper is an open-source server wh…   0                                       [OK]
strimzi/zookeeper                                                                   0                                       
midonet/zookeeper                   Dockerfile for a Zookeeper server.              0                                       [OK]
[root@izbp13xko46hud9vfr5s94z /]# 

拉取zookeeper镜像

  • 语法:docker pull zookeeper
    这是拉取的最新版本的官方镜像,也可以选择其它版本的镜像

  • 示例:

[root@izbp13xko46hud9vfr5s94z /]# docker pull zookeeper 
Using default tag: latest
latest: Pulling from library/zookeeper
8e3ba11ec2a2: Pull complete 
311ad0da4533: Pull complete 
391a6a6b3651: Pull complete 
104bcdeaa0fa: Pull complete 
a212fb0da7e6: Pull complete 
453117a52ac9: Pull complete 
83b030e7117e: Pull complete 
Digest: sha256:48f64faf49473825f84574e04b0b74008b0ee23770405e0e7d8262c172b9d826
Status: Downloaded newer image for zookeeper:latest
[root@izbp13xko46hud9vfr5s94z /]#

启动zookeeper容器实例

  • 语法:docker run --name zookeeper --restart always -d zookeeper
    --restart always:始终重新启动zookeeper

  • 示例:

[root@izbp13xko46hud9vfr5s94z /]# docker run --name zookeeper --restart always -d zookeeper
b6838b7ca21a23fd80037e4e7b6d364ecf7613f39ecaee01cb26cb44455514dc
[root@izbp13xko46hud9vfr5s94z /]#

查看当前zookeeper容器实例的进程信息

  • 语法:docker top zookeeper
    zookeeper:表示当前zookeeper实例的名称,通过--name指定,也可以使用容器ID来替代

  • 示例:

docker top zookeeper
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
1000                6737                6715                0                   13:49               ?                   00:00:01            /usr/lib/jvm/java-1.8-openjdk/jre/bin/java -Dzookeeper.log.dir=. -Dzookeeper.root.logger=INFO,CONSOLE -cp /zookeeper-3.4.13/bin/../build/classes:/zookeeper-3.4.13/bin/../build/lib/*.jar:/zookeeper-3.4.13/bin/../lib/slf4j-log4j12-1.7.25.jar:/zookeeper-3.4.13/bin/../lib/slf4j-api-1.7.25.jar:/zookeeper-3.4.13/bin/../lib/netty-3.10.6.Final.jar:/zookeeper-3.4.13/bin/../lib/log4j-1.2.17.jar:/zookeeper-3.4.13/bin/../lib/jline-0.9.94.jar:/zookeeper-3.4.13/bin/../lib/audience-annotations-0.5.0.jar:/zookeeper-3.4.13/bin/../zookeeper-3.4.13.jar:/zookeeper-3.4.13/bin/../src/java/lib/*.jar:/conf: -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=false org.apache.zookeeper.server.quorum.QuorumPeerMain /conf/zoo.cfg
[root@izbp13xko46hud9vfr5s94z /]# 

也可以通过ps -ef | grep zookeeper来查看进程信息,示例如下:

[root@izbp13xko46hud9vfr5s94z /]# ps -ef | grep zookeeper 
1000      6737  6715  0 13:49 ?        00:00:01 /usr/lib/jvm/java-1.8-openjdk/jre/bin/java -Dzookeeper.log.dir=. -Dzookeeper.root.logger=INFO,CONSOLE -cp /zookeeper-3.4.13/bin/../build/classes:/zookeeper-3.4.13/bin/../build/lib/*.jar:/zookeeper-3.4.13/bin/../lib/slf4j-log4j12-1.7.25.jar:/zookeeper-3.4.13/bin/../lib/slf4j-api-1.7.25.jar:/zookeeper-3.4.13/bin/../lib/netty-3.10.6.Final.jar:/zookeeper-3.4.13/bin/../lib/log4j-1.2.17.jar:/zookeeper-3.4.13/bin/../lib/jline-0.9.94.jar:/zookeeper-3.4.13/bin/../lib/audience-annotations-0.5.0.jar:/zookeeper-3.4.13/bin/../zookeeper-3.4.13.jar:/zookeeper-3.4.13/bin/../src/java/lib/*.jar:/conf: -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=false org.apache.zookeeper.server.quorum.QuorumPeerMain /conf/zoo.cfg
root      7301  6175  0 13:58 pts/0    00:00:00 grep --color=auto zookeeper
[root@izbp13xko46hud9vfr5s94z /]#

停止zookeeper实例进程

  • 语法:docker stop zookeeper
  • 示例:
[root@izbp13xko46hud9vfr5s94z /]# docker stop zookeeper 
zookeeper
[root@izbp13xko46hud9vfr5s94z /]#

启动、重启zookeeper实例进程

  • 启动语法:docker start zookeeper
  • 重启语法:docker restart zookeeper
  • 示例:
[root@izbp13xko46hud9vfr5s94z /]# docker start zookeeper
zookeeper
[root@izbp13xko46hud9vfr5s94z /]# docker restart zookeeper 
zookeeper
[root@izbp13xko46hud9vfr5s94z /]#

查看zookeeper进程日志

  • 语法:docker logs -f zookeeper
  • 示例:
[root@izbp13xko46hud9vfr5s94z /]# docker logs -f zookeeper 
ZooKeeper JMX enabled by default
Using config: /conf/zoo.cfg
2018-08-10 05:49:49,935 [myid:] - INFO  [main:QuorumPeerConfig@136] - Reading configuration from: /conf/zoo.cfg
2018-08-10 05:49:49,945 [myid:] - INFO  [main:DatadirCleanupManager@78] - autopurge.snapRetainCount set to 3
2018-08-10 05:49:49,945 [myid:] - INFO  [main:DatadirCleanupManager@79] - autopurge.purgeInterval set to 0
2018-08-10 05:49:49,945 [myid:] - INFO  [main:DatadirCleanupManager@101] - Purge task is not scheduled.
2018-08-10 05:49:49,952 [myid:] - WARN  [main:QuorumPeerMain@116] - Either no config or no quorum defined in config, running  in standalone mode
2018-08-10 05:49:49,969 [myid:] - INFO  [main:QuorumPeerConfig@136] - Reading configuration from: /conf/zoo.cfg
2018-08-10 05:49:49,969 [myid:] - INFO  [main:ZooKeeperServerMain@98] - Starting server
2018-08-10 05:49:49,982 [myid:] - INFO  [main:Environment@100] - Server environment:zookeeper.version=3.4.13-2d71af4dbe22557fda74f9a9b4309b15a7487f03, built on 06/29/2018 04:05 GMT
2018-08-10 05:49:49,982 [myid:] - INFO  [main:Environment@100] - Server environment:host.name=b6838b7ca21a
2018-08-10 05:49:49,982 [myid:] - INFO  [main:Environment@100] - Server environment:java.version=1.8.0_171
2018-08-10 05:49:49,982 [myid:] - INFO  [main:Environment@100] - Server environment:java.vendor=Oracle Corporation
2018-08-10 05:49:49,982 [myid:] - INFO  [main:Environment@100] - Server environment:java.home=/usr/lib/jvm/java-1.8-openjdk/jre
2018-08-10 05:49:49,982 [myid:] - INFO  [main:Environment@100] - Server environment:java.class.path=/zookeeper-3.4.13/bin/../build/classes:/zookeeper-3.4.13/bin/../build/lib/*.jar:/zookeeper-3.4.13/bin/../lib/slf4j-log4j12-1.7.25.jar:/zookeeper-3.4.13/bin/../lib/slf4j-api-1.7.25.jar:/zookeeper-3.4.13/bin/../lib/netty-3.10.6.Final.jar:/zookeeper-3.4.13/bin/../lib/log4j-1.2.17.jar:/zookeeper-3.4.13/bin/../lib/jline-0.9.94.jar:/zookeeper-3.4.13/bin/../lib/audience-annotations-0.5.0.jar:/zookeeper-3.4.13/bin/../zookeeper-3.4.13.jar:/zookeeper-3.4.13/bin/../src/java/lib/*.jar:/conf:
2018-08-10 05:49:49,983 [myid:] - INFO  [main:Environment@100] - Server environment:java.library.path=/usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64/server:/usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64:/usr/lib/jvm/java-1.8-openjdk/jre/../lib/amd64:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2018-08-10 05:49:49,983 [myid:] - INFO  [main:Environment@100] - Server environment:java.io.tmpdir=/tmp
2018-08-10 05:49:49,983 [myid:] - INFO  [main:Environment@100] - Server environment:java.compiler=<NA>
2018-08-10 05:49:49,991 [myid:] - INFO  [main:Environment@100] - Server environment:os.name=Linux
2018-08-10 05:49:49,992 [myid:] - INFO  [main:Environment@100] - Server environment:os.arch=amd64
2018-08-10 05:49:49,992 [myid:] - INFO  [main:Environment@100] - Server environment:os.version=3.10.0-693.2.2.el7.x86_64
2018-08-10 05:49:49,992 [myid:] - INFO  [main:Environment@100] - Server environment:user.name=zookeeper
2018-08-10 05:49:49,992 [myid:] - INFO  [main:Environment@100] - Server environment:user.home=/home/zookeeper
2018-08-10 05:49:49,992 [myid:] - INFO  [main:Environment@100] - Server environment:user.dir=/zookeeper-3.4.13
2018-08-10 05:49:50,007 [myid:] - INFO  [main:ZooKeeperServer@836] - tickTime set to 2000
2018-08-10 05:49:50,008 [myid:] - INFO  [main:ZooKeeperServer@845] - minSessionTimeout set to -1
2018-08-10 05:49:50,008 [myid:] - INFO  [main:ZooKeeperServer@854] - maxSessionTimeout set to -1
2018-08-10 05:49:50,028 [myid:] - INFO  [main:ServerCnxnFactory@117] - Using org.apache.zookeeper.server.NIOServerCnxnFactory as server connection factory
2018-08-10 05:49:50,034 [myid:] - INFO  [main:NIOServerCnxnFactory@89] - binding to port 0.0.0.0/0.0.0.0:2181
ZooKeeper JMX enabled by default
Using config: /conf/zoo.cfg
2018-08-10 06:04:16,325 [myid:] - INFO  [main:QuorumPeerConfig@136] - Reading configuration from: /conf/zoo.cfg
2018-08-10 06:04:16,331 [myid:] - INFO  [main:DatadirCleanupManager@78] - autopurge.snapRetainCount set to 3
2018-08-10 06:04:16,331 [myid:] - INFO  [main:DatadirCleanupManager@79] - autopurge.purgeInterval set to 0
2018-08-10 06:04:16,331 [myid:] - INFO  [main:DatadirCleanupManager@101] - Purge task is not scheduled.
2018-08-10 06:04:16,331 [myid:] - WARN  [main:QuorumPeerMain@116] - Either no config or no quorum defined in config, running  in standalone mode
2018-08-10 06:04:16,350 [myid:] - INFO  [main:QuorumPeerConfig@136] - Reading configuration from: /conf/zoo.cfg
2018-08-10 06:04:16,350 [myid:] - INFO  [main:ZooKeeperServerMain@98] - Starting server
2018-08-10 06:04:16,361 [myid:] - INFO  [main:Environment@100] - Server environment:zookeeper.version=3.4.13-2d71af4dbe22557fda74f9a9b4309b15a7487f03, built on 06/29/2018 04:05 GMT
2018-08-10 06:04:16,362 [myid:] - INFO  [main:Environment@100] - Server environment:host.name=b6838b7ca21a
2018-08-10 06:04:16,362 [myid:] - INFO  [main:Environment@100] - Server environment:java.version=1.8.0_171
2018-08-10 06:04:16,362 [myid:] - INFO  [main:Environment@100] - Server environment:java.vendor=Oracle Corporation
2018-08-10 06:04:16,362 [myid:] - INFO  [main:Environment@100] - Server environment:java.home=/usr/lib/jvm/java-1.8-openjdk/jre
2018-08-10 06:04:16,362 [myid:] - INFO  [main:Environment@100] - Server environment:java.class.path=/zookeeper-3.4.13/bin/../build/classes:/zookeeper-3.4.13/bin/../build/lib/*.jar:/zookeeper-3.4.13/bin/../lib/slf4j-log4j12-1.7.25.jar:/zookeeper-3.4.13/bin/../lib/slf4j-api-1.7.25.jar:/zookeeper-3.4.13/bin/../lib/netty-3.10.6.Final.jar:/zookeeper-3.4.13/bin/../lib/log4j-1.2.17.jar:/zookeeper-3.4.13/bin/../lib/jline-0.9.94.jar:/zookeeper-3.4.13/bin/../lib/audience-annotations-0.5.0.jar:/zookeeper-3.4.13/bin/../zookeeper-3.4.13.jar:/zookeeper-3.4.13/bin/../src/java/lib/*.jar:/conf:
2018-08-10 06:04:16,362 [myid:] - INFO  [main:Environment@100] - Server environment:java.library.path=/usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64/server:/usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64:/usr/lib/jvm/java-1.8-openjdk/jre/../lib/amd64:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2018-08-10 06:04:16,362 [myid:] - INFO  [main:Environment@100] - Server environment:java.io.tmpdir=/tmp
2018-08-10 06:04:16,362 [myid:] - INFO  [main:Environment@100] - Server environment:java.compiler=<NA>
2018-08-10 06:04:16,364 [myid:] - INFO  [main:Environment@100] - Server environment:os.name=Linux
2018-08-10 06:04:16,364 [myid:] - INFO  [main:Environment@100] - Server environment:os.arch=amd64
2018-08-10 06:04:16,364 [myid:] - INFO  [main:Environment@100] - Server environment:os.version=3.10.0-693.2.2.el7.x86_64
2018-08-10 06:04:16,365 [myid:] - INFO  [main:Environment@100] - Server environment:user.name=zookeeper
2018-08-10 06:04:16,365 [myid:] - INFO  [main:Environment@100] - Server environment:user.home=/home/zookeeper
2018-08-10 06:04:16,365 [myid:] - INFO  [main:Environment@100] - Server environment:user.dir=/zookeeper-3.4.13
2018-08-10 06:04:16,376 [myid:] - INFO  [main:ZooKeeperServer@836] - tickTime set to 2000
2018-08-10 06:04:16,376 [myid:] - INFO  [main:ZooKeeperServer@845] - minSessionTimeout set to -1
2018-08-10 06:04:16,376 [myid:] - INFO  [main:ZooKeeperServer@854] - maxSessionTimeout set to -1
2018-08-10 06:04:16,386 [myid:] - INFO  [main:ServerCnxnFactory@117] - Using org.apache.zookeeper.server.NIOServerCnxnFactory as server connection factory
2018-08-10 06:04:16,392 [myid:] - INFO  [main:NIOServerCnxnFactory@89] - binding to port 0.0.0.0/0.0.0.0:2181
ZooKeeper JMX enabled by default
Using config: /conf/zoo.cfg
2018-08-10 06:04:56,427 [myid:] - INFO  [main:QuorumPeerConfig@136] - Reading configuration from: /conf/zoo.cfg
2018-08-10 06:04:56,448 [myid:] - INFO  [main:DatadirCleanupManager@78] - autopurge.snapRetainCount set to 3
2018-08-10 06:04:56,449 [myid:] - INFO  [main:DatadirCleanupManager@79] - autopurge.purgeInterval set to 0
2018-08-10 06:04:56,449 [myid:] - INFO  [main:DatadirCleanupManager@101] - Purge task is not scheduled.
2018-08-10 06:04:56,449 [myid:] - WARN  [main:QuorumPeerMain@116] - Either no config or no quorum defined in config, running  in standalone mode
2018-08-10 06:04:56,469 [myid:] - INFO  [main:QuorumPeerConfig@136] - Reading configuration from: /conf/zoo.cfg
2018-08-10 06:04:56,469 [myid:] - INFO  [main:ZooKeeperServerMain@98] - Starting server
2018-08-10 06:04:56,480 [myid:] - INFO  [main:Environment@100] - Server environment:zookeeper.version=3.4.13-2d71af4dbe22557fda74f9a9b4309b15a7487f03, built on 06/29/2018 04:05 GMT
2018-08-10 06:04:56,480 [myid:] - INFO  [main:Environment@100] - Server environment:host.name=b6838b7ca21a
2018-08-10 06:04:56,480 [myid:] - INFO  [main:Environment@100] - Server environment:java.version=1.8.0_171
2018-08-10 06:04:56,480 [myid:] - INFO  [main:Environment@100] - Server environment:java.vendor=Oracle Corporation
2018-08-10 06:04:56,480 [myid:] - INFO  [main:Environment@100] - Server environment:java.home=/usr/lib/jvm/java-1.8-openjdk/jre
2018-08-10 06:04:56,480 [myid:] - INFO  [main:Environment@100] - Server environment:java.class.path=/zookeeper-3.4.13/bin/../build/classes:/zookeeper-3.4.13/bin/../build/lib/*.jar:/zookeeper-3.4.13/bin/../lib/slf4j-log4j12-1.7.25.jar:/zookeeper-3.4.13/bin/../lib/slf4j-api-1.7.25.jar:/zookeeper-3.4.13/bin/../lib/netty-3.10.6.Final.jar:/zookeeper-3.4.13/bin/../lib/log4j-1.2.17.jar:/zookeeper-3.4.13/bin/../lib/jline-0.9.94.jar:/zookeeper-3.4.13/bin/../lib/audience-annotations-0.5.0.jar:/zookeeper-3.4.13/bin/../zookeeper-3.4.13.jar:/zookeeper-3.4.13/bin/../src/java/lib/*.jar:/conf:
2018-08-10 06:04:56,480 [myid:] - INFO  [main:Environment@100] - Server environment:java.library.path=/usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64/server:/usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64:/usr/lib/jvm/java-1.8-openjdk/jre/../lib/amd64:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2018-08-10 06:04:56,480 [myid:] - INFO  [main:Environment@100] - Server environment:java.io.tmpdir=/tmp
2018-08-10 06:04:56,480 [myid:] - INFO  [main:Environment@100] - Server environment:java.compiler=<NA>
2018-08-10 06:04:56,481 [myid:] - INFO  [main:Environment@100] - Server environment:os.name=Linux
2018-08-10 06:04:56,481 [myid:] - INFO  [main:Environment@100] - Server environment:os.arch=amd64
2018-08-10 06:04:56,482 [myid:] - INFO  [main:Environment@100] - Server environment:os.version=3.10.0-693.2.2.el7.x86_64
2018-08-10 06:04:56,482 [myid:] - INFO  [main:Environment@100] - Server environment:user.name=zookeeper
2018-08-10 06:04:56,482 [myid:] - INFO  [main:Environment@100] - Server environment:user.home=/home/zookeeper
2018-08-10 06:04:56,482 [myid:] - INFO  [main:Environment@100] - Server environment:user.dir=/zookeeper-3.4.13
2018-08-10 06:04:56,499 [myid:] - INFO  [main:ZooKeeperServer@836] - tickTime set to 2000
2018-08-10 06:04:56,499 [myid:] - INFO  [main:ZooKeeperServer@845] - minSessionTimeout set to -1
2018-08-10 06:04:56,499 [myid:] - INFO  [main:ZooKeeperServer@854] - maxSessionTimeout set to -1
2018-08-10 06:04:56,512 [myid:] - INFO  [main:ServerCnxnFactory@117] - Using org.apache.zookeeper.server.NIOServerCnxnFactory as server connection factory
2018-08-10 06:04:56,521 [myid:] - INFO  [main:NIOServerCnxnFactory@89] - binding to port 0.0.0.0/0.0.0.0:2181

使用zk 命令行客户端连接zk

  • 语法:docker run -it --rm --link zookeeper:zookeeper zookeeper zkCli.sh -server zookeeper
    说明:-server zookeeper是启动zkCli.sh的参数

  • 示例:

[root@izbp13xko46hud9vfr5s94z /]# docker run -it --rm --link zookeeper:zookeeper zookeeper zkCli.sh -server zookeeper
Connecting to zookeeper
2018-08-10 06:12:08,727 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.13-2d71af4dbe22557fda74f9a9b4309b15a7487f03, built on 06/29/2018 04:05 GMT
2018-08-10 06:12:08,732 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=ce698c66d8a0
2018-08-10 06:12:08,732 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.8.0_171
2018-08-10 06:12:08,735 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2018-08-10 06:12:08,735 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/usr/lib/jvm/java-1.8-openjdk/jre
2018-08-10 06:12:08,736 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/zookeeper-3.4.13/bin/../build/classes:/zookeeper-3.4.13/bin/../build/lib/*.jar:/zookeeper-3.4.13/bin/../lib/slf4j-log4j12-1.7.25.jar:/zookeeper-3.4.13/bin/../lib/slf4j-api-1.7.25.jar:/zookeeper-3.4.13/bin/../lib/netty-3.10.6.Final.jar:/zookeeper-3.4.13/bin/../lib/log4j-1.2.17.jar:/zookeeper-3.4.13/bin/../lib/jline-0.9.94.jar:/zookeeper-3.4.13/bin/../lib/audience-annotations-0.5.0.jar:/zookeeper-3.4.13/bin/../zookeeper-3.4.13.jar:/zookeeper-3.4.13/bin/../src/java/lib/*.jar:/conf:
2018-08-10 06:12:08,736 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64/server:/usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64:/usr/lib/jvm/java-1.8-openjdk/jre/../lib/amd64:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2018-08-10 06:12:08,736 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2018-08-10 06:12:08,736 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>
2018-08-10 06:12:08,737 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Linux
2018-08-10 06:12:08,737 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=amd64
2018-08-10 06:12:08,737 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=3.10.0-693.2.2.el7.x86_64
2018-08-10 06:12:08,737 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=root
2018-08-10 06:12:08,737 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/root
2018-08-10 06:12:08,737 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/zookeeper-3.4.13
2018-08-10 06:12:08,738 [myid:] - INFO  [main:ZooKeeper@442] - Initiating client connection, connectString=zookeeper sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@4b85612c
2018-08-10 06:12:08,764 [myid:] - INFO  [main-SendThread(zookeeper:2181):ClientCnxn$SendThread@1029] - Opening socket connection to server zookeeper/172.17.0.3:2181. Will not attempt to authenticate using SASL (unknown error)
Welcome to ZooKeeper!
JLine support is enabled
2018-08-10 06:12:08,929 [myid:] - INFO  [main-SendThread(zookeeper:2181):ClientCnxn$SendThread@879] - Socket connection established to zookeeper/172.17.0.3:2181, initiating session
[zk: zookeeper(CONNECTING) 0] 2018-08-10 06:12:08,981 [myid:] - INFO  [main-SendThread(zookeeper:2181):ClientCnxn$SendThread@1303] - Session establishment complete on server zookeeper/172.17.0.3:2181, sessionid = 0x101060816ed0000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null

杀死zookeeper实例进程

  • 语法:docker kill -s KILL zookeeper
  • 示例:
[root@izbp13xko46hud9vfr5s94z /]# docker kill -s KILL zookeeper 
zookeeper
[root@izbp13xko46hud9vfr5s94z /]#

移除zookeeper实例

  • 语法:docker rm -f -v zookeeper
  • 示例:
[root@izbp13xko46hud9vfr5s94z /]# docker rm -f -v zookeeper 
zookeeper
[root@izbp13xko46hud9vfr5s94z /]#

集群方式安装

集群方式选择使用docker-compose来完成。
编写配置文件,并将其命名为:docker-compose.ymldocker-compose默认配置文件名)
配置文件内容:

version: '2'
services:
    zoo1:
        image: zookeeper
        restart: always
        container_name: zoo1
        ports:
            - "2181:2181"
        environment:
            ZOO_MY_ID: 1
            ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888

    zoo2:
        image: zookeeper
        restart: always
        container_name: zoo2
        ports:
            - "2182:2181"
        environment:
            ZOO_MY_ID: 2
            ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888

    zoo3:
        image: zookeeper
        restart: always
        container_name: zoo3
        ports:
            - "2183:2181"
        environment:
            ZOO_MY_ID: 3
            ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888

此配置文件表示,Docker需要启动三个zookeeper实例,并将2181,2182,2183三个端口号映射到容器内的2181这个端口上。
ZOO_MY_ID:表示zk服务的ID, 取值为1-255之间的整数,且必须唯一
ZOO_SERVERS:表示zk集群的主机列表

启动zookeeper集群

  • 语法:docker-compose up -d
    该命令执行需要在docker-compose配置文件的目录下执行。

  • 示例:

[root@izbp13xko46hud9vfr5s94z conf]# pwd 
/develop/conf
[root@izbp13xko46hud9vfr5s94z conf]# ls 
docker-compose-zookeeper.yml
[root@izbp13xko46hud9vfr5s94z conf]# COMPOSE_PROJECT_NAME=zookeeper_cluster docker-compose up
-bash: docker-compose: 未找到命令
[root@izbp13xko46hud9vfr5s94z conf]#

本例在执行过程中,遇到了-bash: docker-compose: 未找到命令的错误,解决方案是下载docker-compose并安装即可。
安装命令:

curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

安装完成后,重新执行docker-compose up -d这个命令,结果如下:

[root@izbp13xko46hud9vfr5s94z conf]# docker-compose up -d 
Starting zoo1 ... done
Starting zoo2 ... done
Starting zoo3 ... done
[root@izbp13xko46hud9vfr5s94z conf]# 

查看zookeeper集群实例

  • 通过docker ps查看
[root@izbp13xko46hud9vfr5s94z ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                        NAMES
e53b4c838001        zookeeper           "/docker-entrypoint.…"   4 minutes ago       Up 44 seconds       2888/tcp, 0.0.0.0:2181->2181/tcp, 3888/tcp   zoo1
19282fb6f9b4        zookeeper           "/docker-entrypoint.…"   4 minutes ago       Up 44 seconds       2888/tcp, 3888/tcp, 0.0.0.0:2182->2181/tcp   zoo2
099b926fa2d3        zookeeper           "/docker-entrypoint.…"   4 minutes ago       Up 44 seconds       2888/tcp, 3888/tcp, 0.0.0.0:2183->2181/tcp   zoo3
[root@izbp13xko46hud9vfr5s94z ~]#
  • 通过docker-compose ps查看
    这个命令需要在docker-compose配置文件下执行。
[root@izbp13xko46hud9vfr5s94z conf]# docker-compose ps 
Name              Command               State                     Ports                   
------------------------------------------------------------------------------------------
zoo1   /docker-entrypoint.sh zkSe ...   Up      0.0.0.0:2181->2181/tcp, 2888/tcp, 3888/tcp
zoo2   /docker-entrypoint.sh zkSe ...   Up      0.0.0.0:2182->2181/tcp, 2888/tcp, 3888/tcp
zoo3   /docker-entrypoint.sh zkSe ...   Up      0.0.0.0:2183->2181/tcp, 2888/tcp, 3888/tcp
[root@izbp13xko46hud9vfr5s94z conf]# 

停止、启动、重启docker-compose服务

示例:

[root@izbp13xko46hud9vfr5s94z conf]# docker-compose stop
Stopping zoo1 ... done
Stopping zoo2 ... done
Stopping zoo3 ... done
[root@izbp13xko46hud9vfr5s94z conf]# docker-compose start 
Starting zoo1 ... done
Starting zoo2 ... done
Starting zoo3 ... done
[root@izbp13xko46hud9vfr5s94z conf]# docker-compose restart
Restarting zoo1 ... done
Restarting zoo2 ... done
Restarting zoo3 ... done
[root@izbp13xko46hud9vfr5s94z conf]# 

如何查看zookeeper集群节点主从关系

使用docker exec -it zoo1 /bin/bash这个命令进入zoo1节点中,之后输入./bin/zkServer.sh statu来查看节点主从关系
示例:

[root@izbp13xko46hud9vfr5s94z conf]# docker exec -it zoo1 /bin/bash 
bash-4.4# ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /conf/zoo.cfg
Mode: follower
bash-4.4# 

参考链接:
https://blog.fengcl.com/2017/07/04/build-zookeeper-cluster-use-docker/
https://segmentfault.com/a/1190000006907443
https://my.oschina.net/xiaozhiwen/blog/1595486

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐