获取镜像
# 查看可用的稳定版本
sudo docker search zookeeper
sudo docker pull zookeeper:3.4.10
sudo docker image ls |grep zookeeper
服务编排
mkdir -p /share/zookeeper && cd /share/zookeeper
rm -rf /share/zookeeper/docker-compose.yml
vi /share/zookeeper/docker-compose.yml
服务编排内容如下:
version: "2"
services:
zoo1:
# 指定镜像名称或镜像ID
image: zookeeper:3.4.10
# 给容器设置标签
labels:
- "desc=This is 3 node zookeeper-cluster"
- "author=polaris"
- "date=2020-04-21 19:20:11"
# 容器将始终重新启动
restart: always
# 主机名
hostname: zoo1
# 特权模式(能使用systemctl)
privileged: true
# 指定容器的名称 (等同于 docker run --name 的作用)
container_name: zoo1
networks:
zk_net:
ipv4_address: 172.24.11.5
ports:
- 2181:2181
environment:
ZOO_MY_ID: 1
ZOO_SERVERS: server.1=0.0.0.0:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
zoo2:
image: zookeeper:3.4.10
restart: always
hostname: zoo2
privileged: true
container_name: zoo2
networks:
zk_net:
ipv4_address: 172.24.11.6
ports:
- 2182:2181
environment:
ZOO_MY_ID: 2
ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=0.0.0.0:2888:3888 server.3=zoo3:2888:3888
zoo3:
image: zookeeper:3.4.10
restart: always
hostname: zoo3
privileged: true
container_name: zoo3
networks:
zk_net:
ipv4_address: 172.24.11.7
ports:
- 2183:2181
environment:
ZOO_MY_ID: 3
ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=0.0.0.0:2888:3888
networks:
zk_net:
ipam:
driver: default
config:
- subnet: "172.24.11.0/24"
服务构建
cd /share/zookeeper
sudo docker-compose -f docker-compose.yml build --no-cache # 不带缓存构建(只创建镜像,不会启动容器)
sudo docker-compose -f docker-compose.yml up -d # 构建后运行
sudo docker-compose -f docker-compose.yml up --build # 跟踪方式构建,可用于调试
验证
cd /share/zookeeper
# 查看进程
sudo docker-compose -f docker-compose.yml ps
# 查看zk状态(zookeeper_1为容器名称)
sudo docker exec -t zoo1 zkServer.sh status
# zk节点操作验证(CRUD)
sudo docker exec -it zoo1 zkCli.sh -server localhost:2181
zkcli>> ls / # 查看zk根目录
zkcli>> create /zk mydata # 创建zk节点
zkcli>> get /zk # 查看zk节点数据
zkcli>> set /zk "newData" # 更新zk节点数据
zkcli>> delete /zk # 删除zk节点
zkcli>> quit # 退出zkcli
# 查看日志
sudo docker logs zoo1
sudo docker logs -f -t --tail=50 zoo1
# 查看网络
sudo docker network ls
sudo docker inspect zookeeper_zk_net
# 停止
sudo docker-compose -f docker-compose.yml stop
# 移除
sudo docker-compose -f docker-compose.yml down
宿主机或者其他局域网的机器,可以通过宿主机IP及暴露的端口访问zk集群(举例:192.168.0.xxx:2181,192.168.0.xxx:2182,192.168.0.xxx:2183)。