HowtoForge

How to Install Apache ZooKeeper on Ubuntu 20.04

ZooKeeper is a software project by Apache Software Foundation that provides a high-performance shared data service used to build distributed applications. It is used by a cluster to maintain shared data with robust synchronization techniques. It provides a simple interface for managing configuration information, naming, distributed synchronization, and provisioning of group services.

In this tutorial, we will show you how to install Apache ZooKeeper on an Ubuntu 20.04 server.

Prerequisites

Install Java

Apache ZooKeeper is written in Java so you will need to install Java in your system. You can install it with the following command:

apt-get install default-jdk -y

After installing Java, verify the installed version of Java with the following command:

java --version

You should get the following output:

openjdk 11.0.8 2020-07-14
OpenJDK Runtime Environment (build 11.0.8+10-post-Ubuntu-0ubuntu120.04)
OpenJDK 64-Bit Server VM (build 11.0.8+10-post-Ubuntu-0ubuntu120.04, mixed mode, sharing)

Install Zookeeper

Before installing Apache zookeeper, you will need to create a new user for zookeeper. You can create it with the following command:

useradd zookeeper -m

Next, set password for this user and add it to the sudo group with the following command:

usermod --shell /bin/bash zookeeper
passwd zookeeper
usermod -aG sudo zookeeper

Next, create a data directory for zookeeper and change the ownership of this directory:

mkdir /zookeeper
chown -R zookeeper:zookeeper /zookeeper

Next, you will need to download the latest version of the zookeeper to the /opt directory. You can download it with the following command:

cd /opt
wget https://mirrors.estointernet.in/apache/zookeeper/zookeeper-3.6.2/apache-zookeeper-3.6.2-bin.tar.gz

Once the download is completed, extract the downloaded file with the following command:

tar -xvzf apache-zookeeper-3.6.2-bin.tar.gz

Next, rename the extracted directory to zookeeper with the following command:

mv apache-zookeeper-3.6.2-bin zookeeper

Next, change the ownership of the zookeeper directory with the following command:

chown -R zookeeper:zookeeper /opt/zookeeper

Configure ZooKeeper in Standalone Mode

Next, you will need to create a ZooKeeper configuration file to sets up ZooKeeper in standalone mode. You can create it with the following command:

nano /opt/zookeeper/conf/zoo.cfg

Add the following lines:

tickTime=2500
dataDir=/zookeeper
clientPort=2181
maxClientCnxns=80

Save and close the file when you are finished. Then, start the ZooKeeper service with the following command:

cd /opt/zookeeper
bin/zkServer.sh start

You should get the following output:

ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

By default, ZooKeeper is listening on port 2181. You can check it with the following command:

ss -ntpl | grep 2181

You should get the following output:

LISTEN    0         50                       *:2181                   *:*        users:(("java",pid=12749,fd=53)) 

You can now connect to the ZooKeeper local server with the following command:

bin/zkCli.sh -server 127.0.0.1:2181

Once connected, you should get the following output:

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: 127.0.0.1:2181(CONNECTED) 0] 

Now, exit from the session with the following command:

quit

Next, stop the ZooKeeper service with the following command:

bin/zkServer.sh stop

You should see the following output:

ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED

Create a Systemd Service File for ZooKeeper

Next, you will need to create a systemd service file to manage the ZooKeeper service. You can create it with the following command:

nano /etc/systemd/system/zookeeper.service

Add the following lines:

[Unit]
Description=Zookeeper Daemon
Documentation=http://zookeeper.apache.org
Requires=network.target
After=network.target

[Service]    
Type=forking
WorkingDirectory=/opt/zookeeper
User=zookeeper
Group=zookeeper
ExecStart=/opt/zookeeper/bin/zkServer.sh start /opt/zookeeper/conf/zoo.cfg
ExecStop=/opt/zookeeper/bin/zkServer.sh stop /opt/zookeeper/conf/zoo.cfg
ExecReload=/opt/zookeeper/bin/zkServer.sh restart /opt/zookeeper/conf/zoo.cfg
TimeoutSec=30
Restart=on-failure

[Install]
WantedBy=default.target

Save and close the file then reload the systemd daemon to apply the configuration changes:

systemctl daemon-reload

Next, change the ownership of the ZooKeeper and data directory with the following command:

chown -R zookeeper:zookeeper /opt/zookeeper
chown -R zookeeper:zookeeper /zookeeper

Next, start the ZooKeeper service and enable it to start at system reboot with the following command:

systemctl start zookeeper
systemctl enable zookeeper

You can now check the status of the ZooKeeper service with the following command:

systemctl status zookeeper

You should get the following output:

? zookeeper.service - Zookeeper Daemon
     Loaded: loaded (/etc/systemd/system/zookeeper.service; disabled; vendor preset: enabled)
     Active: active (running) since Sun 2020-09-27 06:43:28 UTC; 8s ago
       Docs: http://zookeeper.apache.org
    Process: 13915 ExecStart=/opt/zookeeper/bin/zkServer.sh start /opt/zookeeper/conf/zoo.cfg (code=exited, status=0/SUCCESS)
   Main PID: 13946 (java)
      Tasks: 37 (limit: 4691)
     Memory: 50.6M
     CGroup: /system.slice/zookeeper.service
             ??13946 java -Dzookeeper.log.dir=/opt/zookeeper/bin/../logs -Dzookeeper.log.file=zookeeper-zookeeper-server-ubuntu2004.log -Dzook>

Sep 27 06:43:27 ubuntu2004 systemd[1]: Starting Zookeeper Daemon...
Sep 27 06:43:27 ubuntu2004 zkServer.sh[13915]: /usr/bin/java
Sep 27 06:43:27 ubuntu2004 zkServer.sh[13915]: ZooKeeper JMX enabled by default
Sep 27 06:43:27 ubuntu2004 zkServer.sh[13915]: Using config: /opt/zookeeper/conf/zoo.cfg
Sep 27 06:43:28 ubuntu2004 zkServer.sh[13915]: Starting zookeeper ... STARTED
Sep 27 06:43:28 ubuntu2004 systemd[1]: Started Zookeeper Daemon.

Conclusion

Congratulations! you have successfully installed and configured ZooKeeper on Ubuntu 20.04 server. You can now easily build and deploy the distributed application with ZooKeeper.

How to Install Apache ZooKeeper on Ubuntu 20.04