- Edit docker-compose.yaml file
version: '2'
networks:
byfn:
services:
zookeeper1:
image: zookeeper
container_name: zookeeper1
environment:
- ZOO_MY_ID=1
- ZOO_SERVERS=server.1=zookeeper1:2888:3888 server.2=zookeeper2:2888:3888 server.3=zookeeper3:2888:3888
ports:
- 2181
networks:
- byfn
zookeeper2:
image: zookeeper
container_name: zookeeper2
environment:
- ZOO_MY_ID=2
- ZOO_SERVERS=server.1=zookeeper1:2888:3888 server.2=zookeeper2:2888:3888 server.3=zookeeper3:2888:3888
ports:
- 2181
networks:
- byfn
zookeeper3:
image: zookeeper
container_name: zookeeper3
environment:
- ZOO_MY_ID=3
- ZOO_SERVERS=server.1=zookeeper1:2888:3888 server.2=zookeeper2:2888:3888 server.3=zookeeper3:2888:3888
ports:
- 2181
networks:
- byfn
Detailed configuration, refer to the document: https://hub.docker.com/_/zookeeper/
here very clearly describes how to modify the configuration, data storage, and adjust the log level and output settings.
- Start Service
$ docker-compose up
- Verify the status of a cluster
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c1f7646bf0e6 zookeeper "/docker-entrypoin..." 23 minutes ago Up 23 minutes 2888/tcp, 3888/tcp, 0.0.0.0:32794->2181/tcp zookeeper2 7e05f06c258d zookeeper "/docker-entrypoin..." 23 minutes ago Up 23 minutes 2888/tcp, 3888/tcp, 0.0.0.0:32793->2181/tcp zookeeper3 d293438c1813 zookeeper "/docker-entrypoin..." 23 minutes ago Up 23 minutes 2888/tcp, 3888/tcp, 0.0.0.0:32792->2181/tcp zookeeper1 $ docker exec -t zookeeper1 zkServer.sh status ZooKeeper JMX enabled by default Using config: /conf/zoo.cfg Mode: follower $ docker exec -t zookeeper2 zkServer.sh status ZooKeeper JMX enabled by default Using config: /conf/zoo.cfg Mode: follower $ docker exec -t zookeeper3 zkServer.sh status ZooKeeper JMX enabled by default Using config: /conf/zoo.cfg Mode: leader
Here we see three zookeeper nodes, zookeeper3 is a leader, zookeeper1 and zookeeper2 are follower.
Other status check command:
$ nc localhost 32794 <<< ruok
imok
$ echo srvr | nc localhost 32794 Zookeeper version: 3.4.13-2d71af4dbe22557fda74f9a9b4309b15a7487f03, built on 06/29/2018 04:05 GMT Latency min/avg/max: 0/7/116 Received: 40 Sent: 39 Connections: 1 Outstanding: 0 Zxid: 0x10000000e Mode: follower Node count: 5
- Using cluster
4.1 Basic Operations
[zk: localhost:2181(CONNECTED) 4] create /foo "Hello foo" Created /foo [zk: localhost:2181(CONNECTED) 5] get /foo Hello foo cZxid = 0x100000013 ctime = Fri Jan 04 02:29:47 GMT 2019 mZxid = 0x100000013 mtime = Fri Jan 04 02:29:47 GMT 2019 pZxid = 0x100000013 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 9 numChildren = 0 [zk: localhost:2181(CONNECTED) 6] ls /foo []
4.2 Creating a path create
[zk: localhost:2181(CONNECTED) 7] create /foo1/foo1 "Hello foo1" Node does not exist: /foo1/foo1
Creation failed because the zookeeper does not support recursion created, you must first create a level of parent directory node.
When you create a parent directory, you can let the value is empty. (In fact, a directory also can bring value, the two do not conflict.)
[zk: localhost:2181(CONNECTED) 14] create /foo1 "" # 或者:create /foo1 "" Created /foo1 [zk: localhost:2181(CONNECTED) 15] create /foo1/foo1 "Hello foo1" Created /foo1/foo1
4.3 ls View element node
[zk: localhost:2181(CONNECTED) 21] ls /foo1 [foo1]
Because / foo1 is a directory, he had a child node below / foo1 / foo1
[zk: localhost:2181(CONNECTED) 21] ls /foo1/foo1 []
Because / foo1 / foo1 is a meta-node, the node has no child below him.
Note, ls command can list a directory, you can not list all the subdirectories recursively.
4.4 Gets the value get
get /foo1/foo1
4.5 modify the value set
set /foo1/foo1 'newvalue'
4.6 delete nodes delete
delete /foo1/foo1
Note that you can only delete empty child node; so if you want to delete a node, you must first of all child nodes are removed.
If you want to delete a non-empty directory, you can also use rmr command.
Author: CodingCode
link: https: //www.jianshu.com/p/8d31ede99206
Source: Jane books
are copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.