- 編輯docker-compose.yaml文件
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
詳細的配置請參考文檔:https://hub.docker.com/_/zookeeper/
這裏很是清楚的描述瞭如何修改配置,存儲數據,以及調整日誌級別以及輸出等設置。ruby
- 啓動服務
$ docker-compose up
- 驗證集羣的狀態
$ 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
這裏咱們看到三個zookeeper節點,zookeeper3是leader,zookeeper1和zookeeper2是follower。bash
其餘狀態檢查命令:架構
$ 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
- 集羣的使用
4.1 基本操做tcp
[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 建立路徑create微服務
[zk: localhost:2181(CONNECTED) 7] create /foo1/foo1 "Hello foo1" Node does not exist: /foo1/foo1
建立失敗,由於zookeeper不支持遞歸建立,必須先一級一級的建立父目錄節點。
建立父目錄時,讓值爲空就能夠。(其實目錄也能夠帶值,二者並不衝突。)ui
[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 查看節點的元素lsspa
[zk: localhost:2181(CONNECTED) 21] ls /foo1 [foo1]
由於/foo1是一個目錄,他下面有一個子節點/foo1/foo1
[zk: localhost:2181(CONNECTED) 21] ls /foo1/foo1 []
由於/foo1/foo1是一個元節點,他下面沒有子節點了。
注意,ls命令只能列出一級目錄,不能遞歸列出全部的子目錄。
4.4 獲取值get
get /foo1/foo1
4.5 修改值set
set /foo1/foo1 'newvalue'
4.6 刪除節點delete
delete /foo1/foo1
注意只能刪除子節點爲空的節點;因此若是要刪除一個節點,必須先把全部的子節點都刪除。 若是要刪除非空目錄,還能夠使用rmr命令。