安装
1.拉取镜像
docker pull zookeeper
2.启动单实例
- 执行命令
#--name 为容器指定名称
#-d 指定为后台运行
#--restart 容器的重启策略
# no - 默认值
# on-failure[:max-retries] 只在容器以非0状态码退出时重启
# always 一直直尝试 不管是什么状态码
docker run --name some-zookeeper --restart no -d zookeeper
- 查看日志
e24-6:~ dailong$ docker run --name some-zookeeper --restart no -d zookeeper
ba8876734a156e1652aad1de156c50a69a71eecbf0df74f6788510130fe0bab7
e24-6:~ dailong$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ba8876734a15 zookeeper "/docker-entrypoint.…" 4 seconds ago Up 3 seconds 2181/tcp, 2888/tcp, 3888/tcp, 8080/tcp some-zookeeper
e24-6:~ dailong$ docker logs -f ba8876734a15
ZooKeeper JMX enabled by default
Using config: /conf/zoo.cfg
2020-01-20 05:47:50,696 [myid:] - INFO [main:QuorumPeerConfig@133] - Reading configuration from: /conf/zoo.cfg
2020-01-20 05:47:50,702 [myid:] - INFO [main:QuorumPeerConfig@375] - clientPort is not set
2020-01-20 05:47:50,702 [myid:] - INFO [main:QuorumPeerConfig@389] - secureClientPort is not set
2020-01-20 05:47:50,709 [myid:] - ERROR [main:QuorumPeerConfig@645] - Invalid configuration, only one server specified (ignoring)
2020-01-20 05:47:50,716 [myid:1] - INFO [main:DatadirCleanupManager@78] - autopurge.snapRetainCount set to 3
2020-01-20 05:47:50,716 [myid:1] - INFO [main:DatadirCleanupManager@79] - autopurge.purgeInterval set to 0
2020-01-20 05:47:50,716 [myid:1] - INFO [main:DatadirCleanupManager@101] - Purge task is not scheduled.
2020-01-20 05:47:50,717 [myid:1] - WARN [main:QuorumPeerMain@125] - Either no config or no quorum defined in config, running in standalone mode
2020-01-20 05:47:50,718 [myid:1] - INFO [main:ManagedUtil@46] - Log4j found with jmx enabled.
2020-01-20 05:47:50,733 [myid:1] - INFO [main:QuorumPeerConfig@133] - Reading configuration from: /conf/zoo.cfg
2020-01-20 05:47:50,733 [myid:1] - INFO [main:QuorumPeerConfig@375] - clientPort is not set
2020-01-20 05:47:50,733 [myid:1] - INFO [main:QuorumPeerConfig@389] - secureClientPort is not set
2020-01-20 05:47:50,734 [myid:1] - ERROR [main:QuorumPeerConfig@645] - Invalid configuration, only one server specified (ignoring)
2020-01-20 05:47:50,734 [myid:1] - INFO [main:ZooKeeperServerMain@117] - Starting server
2020-01-20 05:47:50,738 [myid:1] - INFO [main:FileTxnSnapLog@103] - zookeeper.snapshot.trust.empty : false
2020-01-20 05:47:50,753 [myid:1] - INFO [main:Environment@109] - Server environment:zookeeper.version=3.5.6-c11b7e26bc554b8523dc929761dd28808913f091, built on 10/08/2019 20:18 GMT
2020-01-20 05:47:50,755 [myid:1] - INFO [main:Environment@109] - Server environment:host.name=ba8876734a15
2020-01-20 05:47:50,755 [myid:1] - INFO [main:Environment@109] - Server environment:java.version=1.8.0_232
2020-01-20 05:47:50,755 [myid:1] - INFO [main:Environment@109] - Server environment:java.vendor=Oracle Corporation
2020-01-20 05:47:50,756 [myid:1] - INFO [main:Environment@109] - Server environment:java.home=/usr/local/openjdk-8
2020-01-20 05:47:50,756 [myid:1] - INFO [main:Environment@109] - Server environment:java.class.path=/apache-zookeeper-3.5.6-bin/bin/../zookeeper-server/target/classes:/apache-zookeeper-3.5.6-bin/bin/../build/classes:/apache-zookeeper-3.5.6-bin/bin/../zookeeper-server/target/lib/*.jar:/apache-zookeeper-3.5.6-bin/bin/../build/lib/*.jar:/apache-zookeeper-3.5.6-bin/bin/../lib/zookeeper-jute-3.5.6.jar:/apache-zookeeper-3.5.6-bin/bin/../lib/zookeeper-3.5.6.jar:/apache-zookeeper-3.5.6-bin/bin/../lib/slf4j-log4j12-1.7.25.jar:/apache-zookeeper-3.5.6-bin/bin/../lib/slf4j-api-1.7.25.jar:/apache-zookeeper-3.5.6-bin/bin/../lib/netty-transport-native-unix-common-4.1.42.Final.jar:/apache-zookeeper-3.5.6-bin/bin/../lib/netty-transport-native-epoll-4.1.42.Final.jar:/apache-zookeeper-3.5.6-bin/bin/../lib/netty-transport-4.1.42.Final.jar:/apache-zookeeper-3.5.6-bin/bin/../lib/netty-resolver-4.1.42.Final.jar:/apache-zookeeper-3.5.6-bin/bin/../lib/netty-handler-4.1.42.Final.jar:/apache-zookeeper-3.5.6-bin/bin/../lib/netty-common-4.1.42.Final.jar:/apache-zookeeper-3.5.6-bin/bin/../lib/netty-codec-4.1.42.Final.jar:/apache-zookeeper-3.5.6-bin/bin/../lib/netty-buffer-4.1.42.Final.jar:/apache-zookeeper-3.5.6-bin/bin/../lib/log4j-1.2.17.jar:/apache-zookeeper-3.5.6-bin/bin/../lib/json-simple-1.1.1.jar:/apache-zookeeper-3.5.6-bin/bin/../lib/jline-2.11.jar:/apache-zookeeper-3.5.6-bin/bin/../lib/jetty-util-9.4.17.v20190418.jar:/apache-zookeeper-3.5.6-bin/bin/../lib/jetty-servlet-9.4.17.v20190418.jar:/apache-zookeeper-3.5.6-bin/bin/../lib/jetty-server-9.4.17.v20190418.jar:/apache-zookeeper-3.5.6-bin/bin/../lib/jetty-security-9.4.17.v20190418.jar:/apache-zookeeper-3.5.6-bin/bin/../lib/jetty-io-9.4.17.v20190418.jar:/apache-zookeeper-3.5.6-bin/bin/../lib/jetty-http-9.4.17.v20190418.jar:/apache-zookeeper-3.5.6-bin/bin/../lib/javax.servlet-api-3.1.0.jar:/apache-zookeeper-3.5.6-bin/bin/../lib/jackson-databind-2.9.10.jar:/apache-zookeeper-3.5.6-bin/bin/../lib/jackson-core-2.9.10.jar:/apache-zookeeper-3.5.6-bin/bin/../lib/jackson-annotations-2.9.10.jar:/apache-zookeeper-3.5.6-bin/bin/../lib/commons-cli-1.2.jar:/apache-zookeeper-3.5.6-bin/bin/../lib/audience-annotations-0.5.0.jar:/apache-zookeeper-3.5.6-bin/bin/../zookeeper-*.jar:/apache-zookeeper-3.5.6-bin/bin/../zookeeper-server/src/main/resources/lib/*.jar:/conf:
2020-01-20 05:47:50,757 [myid:1] - INFO [main:Environment@109] - Server environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2020-01-20 05:47:50,757 [myid:1] - INFO [main:Environment@109] - Server environment:java.io.tmpdir=/tmp
2020-01-20 05:47:50,757 [myid:1] - INFO [main:Environment@109] - Server environment:java.compiler=<NA>
2020-01-20 05:47:50,757 [myid:1] - INFO [main:Environment@109] - Server environment:os.name=Linux
2020-01-20 05:47:50,757 [myid:1] - INFO [main:Environment@109] - Server environment:os.arch=amd64
2020-01-20 05:47:50,758 [myid:1] - INFO [main:Environment@109] - Server environment:os.version=4.9.184-linuxkit
2020-01-20 05:47:50,758 [myid:1] - INFO [main:Environment@109] - Server environment:user.name=zookeeper
2020-01-20 05:47:50,758 [myid:1] - INFO [main:Environment@109] - Server environment:user.home=/home/zookeeper
2020-01-20 05:47:50,758 [myid:1] - INFO [main:Environment@109] - Server environment:user.dir=/apache-zookeeper-3.5.6-bin
2020-01-20 05:47:50,758 [myid:1] - INFO [main:Environment@109] - Server environment:os.memory.free=37MB
2020-01-20 05:47:50,758 [myid:1] - INFO [main:Environment@109] - Server environment:os.memory.max=889MB
2020-01-20 05:47:50,759 [myid:1] - INFO [main:Environment@109] - Server environment:os.memory.total=46MB
2020-01-20 05:47:50,762 [myid:1] - INFO [main:ZooKeeperServer@938] - minSessionTimeout set to 4000
2020-01-20 05:47:50,762 [myid:1] - INFO [main:ZooKeeperServer@947] - maxSessionTimeout set to 40000
2020-01-20 05:47:50,763 [myid:1] - INFO [main:ZooKeeperServer@166] - Created server with tickTime 2000 minSessionTimeout 4000 maxSessionTimeout 40000 datadir /datalog/version-2 snapdir /data/version-2
2020-01-20 05:47:50,797 [myid:1] - INFO [main:Log@193] - Logging initialized @518ms to org.eclipse.jetty.util.log.Slf4jLog
2020-01-20 05:47:50,938 [myid:1] - WARN [main:ContextHandler@1588] - o.e.j.s.ServletContextHandler@5649fd9b{/,null,UNAVAILABLE} contextPath ends with /*
2020-01-20 05:47:50,938 [myid:1] - WARN [main:ContextHandler@1599] - Empty contextPath
2020-01-20 05:47:50,963 [myid:1] - INFO [main:Server@370] - jetty-9.4.17.v20190418; built: 2019-04-18T19:45:35.259Z; git: aa1c656c315c011c01e7b21aabb04066635b9f67; jvm 1.8.0_232-b09
2020-01-20 05:47:51,009 [myid:1] - INFO [main:DefaultSessionIdManager@365] - DefaultSessionIdManager workerName=node0
2020-01-20 05:47:51,009 [myid:1] - INFO [main:DefaultSessionIdManager@370] - No SessionScavenger set, using defaults
2020-01-20 05:47:51,012 [myid:1] - INFO [main:HouseKeeper@149] - node0 Scavenging every 660000ms
2020-01-20 05:47:51,028 [myid:1] - INFO [main:ContextHandler@855] - Started o.e.j.s.ServletContextHandler@5649fd9b{/,null,AVAILABLE}
2020-01-20 05:47:51,041 [myid:1] - INFO [main:AbstractConnector@292] - Started ServerConnector@4520ebad{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
2020-01-20 05:47:51,041 [myid:1] - INFO [main:Server@410] - Started @770ms
2020-01-20 05:47:51,042 [myid:1] - INFO [main:JettyAdminServer@112] - Started AdminServer on address 0.0.0.0, port 8080 and command URL /commands
2020-01-20 05:47:51,046 [myid:1] - INFO [main:ServerCnxnFactory@135] - Using org.apache.zookeeper.server.NIOServerCnxnFactory as server connection factory
2020-01-20 05:47:51,049 [myid:1] - INFO [main:NIOServerCnxnFactory@673] - Configuring NIO connection handler with 10s sessionless connection timeout, 1 selector thread(s), 4 worker threads, and 64 kB direct buffers.
2020-01-20 05:47:51,050 [myid:1] - INFO [main:NIOServerCnxnFactory@686] - binding to port /0.0.0.0:2181
2020-01-20 05:47:51,064 [myid:1] - INFO [main:ZKDatabase@117] - zookeeper.snapshotSizeFactor = 0.33
2020-01-20 05:47:51,067 [myid:1] - INFO [main:FileTxnSnapLog@384] - Snapshotting: 0x0 to /data/version-2/snapshot.0
2020-01-20 05:47:51,069 [myid:1] - INFO [main:FileTxnSnapLog@384] - Snapshotting: 0x0 to /data/version-2/snapshot.0
2020-01-20 05:47:51,086 [myid:1] - INFO [main:ContainerManager@64] - Using checkIntervalMs=60000 maxPerMinute=10000
3.启动多实例集群,利用docker-compose
e24-6:docker dailong$ cat docker-compose.yml
version: '3.1'
services:
zoo1:
image: zookeeper
restart: always
hostname: zoo1
ports:
- 2181:2181
environment:
ZOO_MY_ID: 1
ZOO_SERVERS: server.1=0.0.0.0:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181
zoo2:
image: zookeeper
restart: always
hostname: zoo2
ports:
- 2182:2181
environment:
ZOO_MY_ID: 2
ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=0.0.0.0:2888:3888;2181 server.3=zoo3:2888:3888;2181
zoo3:
image: zookeeper
restart: always
hostname: zoo3
ports:
- 2183:2181
environment:
ZOO_MY_ID: 3
ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=0.0.0.0:2888:3888;2181
- 到yml文件目录,运行命令即可:
e24-6:docker dailong$ ls
docker-compose.yml
e24-6:docker dailong$ docker-compose up -d
Creating docker_zoo1_1 ... done
Creating docker_zoo2_1 ... done
Creating docker_zoo3_1 ... done
e24-6:docker dailong$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
84ee34d1533c zookeeper "/docker-entrypoint.…" 20 seconds ago Up 17 seconds 2888/tcp, 3888/tcp, 8080/tcp, 0.0.0.0:2183->2181/tcp docker_zoo3_1
9e86f2fbd18a zookeeper "/docker-entrypoint.…" 20 seconds ago Up 17 seconds 2888/tcp, 3888/tcp, 8080/tcp, 0.0.0.0:2182->2181/tcp docker_zoo2_1
adffe1bfb865 zookeeper "/docker-entrypoint.…" 20 seconds ago Up 17 seconds 2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp, 8080/tcp docker_zoo1_1
e24-6:docker dailong$
我之前也没接触过,只听说过了解,使用后面补
参考链接
https://hub.docker.com/_/zookeeper 内有详细安装步骤
https://docs.docker.com/install/ docker文档