获取镜像

  1. # 查看可用的稳定版本
  2. sudo docker search zookeeper
  3. sudo docker pull zookeeper:3.4.10
  4. 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"

networks:
  zkCluster:

services:
  zoo1:
    build: ./zookeeper
    restart: always
    hostname: zoo1
    container_name: zoo1
    networks:
        - zkCluster
    ports:
      - 2181:2181
    volumes:
      - ./zookeeper/zoo1/data:/data
      - ./zookeeper/zoo1/datalog:/datalog
    environment:
      ZOO_MY_ID: 1
      ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888

  zoo2:
    build: ./zookeeper
    restart: always
    hostname: zoo2
    container_name: zoo2
    networks:
        - zkCluster
    ports:
      - 2182:2181
    volumes:
      - ./zookeeper/zoo2/data:/data
      - ./zookeeper/zoo2/datalog:/datalog
    environment:
      ZOO_MY_ID: 2
      ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888

  zoo3:
    build: ./zookeeper
    restart: always
    hostname: zoo3
    container_name: zoo3
    networks:
        - zkCluster
    ports:
      - 2183:2181
    volumes:
      - ./zookeeper/zoo3/data:/data
      - ./zookeeper/zoo3/datalog:/datalog
    environment:
      ZOO_MY_ID: 3
      ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888

服务配置

Φ 配置文件概览

以下配置相关文件保持与编排文件相同目录。

d zookeeper                 # 初始化配置
d     zoo1                    # zoo1配置
d         data                  # 数据缓存目录
d         datalog               # 日志目录
d     zoo2                    # zoo2配置
d         data                  # 数据缓存目录
d         datalog               # 日志目录
d     zoo3                    # zoo3配置
d         data                  # 数据缓存目录
d         datalog               # 日志目录
-    Dockerfile

Φ Dockerfile

FROM zookeeper:3.4.10

服务构建

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状态(zoo1为容器名称)
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 zoo1
# 停止
sudo docker-compose -f docker-compose.yml stop
# 移除
sudo docker-compose -f docker-compose.yml down

宿主机或者其他局域网的机器,可以通过宿主机IP及暴露的端口访问zk集群(举例:192.168.0.101:2181,192.168.0.101:2182,192.168.0.101:2183)。

附件

docker-compose.zookeeper.zip