获取镜像

  1. # 查看可用的稳定版本
  2. sudo docker search zookeeper
  3. sudo docker pull zookeeper:3.4.10
  4. sudo docker image ls |grep zookeeper

服务编排

  1. mkdir -p /share/zookeeper && cd /share/zookeeper
  2. rm -rf /share/zookeeper/docker-compose.yml
  3. vi /share/zookeeper/docker-compose.yml

服务编排内容如下:

  1. version: "2"
  2. services:
  3. zoo1:
  4. # 指定镜像名称或镜像ID
  5. image: zookeeper:3.4.10
  6. # 给容器设置标签
  7. labels:
  8. - "desc=This is 3 node zookeeper-cluster"
  9. - "author=polaris"
  10. - "date=2020-04-21 19:20:11"
  11. # 容器将始终重新启动
  12. restart: always
  13. # 主机名
  14. hostname: zoo1
  15. # 特权模式(能使用systemctl)
  16. privileged: true
  17. # 指定容器的名称 (等同于 docker run --name 的作用)
  18. container_name: zoo1
  19. networks:
  20. zk_net:
  21. ipv4_address: 172.24.11.5
  22. ports:
  23. - 2181:2181
  24. environment:
  25. ZOO_MY_ID: 1
  26. ZOO_SERVERS: server.1=0.0.0.0:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
  27. zoo2:
  28. image: zookeeper:3.4.10
  29. restart: always
  30. hostname: zoo2
  31. privileged: true
  32. container_name: zoo2
  33. networks:
  34. zk_net:
  35. ipv4_address: 172.24.11.6
  36. ports:
  37. - 2182:2181
  38. environment:
  39. ZOO_MY_ID: 2
  40. ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=0.0.0.0:2888:3888 server.3=zoo3:2888:3888
  41. zoo3:
  42. image: zookeeper:3.4.10
  43. restart: always
  44. hostname: zoo3
  45. privileged: true
  46. container_name: zoo3
  47. networks:
  48. zk_net:
  49. ipv4_address: 172.24.11.7
  50. ports:
  51. - 2183:2181
  52. environment:
  53. ZOO_MY_ID: 3
  54. ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=0.0.0.0:2888:3888
  55. networks:
  56. zk_net:
  57. ipam:
  58. driver: default
  59. config:
  60. - 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)。