Installing Apache ZooKeeper on Ubuntu
1. Updating system repositories
We need to make our system repositories up to date in order to avoid running into different errors later during installation.
sudo apt update && apt upgrade -y
2. Install Java runtime
Apache ZooKeeper requires java to run because it is written in java language. So to install java run the following command in your terminal. First, you can check the version if you already installed java.
java -version openjdk version "1.8.0_312" OpenJDK Runtime Environment (build 1.8.0_312-8u312-b07-0ubuntu1~20.04-b07) OpenJDK 64-Bit Server VM (build 25.312-b07, mixed mode)
If you dont have java, run the command below.
sudo apt install openjdk-8-jdk openjdk-8-jre
3. Create user for Apache ZooKeeper
Create a user that will run all the ZooKeeper services.
sudo useradd zookeeper -m
Set bash as the default shell for the zookeeper user.
sudo usermod --shell /bin/bash zookeeper
set a password for this user
sudo passwd zookeeper
Add a user to sudoers group
sudo usermod -aG sudo zookeeper
Check the created user if it exists.
sudo getent group sudo sudo:x:27:zookeeper
4. Create a data directory for Apache ZooKeeper
ZooKeeper requires a place to read and write data to, this is the reason we need to create one for ZooKeeper.
sudo mkdir -p /data/zookeeper
To ensure that the created user can write into the directory, we need to give them permission.
sudo chown zookeeper:zookeeper /data/zookeeper
5. Install Apache Zookeeper on Ubuntu 20.04
Now we can download Zookeeper from the Apache download page. But first, move to /opt/ directory where you will extract the apache ZooKeeper to.
cd /opt
Use wget to get the download.
wget https://dlcdn.apache.org/zookeeper/zookeeper-3.7.1/apache-zookeeper-3.7.1-bin.tar.gz
Extract the binaries from the above download.
sudo tar -xvf apache-zookeeper-3.7.1-bin.tar.gz
Give permission to extracted binaries.
sudo chown zookeeper:zookeeper -R apache-zookeeper-3.7.1-bin
sudo ln -s
apache-zookeeper-3.7.1-binzookeeper
sudo chown -h zookeeper:zookeeper zookeeper
6. Configuring Apache ZooKeeper
To configure ZooKeeper in standalone create a zoo.cfg in the Zookeeper directory.
sudo vi /opt/zookeeper/conf/zoo.cfg
Add the following lines in the zoo.cfg file
tickTime = 2000 dataDir = /data/zookeeper clientPort = 2181 initLimit = 5 syncLimit = 2
Let’s look at the following in details
- tickTime: Sets the length of a tick in milliseconds.
- dataDir: Specifies the directory used to store snapshots of the in-memory database and the transaction log for updates.
- clientPort: The port used to listen 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.
7. Start Zookeeper service
To start the ZooKeeper service run the following command. Run this command inside /opt/zookeeper directory
sudo bin/zkServer.sh start
output
/usr/bin/java ZooKeeper JMX enabled by default Using config: /opt/apache-zookeeper-3.7.0/bin/../conf/zoo.cfg Starting zookeeper ... STARTED
8. Connecting to ZooKeeper server
To connect to the local ZooKeeper server run the following command
bin/zkCli.sh -server 127.0.0.1:2181