Introduction
Apache ZooKeeper is an open source distributed coordination service for distributed applications. It provides a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services. In this tutorial, we will show you how to install Apache ZooKeeper on an Ubuntu 18.04 server. We will also cover how to configure ZooKeeper and how to manage it using the command line. By the end of this tutorial, you will have a fully functional ZooKeeper installation.
How To Install Apache ZooKeeper on Ubuntu
1. Download the latest version of Apache ZooKeeper from the Apache website.
2. Extract the downloaded file and move it to the desired location.
3. Create a configuration file for ZooKeeper.
4. Create a data directory for ZooKeeper.
5. Create a log directory for ZooKeeper.
6. Create a user and group for ZooKeeper.
7. Set the environment variables for ZooKeeper.
8. Start the ZooKeeper server.
9. Test the ZooKeeper server.
10. Configure the ZooKeeper server.
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.
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.
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.
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.
2. Copy the HTTP address for the suggested mirror site.
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.
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.
To see a list of available commands, run the following command in the new command prompt:
help
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.
How To Install Apache ZooKeeper on Ubuntu
Apache ZooKeeper is an open-source server that enables highly reliable distributed coordination. It is used to manage and coordinate distributed applications and services. In this tutorial, we will show you how to install Apache ZooKeeper on an Ubuntu 18.04 server.
Prerequisites
Before you begin with this guide, you should have a non-root user with sudo privileges configured on your server. You can learn how to set up a user with these privileges by following our Ubuntu 18.04 initial server setup guide.
Step 1 — Installing Java
Apache ZooKeeper requires Java to be installed on the server. We can install the default OpenJDK package with the following command:
sudo apt-get update
sudo apt-get install default-jdk
Once the installation is complete, you can check the version of Java with the following command:
java -version
You should see the following output:
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-8u191-b12-2ubuntu0.18.04.1-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)
Step 2 — Downloading Apache ZooKeeper
At the time of writing this tutorial, the latest version of Apache ZooKeeper is 3.4.14. You can check for the latest version on the Apache ZooKeeper download page. You can download the latest version with the following command:
wget http://www-us.apache.org/dist/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz
Once the download is complete, extract the downloaded file with the following command:
tar -xzf zookeeper-3.4.14.tar.gz
Next, move the extracted directory to the /opt
directory:
sudo mv zookeeper-3.4.14 /opt/zookeeper
Step 3 — Configuring Apache ZooKeeper
Next, you will need to configure Apache ZooKeeper. You can do this by editing the conf/zoo.cfg
file:
sudo nano /opt/zookeeper/conf/zoo.cfg
Add the following lines at the end of the file:
tickTime=2000
dataDir=/var/lib/zookeeper
clientPort=2181
Save and close the file when you are finished.
Step 4 — Creating the Data Directory
Next, you will need to create the data directory for Apache ZooKeeper. You can do this by running the following command:
sudo mkdir -p /var/lib/zookeeper
Step 5 — Starting Apache ZooKeeper
Now, you can start Apache ZooKeeper with the following command:
sudo /opt/zookeeper/bin/zkServer.sh start
You should see the following output:
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
You can also check the status of Apache ZooKeeper with the following command:
sudo /opt/zookeeper/bin/zkServer.sh status
You should see the following output:
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/bin/../conf/zoo.cfg
Mode: standalone
Step 6 — Testing Apache ZooKeeper
At this point, Apache ZooKeeper is installed and running on your server. You can test it with the following command:
echo ruok | nc localhost 2181
You should see the following output:
imok
This indicates that Apache ZooKeeper is running properly.
Conclusion
In this tutorial, you learned how to install Apache ZooKeeper on an Ubuntu 18.04 server. You can now use Apache ZooKeeper to manage and coordinate distributed applications and services.