Docker 安装 ZooKeeper

编写 Docker-Compose 配置文件

创建 docker-compose.yml 配置文件,写入以下配置内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
version: "3.5"

services:
zookeeper:
image: bitnami/zookeeper:3.7.2
container_name: zookeeper
restart: always
ports:
- 2181:2181
environment:
- ALLOW_ANONYMOUS_LOGIN=yes
- ZOOKEEPER_CLIENT_PORT=2181
- ZOOKEEPER_TICK_TIME=2000

创建并启动 ZooKeeper 容器

1
# docker-compose up -d

测试客户端连接 ZooKeeper 容器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 连接ZooKeeper容器
# docker exec -it zookeeper /bin/bash

# 进入ZooKeeper的bin目录
# cd /opt/bitnami/zookeeper/bin

# 客户端连接ZooKeeper服务器端
$ ./zkCli.sh -server 127.0.0.1:2181

# 创建临时节点
[zk: 127.0.0.1:2181(CONNECTED) 1] create -e /test-node 123456

# 列出所有根节点
[zk: 127.0.0.1:2181(CONNECTED) 2] ls /
[test-node, zookeeper]

# 获取指定节点的值
[zk: 127.0.0.1:2181(CONNECTED) 3] get /test-node
123456

# 获取指定节点的详细信息
[zk: 127.0.0.1:2181(CONNECTED) 3] stat /test-node
cZxid = 0x4
ctime = Fri Feb 22 02:27:43 UTC 2019
mZxid = 0x4
mtime = Fri Feb 22 02:27:43 UTC 2019
pZxid = 0x4
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x1000a766c5e0001
dataLength = 6
numChildren = 0

# 断开客户端连接
[zk: 127.0.0.1:2181(CONNECTED) 4] quit