docker-compose一键搭建zookeeper集群

1、setup.sh

setup.sh脚本用来创建本地文件夹,这些文件夹会被挂载到docker容器。

#!/bin/bash

mkdir -p "$PWD\zoo1\data"
mkdir -p "$PWD\zoo1\datalog"

mkdir -p "$PWD\zoo2\data"
mkdir -p "$PWD\zoo2\datalog"

mkdir -p "$PWD\zoo3\data"
mkdir -p "$PWD\zoo3\datalog"

生成的文件夹
docker-compose一键搭建zookeeper集群_第1张图片

2、docker-compose.yml

version: '3.1'
services:
  zoo1:
    image: zookeeper:3.6.3
    restart: always
    container_name: zoo1
    ports:
      - "2181:2181"
    environment:
      ZOO_MY_ID: 1
      ZOO_TICK_TIME: 20000
      ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181
    volumes:
      - ./zoo1/data:/data
      - ./zoo1/datalog:/datalog
  zoo2:
    image: zookeeper:3.6.3
    restart: always
    container_name: zoo2
    ports:
      - "2182:2181"
    environment:
      ZOO_MY_ID: 2
      ZOO_TICK_TIME: 20000
      ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181
    volumes:
      - ./zoo2/data:/data
      - ./zoo2/datalog:/datalog

  zoo3:
    image: zookeeper:3.6.3
    restart: always
    container_name: zoo3
    ports:
      - "2183:2181"
    environment:
      ZOO_MY_ID: 3
      ZOO_TICK_TIME: 20000
      ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181
    volumes:
      - ./zoo3/data:/data
      - ./zoo3/datalog:/datalog

3、启动

docker-compose up

4、测试

安装zookeeper客户端

 docker run -it --rm \
   --link zoo1:zk1  \
   --link zoo2:zk2 \
   --link zoo3:zk3  \
   --net zookeeper_default \
    zookeeper:3.6.3 zkCli.sh -server zk1:2181,zk2:2181,zk3:2181

成功连接
docker-compose一键搭建zookeeper集群_第2张图片

你可能感兴趣的:(docker,docker,zookeeper,容器)