1. 程式人生 > >Docker 部署 zookeeper

Docker 部署 zookeeper

mkdir -p /swarm/volumes/zk-{1..3}

建立yaml檔案

cat > zookeeper.yaml <<'EOF'
version: '3.7'

services:
  zk-1:
    image: zookeeper:3.4.14
    restart: always
    hostname: zk-1
    container_name: zk-1
    ports:
      - 2181:2181
    environment:
      TZ: Asia/Shanghai
      ZOO_MY_ID: 1
      ZOO_SERVERS: server.1=zk-1:2888:3888 server.2=zk-2:2888:3888 server.3=zk-3:2888:3888
    volumes:
      - type: bind
        source: /swarm/volumes/zk-1
        target: /data
    deploy:
      placement:
        constraints:
          - node.hostname == node01
    healthcheck:
      test: echo ruok | nc 127.0.0.1 2181 || exit 1
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 20s
    networks:
      app_net:
        ipv4_address: 10.10.10.11

  zk-2:
    image: zookeeper:3.4.14
    restart: always
    hostname: zk-2
    container_name: zk-2
    ports:
      - 2182:2181
    environment:
      TZ: Asia/Shanghai
      ZOO_MY_ID: 2
      ZOO_SERVERS: server.1=zk-1:2888:3888 server.2=zk-2:2888:3888 server.3=zk-3:2888:3888
    volumes:
      - type: bind
        source: /swarm/volumes/zk-2
        target: /data
    deploy:
      placement:
        constraints:
          - node.hostname == node02
    healthcheck:
      test:  echo ruok | nc 127.0.0.1 2181 || exit 1
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 20s
    networks:
      app_net:
        ipv4_address: 10.10.10.12

  zk-3:
    image: zookeeper:3.4.14
    restart: always
    hostname: zk-3
    container_name: zk-3
    ports:
      - 2183:2181
    environment:
      TZ: Asia/Shanghai
      ZOO_MY_ID: 3
      ZOO_SERVERS: server.1=zk-1:2888:3888 server.2=zk-2:2888:3888 server.3=zk-3:2888:3888
    volumes:
      - type: bind
        source: /swarm/volumes/zk-3
        target: /data
    deploy:
      placement:
        constraints:
          - node.hostname == node03
    healthcheck:
      test:  echo ruok | nc 127.0.0.1 2181 || exit 1
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 20s
    networks:
      app_net:
        ipv4_address: 10.10.10.13

networks:
  app_net:
    ipam:
      driver: default
      config:
        - subnet: "10.10.10.0/24"
EOF

啟動

docker-compose -f zookeeper.yaml up -d
docker-compose -f zookeeper.yaml ps

檢測服務狀態

for i in zk-1 zk-2 zk-3; do
    echo "---------------- $i ----------------"
    docker exec $i zkServer.sh status
done

檢測埠狀態輸出(imok為正常)

# 內部IP
echo ruok | nc 10.10.10.11 2181; echo
echo ruok | nc 10.10.10.12 2181; echo
echo ruok | nc 10.10.10.13 2181; echo

# 外部訪問(替換 127.0.0.1 為宿主機IP)
echo ruok | nc 127.0.0.1 2181; echo
echo ruok | nc 127.0.0.1 2182; echo
echo ruok | nc 127.0.0.1 2183; echo

刪除

docker-compose -f zookeeper.yaml down -v
  • docker swarm 方式部署
  1. 叢集部署 可指定容器執行在哪個宿主機
  2. 健康策略如果不滿足 退出容器重啟一個
  3. 無法使用固定IP,無法指定容器名稱

建立資料目錄

mkdir -p /swarm/volumes/zk-{1..3}

建立yaml檔案

cat > zookeeper.yaml <<'EOF'
version: '3.7'

services:
  zk-1:
    image: zookeeper:3.4.14
    restart: always
    hostname: zk-1
    ports:
      - target: 2181
        published: 2181
        protocol: tcp
        mode: host         # ingress 群集模式負載平衡(default) # host 僅當前執行容器的宿主機上釋出主機埠
    environment:
      TZ: Asia/Shanghai
      ZOO_MY_ID: 1
      ZOO_SERVERS: server.1=zk-1:2888:3888 server.2=zk-2:2888:3888 server.3=zk-3:2888:3888
    volumes:
      - type: bind
        source: /swarm/volumes/zk-1
        target: /data
    deploy:
      placement:
        constraints:
          - node.hostname == node01
    healthcheck:
      test: echo ruok | nc 127.0.0.1 2181 || exit 1
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 20s

  zk-2:
    image: zookeeper:3.4.14
    restart: always
    hostname: zk-2
    ports:
      - target: 2181
        published: 2181
        protocol: tcp
        mode: host         # ingress 群集模式負載平衡(default) # host 僅當前執行容器的宿主機上釋出主機埠
    environment:
      TZ: Asia/Shanghai
      ZOO_MY_ID: 2
      ZOO_SERVERS: server.1=zk-1:2888:3888 server.2=zk-2:2888:3888 server.3=zk-3:2888:3888
    volumes:
      - type: bind
        source: /swarm/volumes/zk-2
        target: /data
    deploy:
      placement:
        constraints:
          - node.hostname == node02
    healthcheck:
      test:  echo ruok | nc 127.0.0.1 2181 || exit 1
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 20s

  zk-3:
    image: zookeeper:3.4.14
    restart: always
    hostname: zk-3
    ports:
      - target: 2181
        published: 2181
        protocol: tcp
        mode: host         # ingress 群集模式負載平衡(default) # host 僅當前執行容器的宿主機上釋出主機埠
    environment:
      TZ: Asia/Shanghai
      ZOO_MY_ID: 3
      ZOO_SERVERS: server.1=zk-1:2888:3888 server.2=zk-2:2888:3888 server.3=zk-3:2888:3888
    volumes:
      - type: bind
        source: /swarm/volumes/zk-3
        target: /data
    deploy:
      placement:
        constraints:
          - node.hostname == node03
    healthcheck:
      test:  echo ruok | nc 127.0.0.1 2181 || exit 1
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 20s
EOF

部署

docker stack deploy -c ./zookeeper.yaml zookeeper
docker stack ps zookeeper; docker service ls

檢測服務狀態

name=zookeeper; cmd='zkServer.sh status'
docker exec $(docker ps | grep $name | awk '{print $1}') $cmd
echo ruok | nc node01 2181; echo
echo ruok | nc node02 2181; echo
echo ruok | nc node03 2181; echo

刪除

docker stack rm zookeeper
  • 登入容器
# 進入容器
docker exec -it $(docker ps | grep zookeeper | awk '{print $1}') /bin/bash

# 替換軟體源
cat > /etc/apt/sources.list <<EOF
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates main contrib non-free
EOF

# 安裝軟體
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends procps iputils-pin