Use Docker to build a ZooKeeper cluster

Source: Internet
Author: User
Tags min zookeeper docker run

Original link background

Originally learning ZK, I was built in the local pseudo-cluster, although said to use no problem, but always feel a little trouble to deploy. Just now I found that ZK already has the image of Docker, so I tried it, found that it was really cool, a few commands can build a complete ZK cluster. Let me take a quick look at some of the steps to build a ZK cluster using Docker. image Download

There are many ZK images on the hub.docker.com, but for the sake of stability, we will use the official ZK mirror. First execute the following command:

Docker Pull Zookeeper

When the following results appear, the image has already been downloaded:

Docker Pull Zookeeper
Using default Tag:latest
Latest:pulling from Library/zookeeper
E110a4a17941:pull Complete
A696cba1f6e8:pull Complete
Bc427bd93e95:pull Complete
C72391ae24f6:pull Complete
40ab409b6b34:pull Complete
D4bb8183b85d:pull Complete
0600755f1470:pull Complete
digest:sha256:12458234bb9f01336df718b7470cabaf5c357052cbcb91f8e80be07635994464
status:downloaded newer image for Zookeeper:latest ZK Mirror Basic use start ZK Mirror

Docker Run–name my_zookeeper-d Zookeeper:latest

This command runs a zookeeper container in the background, the name is My_zookeeper, and it exports port 2181 by default.
Then we use:

Docker Logs-f My_zookeeper

This command looks at the operation of ZK, and the output resembles the following, indicating that ZK has successfully started:

Docker Logs-f My_zookeeper
ZooKeeper JMX enabled by default
Using Config:/conf/zoo.cfg
...
2016-09-14 06:40:03,445 [myID:]-INFO [main:nioservercnxnfactory@89]-binding to port 0.0.0.0/0.0.0.0:2181 using ZK command line Client Connection ZK

Because the ZK container we just started is not bound to the host's port, so we can't access it directly. But we can access this ZK container through the Docker link mechanism. Execute the following command:

Docker Run-it–rm–link My_zookeeper:zookeeper Zookeeper Zkcli.sh-server Zookeeper

If you have any knowledge of Docker, then the above command must not be unfamiliar. This command means: Start a zookeeper image and run the zkcli.sh command in this image, the command parameter is "-server zookeeper" to the name we started earlier Connect the My_zookeeper container to our newly created container and name its hostname zookeeper

Once we have executed this command, we can operate the ZK service as normal as the ZK command line client. the construction of ZK cluster

Because it's too much trouble to start the ZK one by one, I use docker-compose directly to start the ZK cluster for the sake of convenience.
First create a file named Docker-compose.yml, with the following contents:

Version: ' 2 '
Services
Zoo1:
Image:zookeeper
Restart:always
Container_name:zoo1
Ports
-"2,181:2,181"
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
-"2,182:2,181"
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
-"2,183:2,181"
Environment:
Zoo_my_id:3
zoo_servers:server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888

This configuration file tells Docker to run three zookeeper mirrors and bind the local 2181, 2182, and 2183 ports to the corresponding container's 2181 ports, respectively.
zoo_my_id and zoo_servers are two environment variables that need to be set for the ZK cluster, where zoo_my_id represents the ZK service ID, which is an integer between 1-255 , must be unique within the cluster. zoo_servers is the list of hosts for the ZK cluster.

We then run in the docker-compose.yml current directory:

Compose_project_name=zk_test Docker-compose up

You can start the ZK cluster.
After successful execution of the above command, then run the Docker-compose PS command in the other terminal to view the launched ZK container:

Compose_project_name=zk_test Docker-compose PS
Name Command State Ports
Zoo1/docker-entrypoint.sh Zkse ... Up 0.0.0.0:2181->2181/tcp
Zoo2/docker-entrypoint.sh Zkse ... Up 0.0.0.0:2182->2181/tcp
Zoo3/docker-entrypoint.sh Zkse ... Up 0.0.0.0:2183->2181/tcp

Note that we have added compose_project_name=zk_test this environment variable before "docker-compose up" and "Docker-compose PS", which is a name for our compose project, so as not to be associated with other The compose confusion. Connect a ZK cluster using the Docker command line client

With the Docker-compose PS command, we know that the three host names of the launched ZK cluster are zoo1, Zoo2, Zoo3, respectively, so we can link them individually:

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

Connect to ZK cluster via local host

Because we mapped the zoo1, Zoo2, Zoo3 2181 ports to the 2181, 2182, 2183 ports on the local host, we connected the ZK cluster using the following command:

Zkcli.sh-server localhost:2181,localhost:2182,localhost:2183 View cluster

We can connect to the specified ZK server via the NC command and then send stat to view the state of the ZK service, for example:

echo Stat | NC 127.0.0.1 2181
Zookeeper version:3.4.9-1757313, built on 08/23/2016 06:50 GMT
Clients:
/172.18.0.1:498100
Latency min/avg/max:5/39/74
Received:4
Sent:3
Connections:1
outstanding:0
zxid:0x200000002
Mode:follower
Node Count:4

echo Stat | NC 127.0.0.1 2182
Zookeeper version:3.4.9-1757313, built on 08/23/2016 06:50 GMT
Clients:
/172.18.0.1:508700
Latency min/avg/max:0/0/0
Received:2
Sent:1
Connections:1
outstanding:0
zxid:0x200000002
Mode:follower
Node Count:4

echo Stat | NC 127.0.0.1 2183
Zookeeper version:3.4.9-1757313, built on 08/23/2016 06:50 GMT
Clients:
/172.18.0.1:518200
Latency min/avg/max:0/0/0
Received:2
Sent:1
Connections:1
outstanding:0
zxid:0x200000002
Mode:leader
Node Count:4

Through the above output, we can see that, zoo1, Zoo2 are follower, and Zoo3 is leader, so it proves that our ZK cluster is really building up.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.