/ Cloud

Setup a HA Zookeeper Cluster

Overview
In this blog I will cover quick way of setting up a HA Zookeeper cluster on Centos 7.3.

Cluster components

  • Zookeeper v3.4.10
  • OS - Centos v7.3x64

Prerequisites
To complete this article, you will need below infrastructure:

  • CentOS x86_64 7.3 for Zookeeper Cluster(Ubuntu can be also used)
  • 3x Zookeeper nodes -

Zookeeper nodes

zk-1.scaleon.io --- 172.16.10.32
zk-2.scaleon.io --- 172.16.10.11
zk-3.scaleon.io --- 172.16.10.23

Configure the Zookeeper cluster
Run below commands on all the 3 zookeeper nodes

yum install wget -y
yum install java-1.8.0-openjdk -y
wget http://apache.org/dist/zookeeper/current/zookeeper-3.4.10.tar.gz
tar xvzf zookeeper-3.4.10.tar.gz
useradd zookeeper
cp -r zookeeper-3.4.10/ /opt
chown -R zookeeper:zookeeper /opt/zookeeper-3.4.10/
ln -s /opt/zookeeper-3.4.10 /opt/zookeeper
chown -h zookeeper:zookeeper /opt/zookeeper
mkdir -p /var/lib/zookeeper
chown -R zookeeper:zookeeper /var/lib/zookeeper
cd /opt/zookeeper/conf
cp zoo_sample.cfg zoo.cfg
mkdir -p /var/lib/zookeeper/data
echo 1 > /var/lib/zookeeper/data/myid 
chown zookeeper:zookeeper /var/lib/zookeeper/data

cat > /etc/systemd/system/zookeeper.service <<EOF
[Unit]
Description=Apache Zookeeper server 
Documentation=http://zookeeper.apache.org
Requires=network.target remote-fs.target 
After=network.target remote-fs.target

[Service]
Type=forking
User=zookeeper
Group=zookeeper
ExecStart=/opt/zookeeper/bin/zkServer.sh start
ExecStop=/opt/zookeeper/bin/zkServer.sh stop
ExecReload=/opt/zookeeper/bin/zkServer.sh restart
WorkingDirectory=/var/lib/zookeeper

[Install]
WantedBy=multi-user.target
EOF

Update data directory and servers in zoo.cfg. Add below to /opt/zookeeper/conf/zoo.cfg

server.1=zk-1.scaleon.io:2888:3888
server.2=zk-2.scaleon.io:2888:3888
server.3=zk-3.scaleon.io:2888:3888
autopurge.snapRetainCount=10
autopurge.purgeInterval =12

Start the services on all 3 nodes
systemctl daemon-reload
systemctl start zookeeper.service
systemctl status zookeeper.service
systemctl enable zookeeper.service

Verification

We can see below zk-2 is leader and other 2 nodes are follower

[root@zk-1 ~]# /opt/zookeeper/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/bin/../conf/zoo.cfg
Mode: follower
[root@zk-1 ~]#

[root@zk-2 ~]# /opt/zookeeper/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/bin/../conf/zoo.cfg
Mode: leader
[root@zk-2 ~]#

[root@zk-3 ~]# /opt/zookeeper/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/bin/../conf/zoo.cfg
Mode: follower
[root@zk-3 ~]# 

Thanks.