Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Install Apache ZooKeeper on Ubuntu

Install Java, a service user and ZooKeeper, and run it as a system service.

VPS and Cloud Servers2 min read16 screenshots

What ZooKeeper is

Apache ZooKeeper is a service that keeps configuration and naming data, and helps distributed systems such as Kafka and Hadoop to coordinate.

Step 1: Install Java

Check if Java is installed:

java --version
Checking the Java version

If not, install it:

sudo apt update
sudo apt install default-jdk
Installing the JDK

Step 2: Create a user for ZooKeeper

sudo adduser --system --group --home /opt/zookeeper zookeeper
Creating the zookeeper user

Step 3: Create the data folder

sudo mkdir -p /var/lib/zookeeper
sudo chown zookeeper:zookeeper /var/lib/zookeeper
Creating the data folder

Step 4: Download and install ZooKeeper

cd /opt
Moving to /opt

Open the Apache ZooKeeper download page and copy the link of the latest stable binary release.

The ZooKeeper download page
The download link

Download it (replace the version with the current one):

sudo wget https://dlcdn.apache.org/zookeeper/zookeeper-3.9.2/apache-zookeeper-3.9.2-bin.tar.gz
Downloading ZooKeeper

Extract it:

sudo tar -xzf apache-zookeeper-*.tar.gz -C /opt
Extracting the archive

Rename the folder:

sudo mv /opt/apache-zookeeper-*-bin /opt/zookeeper
Renaming the folder

Give the user the right to own it:

sudo chown -R zookeeper:zookeeper /opt/zookeeper
Setting the owner

Step 5: Configure ZooKeeper

Copy the sample file to zoo.cfg:

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

Use these settings:

tickTime=2000
dataDir=/var/lib/zookeeper
clientPort=2181
initLimit=10
syncLimit=5
The zoo.cfg file
  • tickTime. The basic time unit in milliseconds.
  • dataDir. Where ZooKeeper keeps its data.
  • clientPort. The port for client connections.
  • initLimit. The time that followers have to connect and sync to the leader.
  • syncLimit. The time allowed between a request and its acknowledgment.

Step 6: Create a system service

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

Add:

[Unit]
Description=Apache ZooKeeper Service
After=network.target

[Service]
Type=forking
User=zookeeper
Group=zookeeper
ExecStart=/opt/zookeeper/bin/zkServer.sh start
ExecStop=/opt/zookeeper/bin/zkServer.sh stop
Restart=on-failure

[Install]
WantedBy=multi-user.target
The service file

Reload systemd:

sudo systemctl daemon-reload
Reloading systemd

Step 7: Start ZooKeeper

sudo systemctl start zookeeper
sudo systemctl enable zookeeper
sudo systemctl status zookeeper
Starting ZooKeeper

Test with the client:

/opt/zookeeper/bin/zkCli.sh -server 127.0.0.1:2181
The ZooKeeper folder and client

Quick recap

  • Install Java, create a user, download and extract ZooKeeper.
  • Set zoo.cfg with dataDir and clientPort.
  • Run it as a systemd service.