Use the official Docker image to build a ZooKeeper cluster
refer to:
https://segmentfault.com/a/1190000006907443
Use the official image of zookeeper on hub.docker.com
docker pull zookeeper
docker run --rm=true --name my_zookeeper zookeeper:latest
Officially used openJDK
java.home=/usr/lib/jvm/java-1.8-openjdk/jre
Use docker-compose to start the ZK cluster
docker-compose.yml
version: '2'
services:
zoo1:
image: zookeeper
restart: always
container_name: zoo1
ports:
- "2181:2181"
environment:
ZOO_MY_ID: 1
ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
zoo2:
image: zookeeper
restart: always
container_name: zoo2
ports:
- "2182:2181"
environment:
ZOO_MY_ID: 2
ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
zoo3:
image: zookeeper
restart: always
container_name: zoo3
ports:
- "2183:2181"
environment:
ZOO_MY_ID: 3
ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
start cluster -d to run in background mode
COMPOSE_PROJECT_NAME=zk_test docker-compose up -d
COMPOSE_PROJECT_NAME=zk_test Give the compose project a name to avoid confusion with other composes.
Connect to the ZK cluster using the Docker command line client
Through the docker-compose ps command, we know that the three host names of the started ZK cluster are zoo1, zoo2, zoo3, so we can link them separately:
docker run -it --rm \
--link zoo1:zk1 \
--link zoo2:zk2 \
--link zoo3:zk3 \
--net zktest_default \
zookeeper zkCli.sh -server zk1:2181,zk2:2181,zk3:2181
Check the cluster status:
echo stat | nc localhost 2181
echo stat | nc localhost 2182
echo stat | nc localhost 2183