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.
Join the DZone community and get the full member experience.Join For Free
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_1, zookeeper-3.4.12_2, zookeeper-3.4.12_3.
Go to the zookeeper-3.4.12_1 directory.
Create two folders with the names data and logs inside Zookeeper's home directory.
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.
Go to the conf folder and change the name of the zoo_sample.cfg file to zoo.cfg.
Change dataDir with the full path of the data folder that we created in the above step.
Set the clientPort to 2181.
Paste the below lines into the zoo.cfg file.
server.1=localhost:2891:3881 #1 is the id that we put in myid file. server.2=localhost:2892:3882 #2 is the id that we will put in myid file of second node. server.3=localhost:2893:3883 #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.
Opinions expressed by DZone contributors are their own.