- 创建Docker容器
[docker@localhost ~]$ docker run -d -p 10101:22 --name zookeeper-01 jk/centos7-ssh /usr/sbin/sshd -D
- 下载Zookeeper
https://zookeeper.apache.org/ #官网
#下载
[root@e2e73715f903 tmp]# wget http://apache.fayea.com/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz
- 安装
[root@e2e73715f903 tmp]# tar -zxvf zookeeper-3.4.10.tar.gz
[root@e2e73715f903 zookeeper]# cd conf/
[root@e2e73715f903 conf]# ls
configuration.xsl log4j.properties zoo_sample.cfg
[root@e2e73715f903 conf]# ls -lh
total 12K
-rw-rw-r-- 1 1001 1001 535 Mar 23 10:14 configuration.xsl
-rw-rw-r-- 1 1001 1001 2.2K Mar 23 10:14 log4j.properties
-rw-rw-r-- 1 1001 1001 922 Mar 23 10:14 zoo_sample.cfg
[root@e2e73715f903 conf]# mv zoo_sample.cfg zoo.cfg
[root@e2e73715f903 conf]# vi zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/root/data
clientPort=2181
dataLogDir=/root/logs
server.2=172.17.0.2:2888:3888
server.3=172.17.0.3:2888:3888
server.4=172.17.0.4:2888:3888
#设置myid(data)目录下
[root@e2e73715f903 ~]# cd data/
[root@e2e73715f903 data]# ls
myid
[root@e2e73715f903 data]# more myid
2
#启动服务
[root@e2e73715f903 bin]# ./zkServer.sh start
- 制作镜像
#提交为镜像
[docker@localhost ~]$ docker commit e2e73715f903 jk/zookeeper-3.4.10
[docker@localhost ~]$ docker run -d -p 10201:22 -p 10202:2181 --name zookeeper-02 jk/zookeeper-3.4.10 /usr/sbin/sshd -D
- 启动集群服务
[docker@localhost ~]$ docker stop zookeeper-01
[docker@localhost ~]$ docker exec zookeeper-01 /root/zookeeper/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /root/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
#状态
[docker@localhost ~]$ docker exec zookeeper-01 /root/zookeeper/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/zookeeper/bin/../conf/zoo.cfg
Mode: follower
[docker@localhost ~]$ docker exec zookeeper-02 /root/zookeeper/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/zookeeper/bin/../conf/zoo.cfg
Mode: leader
[docker@localhost ~]$ docker exec zookeeper-03 /root/zookeeper/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/zookeeper/bin/../conf/zoo.cfg
Mode: follower
- 客服端连接
[docker@localhost ~]$ docker exec zookeeper-02 /root/zookeeper/bin/zkCli.sh -server localhost:2181
Connecting to localhost:2181
2017-08-07 07:30:42,485 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT
2017-08-07 07:30:42,487 [myid:] - INFO [main:Environment@100] - Client environment:host.name=8465048b3eee
2017-08-07 07:30:42,488 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_111
2017-08-07 07:30:42,489 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2017-08-07 07:30:42,489 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/java/jdk1.8.0_111/jre
2017-08-07 07:30:42,489 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/root/zookeeper/bin/../build/classes:/root/zookeeper/bin/../build/lib/*.jar:/root/zookeeper/bin/../lib/slf4j-log4j12-1.6.1.jar:/root/zookeeper/bin/../lib/slf4j-api-1.6.1.jar:/root/zookeeper/bin/../lib/netty-3.10.5.Final.jar:/root/zookeeper/bin/../lib/log4j-1.2.16.jar:/root/zookeeper/bin/../lib/jline-0.9.94.jar:/root/zookeeper/bin/../zookeeper-3.4.10.jar:/root/zookeeper/bin/../src/java/lib/*.jar:/root/zookeeper/bin/../conf:
2017-08-07 07:30:42,489 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2017-08-07 07:30:42,489 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2017-08-07 07:30:42,489 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2017-08-07 07:30:42,490 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2017-08-07 07:30:42,490 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2017-08-07 07:30:42,490 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-514.21.1.el7.x86_64
2017-08-07 07:30:42,490 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
2017-08-07 07:30:42,490 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
2017-08-07 07:30:42,490 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/
2017-08-07 07:30:42,491 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@506c589e
Welcome to ZooKeeper!
2017-08-07 07:30:42,504 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2017-08-07 07:30:42,544 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@876] - Socket connection established to localhost/0:0:0:0:0:0:0:1:2181, initiating session
[zk: localhost:2181(CONNECTING) 0] 2017-08-07 07:30:42,733 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, sessionid = 0x35dbb91e6440000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
- 查看ZooKeeper是否启动
echo ruok | nc localhost 2181