1. Download the image

Select the version you want to download here hub.docker.com/_/zookeeper

Docker pull zookeeper: 3.7.0Copy the code

2. Create a mount directory

Create a data directory on node1

mkdir -p /usr/local/docker/zookeeper/node1/data 
mkdir -p /usr/local/docker/zookeeper/node1/logs 
mkdir -p /usr/local/docker/zookeeper/node1/conf
Copy the code

Create the zoo. CFG configuration file for node1

vim /usr/local/docker/zookeeper/node1/conf/zoo.cfg
Copy the code
# Heartbeat time 2 seconds
tickTime=2000
Maximum number of heartbeats that can be tolerated during the initial connection between the server and the Leader server 10*tickTime
initLimit=10
# Maximum response time between the Leader and followers in a cluster. Unit: 5*tickTime
syncLimit=5
Location to store in-memory database snapshots, which point to transaction logs for database updates unless otherwise specified
dataDir=/data
dataLogDir=/logs
# zookeeper port
clientPort=2181
1 indicates the number of a server. The first port is the port elected by the Leader, and the second port is the communication interface between the Zookeeper servers
server.1=zookeeper_1:2881:3881
server.2=zookeeper_2:2882:3882
server.3=zookeeper_3:2883:3883
Copy the code

Write the id of the node to 1

cat >> /usr/local/docker/zookeeper/node1/data/myid << eof
1
eof
Copy the code

Create the node2 node data directory

mkdir -p /usr/local/docker/zookeeper/node2/data 
mkdir -p /usr/local/docker/zookeeper/node2/logs 
mkdir -p /usr/local/docker/zookeeper/node2/conf
Copy the code

Create the zoo. CFG configuration file for node2

vim /usr/local/docker/zookeeper/node2/conf/zoo.cfg
Copy the code
# Heartbeat time 2 seconds
tickTime=2000
Maximum number of heartbeats that can be tolerated during the initial connection between the server and the Leader server 10*tickTime
initLimit=10
# Maximum response time between the Leader and followers in a cluster. Unit: 5*tickTime
syncLimit=5
Location to store in-memory database snapshots, which point to transaction logs for database updates unless otherwise specified
dataDir=/data
dataLogDir=/logs
# zookeeper port
clientPort=2181
1 indicates the number of a server. The first port is the port elected by the Leader, and the second port is the communication interface between the Zookeeper servers
server.1=zookeeper_1:2881:3881
server.2=zookeeper_2:2882:3882
server.3=zookeeper_3:2883:3883
Copy the code

Write the id of the node to 2

cat >> /usr/local/docker/zookeeper/node2/data/myid << eof
2
eof
Copy the code

Create a data directory for node3

mkdir -p /usr/local/docker/zookeeper/node3/data 
mkdir -p /usr/local/docker/zookeeper/node3/logs 
mkdir -p /usr/local/docker/zookeeper/node3/conf
Copy the code

Create the zoo. CFG configuration file for node3

vim /usr/local/docker/zookeeper/node3/conf/zoo.cfg
Copy the code
# Heartbeat time 2 seconds
tickTime=2000
Maximum number of heartbeats that can be tolerated during the initial connection between the server and the Leader server 10*tickTime
initLimit=10
# Maximum response time between the Leader and followers in a cluster. Unit: 5*tickTime
syncLimit=5
Location to store in-memory database snapshots, which point to transaction logs for database updates unless otherwise specified
dataDir=/data
dataLogDir=/logs
# zookeeper port
clientPort=2181
1 indicates the number of a server. The first port is the port elected by the Leader, and the second port is the communication interface between the Zookeeper servers
server.1=zookeeper_1:2881:3881
server.2=zookeeper_2:2882:3882
server.3=zookeeper_3:2883:3883
Copy the code

Write the id of the node to 3

cat >> /usr/local/docker/zookeeper/node3/data/myid << eof
3
eof
Copy the code

3. Run

docker network create zkcluster 
Copy the code

Run the zookeeper_1 node

docker run --net zkcluster \ --link zookeeper_1:zookeeper_1 \ --link zookeeper_2:zookeeper_2 \ --link zookeeper_3:zookeeper_3 \ -p 2181:2181 \ -p 2881:2888 \ -p 3881:3888 \ -v /usr/local/docker/zookeeper/node1/data:/data \  -v /usr/local/docker/zookeeper/node1/logs:/logs \ -v /usr/local/docker/zookeeper/node1/conf/zoo.cfg:/conf/zoo.cfg \ - the name zookeeper_1 -d zookeeper: 3.7.0Copy the code

Run the zookeeper_2 node

docker run --net zkcluster \ --link zookeeper_1:zookeeper_1 \ --link zookeeper_2:zookeeper_2 \ --link zookeeper_3:zookeeper_3 \ -p 2182:2181 \ -p 2882:2888 \ -p 3882:3888 \ -v /usr/local/docker/zookeeper/node2/data:/data \  -v /usr/local/docker/zookeeper/node2/logs:/logs \ -v /usr/local/docker/zookeeper/node2/conf/zoo.cfg:/conf/zoo.cfg \ - the name zookeeper_2 -d zookeeper: 3.7.0Copy the code

Run the zookeeper_3 node

docker run --net zkcluster \ --link zookeeper_1:zookeeper_1 \ --link zookeeper_2:zookeeper_2 \ --link zookeeper_3:zookeeper_3 \ -p 2183:2181 \ -p 2883:2888 \ -p 3883:3888 \ -v /usr/local/docker/zookeeper/node3/data:/data \  -v /usr/local/docker/zookeeper/node3/logs:/logs \ -v /usr/local/docker/zookeeper/node3/conf/zoo.cfg:/conf/zoo.cfg \ - the name zookeeper_3 -d zookeeper: 3.7.0Copy the code