使用docker-compose搭建zookeeper集群环境
zookeeper是一个集群环境,用来管理微服务架构下面的配置管理功能。
这篇文章是一个基础步骤如何搭建zookeeper的docker集群环境。
我们使用docker-compose来搭建如下的zookeeper集群环境:
- 集群包含三个node:zookeeper1, zookeeper2, zookeeper3
- 下载zookeeper docker image
$ docker pull zookeeper
- 编辑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/
这里非常清楚的描述了如何修改配置,存储数据,以及调整日志级别以及输出等设置。
- 启动服务
$ 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。
其他状态检查命令:
$ 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 基本操作
[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不支持递归创建,必须先一级一级的创建父目录节点。
创建父目录时,让值为空就可以。(其实目录也可以带值,两者并不冲突。)
[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
[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命令。