Using Docker to build ZooKeeper cluster in CentOS 7

Keywords: Programming Zookeeper Docker CentOS

1. First, Docker should be installed in CentOS 7, Docker installation reference

2. Pull the docker image of ZooKeeper and go to https://hub.docker.com/ Search relevant zoomkeeper images on

docker pull zookeeper:3.4.14

3. Create three folders corresponding to three containers on the host computer

/root/zookeeper01/conf
/root/zookeeper01/data
/root/zookeeper01/datalog

/root/zookeeper02/conf
/root/zookeeper02/data
/root/zookeeper02/datalog

/root/zookeeper03/conf
/root/zookeeper03/data
/root/zookeeper03/datalog

4. Create and run three ZooKeeper containers

docker run -d --name zookeeper-1 \
-v /root/zookeeper01/conf:/conf \         #Map folder to corresponding folder of host
-v /root/zookeeper01/data:/data \         #Map folder to corresponding folder of host
-v /root/zookeeper01/datalog:/datalog \   #Map folder to corresponding folder of host
zookeeper:3.4.14

docker run -d --name zookeeper-2 \
-v /root/zookeeper02/conf:/conf \
-v /root/zookeeper02/data:/data \
-v /root/zookeeper02/datalog:/datalog \
zookeeper:3.4.14

docker run -d --name zookeeper-3 \
-v /root/zookeeper03/conf:/conf \
-v /root/zookeeper03/data:/data \
-v /root/zookeeper03/datalog:/datalog \
zookeeper:3.4.14

5. Modify the corresponding zoo.cfg file and myid file

First, obtain the ip addresses of three containers respectively

# Enter the command line of the container
docker exec -it zookeeper-1 /bin/bash
# View the ip address of the container
cat /etc/hosts

Modify zoo.cfg on the host machine, and add it at the end of each zoo.cfg file

# These three IPS are the ip of three containers. 2888 is the data communication port in the ZooKeeper cluster, and 3888 is the election port in the cluster
server.1=172.17.0.2:2888:3888
server.2=172.17.0.3:2888:3888
server.3=172.17.0.4:2888:3888

Modify three myid files respectively, the contents are 1, 2, 3

6. Restart three containers or stop and then start

# Restart container
docker restart zookeeper-1 zookeeper-2 zookeeper-3
# Stop container
docker stop zookeeper-1 zookeeper-2 zookeeper-3
# Starting container
docker start zookeeper-1 zookeeper-2 zookeeper-3

7. Verify success

# Enter the command line of a container
docker exec -it zookeeper-1 /bin/bash

# Under zookeeper/bin, run the zookeeper client
./zkCli.sh

# Create a node
create /test test

# Then go to other containers to see if there is a test node just created
get /test
 

Posted by mcollyns on Fri, 20 Mar 2020 07:59:17 -0700