How to Setup Zookeeper Cluster

DZone 's Guide to

How to Setup Zookeeper Cluster

In this article, a software engineer will discuss how to setup a Zookeeper cluster with 3 nodes, giving you the knowledge to create as many nodes as you need.

· Big Data Zone ·
Free Resource

In my previous article, I discussed how to set up Zookeeper with a single node. For better reliability and high availability of the Zookeeper service, we should set up Zookeeper in cluster mode. In this article, I will discuss how to setup a Zookeeper cluster with 3 nodes. Zookeeper clusters are also called Zookeeper ensembles. At the end of this article, you will be able to set up an ensemble with as many nodes as you desire. 

Download Zookeeper from here.

Extract the zip file. Make two extra copies of the extracted folder. Add the suffixes _1,_2,_3 to these folders' names. So if your extracted folder was zookeeper-3.4.12,now you will have three folders with the names zookeeper-3.4.12_1zookeeper-3.4.12_2zookeeper-3.4.12_3

  1. Go to the zookeeper-3.4.12_1 directory.  

  2. Create two folders with the names data and logs inside Zookeeper's home directory.

  3. Execute the command  echo "1" > data/myid. This is the server id in the ensemble. The id must be unique within the ensemble and should have a value between 1 and 255.

  4. Go to the conf folder and change the name of the zoo_sample.cfg file to zoo.cfg.

  5. Change dataDir with the full path of the data folder that we created in the above step.

  6. Set the clientPort to 2181.

  7. Paste the below lines into the zoo.cfg file.

#1 is the id that we put in myid file.
#2 is the id that we will put in myid file of second node.
#3 is the id that we will put in myid file of third node.

Zookeeper will use these ports (2891, etc.) to connect the individual follower nodes to the leader nodes. The other ports (3881, etc.) are used for leader election in the ensemble.

Our configuration is ready for the first node. For the other two folders or nodes, perform the same steps as mentioned above with the following changes.

  • In step 3, change the command to echo "2" > data/myid and echo "3" > data/myid respectively.

  • In step 6, change the port to 2182 and 2183, respectively.

Now our configuration is ready. Go to the home directory of each folder and the execute command:

 java -cp zookeeper-3.4.12.jar:lib/log4j-1.2.17.jar:lib/slf4j-log4j12-1.7.25.jar:lib/slf4j-api-1.7.25.jar:conf org.apache.zookeeper.server.quorum.QuorumPeerMain conf/zoo.cfg >> logs/zookeeper.log & 

Check the logs to see whether your cluster is ready or not.


We discussed how to set up an ensemble with 3 nodes. Now you can create an ensemble with as many nodes as you want by making a few changes.

apache kafka, big data, zookeeper

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}