Apache ZooKeeper is a centralized service for maintaining configuration information, naming, providing synchronization, and providing group services. It enables highly reliable distributed coordination.
In this tutorial, we are going to learn how to install Zookeeper in Ubuntu 20.04, see the alternatives to ZooKeeper.
There are some other products you can use if ZooKeeper isn’t your favorite flavor. These are:
- Hashicorp Consul
Errors to avoid if you configure ZooKeeper correctly.
If you want to avoid errors from occurring, you need to configure ZooKeeper properly. Here are the errors you can easily avoid:
- Inconsistent list of servers. Zookeeper clients servers must match Zookeeper servers that each ZooKeeper server has.
- Inconsistent placement of transaction logs. ZooKeeper sync transactions to media before it returns a response. A dedicated transaction log device is key to consistent good performance.
- Incorrect java heap size. To avoid this, set java heap size correctly.
- Publicly accessible deployment. Deploy a zooKeeper behind a firewall
- Have Java release 1.8 and above JDK 12
- Use 3 recommended ZooKeeper servers for ensemble
- Atleast 2GB RAM
Installing Apache ZooKeeper on Ubuntu 20.04
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)
For my case I already have java installed but for a fresh installation of java in your system, run the following command to make the installed. Check this article on how to install java on Ubuntu 20.04.
$ sudo apt install openjdk-8-jdk openjdk-8-jre
3. Create user for Apache ZooKeeper
We need to create a user that will run all the ZooKeeper services.
$ sudo useradd nextgen -m
-m will create a dedicated home directory for the zookeeper user.
Next, we need to set bash as the default shell for the zookeeper user.
$ sudo usermod --shell /bin/bash nextgen
set a password for this user
$ sudo passwd nextgen
Next, add a user to sudoers group
$ sudo usermod -aG sudo nextgen
You can check the created user if it exists.
$ sudo getent group sudo sudo:x:27:nextgen
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 nextgen:nextgen /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.0/apache-zookeeper-3.7.0.tar.gz
Next, we need to extract the binaries from the above download.
$ sudo tar -xvf apache-zookeeper-3.7.0.tar.gz
Next, we need to give permission to extracted binaries.
$ sudo chown nextgen:nextgen -R apache-zookeeper-3.7.0
The next thing is to create a symbolic link so that ZooKeeper can become accessible from all directories in your system.
$ sudo ln -s apache-zookeeper-3.7.0 zookeeper
we now need to change the ownership of that link to nextgen:nextgen
$ sudo chown -h nextgen:nextgen zookeeper
-h specifies you have changed the ownership of the link.
6. Configuring Apache ZooKeeper
Because we are not in a production environment, we are going to configure ZooKeeper in a standalone environment. When you are in a production environment configure replication mode. To configure ZooKeeper in standalone create a zoo.cfg in the Zookeeper directory.
$ sudo vi /opt/zookeeper/conf/zoo.cfg
We need to 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 detail
- 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
/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
We have learned how to install Apache ZooKeeper on Ubuntu 20.04. I am happy that you have enjoyed the session.