How To Install Apache ZooKeeper on Ubuntu

August 11, 2020

Introduction

ZooKeeper is an Apache Software Foundation project designed to simplify monitoring and managing group services. It uses a simple interface for its centralized coordination service that manages configuration, information, naming, distributed synchronization, and provisioning.

In this tutorial, learn how to install and set up Apache ZooKeeper on Ubuntu 18.04 or 20.04. 

Install Apache ZooKeeper on Ubuntu.

Prerequisites

  • A Linux system running Ubuntu 20.04 or 18.04
  • Access to a terminal window/command line (Search Terminal)
  • A user account with sudo or root privileges

Installing Apache ZooKeeper on Ubuntu

Step 1: Installing Java

ZooKeeper is written in Java and requires this programming language to work. To check whether Java is already installed, run the command:

java --version

If the output displays a running Java version, you can move on to the next step. If the system says there is no such file or directory, you need to install Java before moving on to ZooKeeper.

There are different open-source Java packages available for Ubuntu. Find which one is best for you and its installation guide in How to Install Java on Ubuntu. The instructions apply to Ubuntu 18.04 and Ubuntu 20.04.

Step 2: Creating a User for ZooKeeper

1. Create a separate user for the ZooKeeper service by typing:

useradd zookeeper -m

The -m flag creates a home directory for the user. In this case, it will be /home/zookeeper. To name the user differently, replace zookeeper with the name of your choice.

2. Next, set bash as the default shell for the new user with the command:

usermod --shell /bin/bash zookeeper

3. Set a password for the user:

passwd zookeeper

Type and retype a strong password for the ZooKeeper user.

Set a password for the ZooKeeper user.

4. Then, add the user to the sudoers group for it to have sudo privileges:

usermod -aG sudo zookeeper

5. Check to verify that the user is now a superuser by listing the accounts in the sudoers group:

sudo getent group sudo

The output should display the user you created.

List members of sudo users.

Step 3: Creating a ZooKeeper Data Directory

Before installing ZooKeeper, create a directory structure where it can store configuration and state data (on a local disk or remote storage).

To store the data on the local machine, first create a new ZooKeeper directory by running:

sudo mkdir -p /data/zookeeper

Then, give the ZooKeeper user ownership to that directory:

chown -R zookeeper:zookeeper /data/zookeeper

Step 4: Downloading and Installing ZooKeeper

1. Open a web browser of your choice and navigate to the Apache ZooKeeper Download Page. The latest stable release appears at the top of the page. Click on the version you want to install to open the Apache download mirrors.

Apache ZooKeeper download page.

2. Copy the HTTP address for the suggested mirror site.

Download Apache mirror site for ZooKeeper.

3. Go back to the command line and move to the /opt directory:

cd /opt

4. Use the wget command to download the .tar file. Paste the link copied from the official Apache web page:

sudo wget https://downloads.apache.org/zookeeper/zookeeper-3.6.1/apache-zookeeper-3.6.1-bin.tar.gz

5. Extract the file by running:

sudo tar -xvf apache-zookeeper-3.6.1-bin.tar.gz

Note: When extracting the file, the name of the ZooKeeper binary package will vary. Make sure it matches the file you downloaded.

6. Rename the extracted file to zookeeper with the command:

mv apache-zookeeper-3.6.1-bin zookeeper

7. Give the zookeeper user ownership of that file by running:

chown -R zookeeper:zookeeper /opt/zookeeper

Step 5: Configuring ZooKeeper in Standalone Mode

The next step is creating a configuration file for ZooKeeper. The configuration below sets up ZooKeeper in standalone mode (used for developing and testing). For production environments, you need to run ZooKeeper in replication mode.

To configure ZooKeeper in standalone mode, create a new zoo.cfg file in the zookeeper directory:

sudo nano /opt/zookeeper/conf/zoo.cfg

Add the following lines:

tickTime = 2000

dataDir = /data/zookeeper

clientPort = 2181

initLimit = 5

syncLimit = 2

Save and exit the text editor.

Bear in mind this is a basic configuration setting for a single node cluster. The file can differ according to your needs. The lines above consist of the following:

  • tickTime: The number of milliseconds of each tick.
  • dataDir: The directory where snapshots of the in-memory database and transaction log for updates are stored.
  • clientPort: The port listening for client connections.
  • initLimit: The number of ticks that the initial synchronization phase can take.
  • syncLimit: The number of ticks that can pass between sending a request and getting an acknowledgement.

Step 6: Starting the ZooKeeper Service

Now, you can start the ZooKeeper service. Run the following command inside the /opt directory. If you exited out of the directory, navigate in it with cd /opt.

To start the ZooKeeper service use the command:

sudo bin/zkServer.sh start

The output should display that ZooKeeper has STARTED.

Start the ZooKeeper service.

Step 7: Connecting to the ZooKeeper Server

Once you have started the service, you can connect to the ZooKeeper server. In this guide, it is the local server.
Connect to ZooKeeper with the command:

bin/zkCli.sh -server 127.0.0.1:2181

Wait for the output to open a new zk prompt, which should confirm you are CONNECTED.

Connecting to the ZooKeeper server.

To see a list of available commands, run the following command in the new command prompt:

help
List ZooKeeper commands.

To close the session, type:

quit

To stop the ZooKeeper service, run the command:

bin/zkServer.sh stop

Step 8: Creating a System Service File

Finally, you need to create a service file to manage ZooKeeper.

1. Create a new zookeeper.service file in a text editor of your choice:

sudo nano /etc/systemd/system/zookeeper.service

2. Paste the following content into the file:

[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

3. Save and exit the file.

4. Reload the systemd service by running:

systemctl daemon-reload

5. Then, start the ZooKeeper service and enable it to start on boot:

systemctl start zookeeper
systemctl enable zookeeper

6. Verify the service is up and running with the command:

systemctl status zookeeper

You should see ZooKeeper is active (running).

Configuring Replicated ZooKeeper

A production environment requires setting up a replicated cluster of ZooKeeper nodes.

Once you have installed Java, created a ZooKeeper user, and downloaded the binary package, you need to create a configuration file. Unlike the basic configuration for standalone mode, replicated mode includes a few more lines.

All the servers in the same application need to have copies of the configuration file.

Create each file with the command:

sudo nano /opt/zookeeper/conf/zoo.cfg

Then, paste the following content into the file:

tickTime=2000
dataDir=/var/zookeeper
clientPort=2181
initLimit=5
syncLimit=2
server.1=[server_ip]:2888:3888
server.2=[server_ip]:2888:3888
server.3=[server_ip]:2888:3888

Save and exit the file. Repeat the process on each server in the group.

Conclusion

With this, you have successfully installed Apache ZooKeeper on Ubuntu! As mentioned above, this installation works on Ubuntu 18.04 and on the latest 20.04 release.

Was this article helpful?
YesNo
Sofija Simic
Sofija Simic is an experienced Technical Writer. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
Next you should read
How to Install Apache Tomcat 9 on Ubuntu 18.04
July 2, 2019

Apache Tomcat is a free, open-source, lightweight application server used for Java-based web applications.
Read more
How to Set Up Apache Virtual Hosts on Ubuntu 18.04
September 26, 2019

Name-based virtual hosts allow you to have a number of domains with the same IP address. This article...
Read more
Apache Hadoop Architecture Explained (with Diagrams)
May 25, 2020

Apache Hadoop is the go-to framework for storing and processing big data. This article provides clear-cut...
Read more
How to Install Apache Hive on Ubuntu
June 23, 2020

This tutorial shows you how to install, configure, and perform basic commands in Apache Hive. Improve your...
Read more