There is a new version of this tutorial available for Ubuntu 20.04 (Focal Fossa).

How to Setup Apache ZooKeeper Cluster on Ubuntu 18.04 LTS

Zookeeper is a centralized service for managing configuration information, naming, distributed synchronization, and provisioning of group services that is published as open source software. It is written in a Java language that allows you to focus on the core application logic without worrying about the distributed nature of the application. ZooKeeper's architecture supports high availability through redundant services. Zookeeper enables distributed processes to coordinate with each other via a common hierarchical namespace of data registers. Zookeeper also offers its customers high throughput, high availability, low latency and strictly controlled access to the Znodes. Zookeeper is specifically designed to store status information, configurations, location information, and more. ZooKeeper offers many features like reliable system, simple architecture, fast processing, scalability and much more.

In this tutorial, we will learn how to set up a single node Apache ZooKeeper Cluster on Ubuntu 18.04.

Requirements

  • A server running Ubuntu 18.04.
  • A root password is setup on your server.

Getting Started

First, you will need to update your system with the latest version. You can do this by running the following command:

apt-get update -y
apt-get upgrade -y

Once the system is updated, restart your system to apply all the changes.

Install Java

Zookeeper is written in Java. So, you will need to install Java to your system. By default, the latest version of Java is not available in the Ubuntu 18.04 default repository. So, add the Java repository with the following command:

add-apt-repository ppa:linuxuprising/java

Next, update the repository and install Java with the following command:

apt-get update -y
apt-get install oracle-java11-set-default

Once the Java has been installed, you can check the Java version with the following command:

java --version

You should see the following output:

java 11.0.2 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.2+7-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.2+7-LTS, mixed mode)

Once you have finished, you can proceed to the next step.

Create a ZooKeeper User

Next, you will need to create a zookeeper user to run zookeeper service.

First, create a zookeeper user with the following command:

useradd zookeeper -m
usermod --shell /bin/bash zookeeper

Next, set a password with the following command:

passwd zookeeper

Next, add the zookeeper user to the sudo group so it can run commands in a privileged mode:

usermod -aG sudo zookeeper

After creating user, you can procced to install ZooKeeper.

Install ZooKeeper

ZooKeeper stores all configuration and state data to disk. So, you will need to create a directory structure for ZooKeeper with the following command:

mkdir -p /data/zookeeper

Next, give proper ownership to the /data directory with the following command:

chown -R zookeeper:zookeeper /data/zookeeper

Next, change the directory to the /opt and download the latest version of ZooKeeper with the following command:

cd /opt
wget https://archive.apache.org/dist/zookeeper/zookeeper-3.4.9/zookeeper-3.4.9.tar.gz

Next, extract the downloaded file with the following command:

tar -xvzf zookeeper-3.4.9.tar.gz

Next, rename the extracted binary with the following command:

mv zookeeper-3.4.9 zookeeper

Next, give ownership to the ZooKeeper user with the following command:

chown -R zookeeper:zookeeper /opt/zookeeper

Configure ZooKeeper

Next, you will need to create a configuration file for ZooKeeper. You can do it with the following command:

nano /opt/zookeeper/conf/zoo.cfg

Add the following lines:

tickTime=2500
dataDir=/data/zookeeper
clientPort=2181
maxClientCnxns=80

Save and close the file, when you are finished.

Next, start the ZooKeeper service with the following command:

cd /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 connect to the local ZooKeeper server with the following command:

bin/zkCli.sh -server 127.0.0.1:2181

Once you have connected successfully, you should see the following output:

[zk: 127.0.0.1:2181(CONNECTED) 1] 

Now, type help on the prompt, you should see the a list of commands that you can execute from the client.

help
ZooKeeper -server host:port cmd args
	stat path [watch]
	set path data [version]
	ls path [watch]
	delquota [-n|-b] path
	ls2 path [watch]
	setAcl path acl
	setquota -n|-b val path
	history 
	redo cmdno
	printwatches on|off
	delete path [version]
	sync path
	listquota path
	rmr path
	get path [watch]
	create [-s] [-e] path data acl
	addauth scheme auth
	quit 
	getAcl path
	close 
	connect host:port

Now, type quit to exit from the connected session.

You can stop the ZooKeeper with the following command:

bin/zkServer.sh stop

You should see the following output:

ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED

Create a Systemd Service file for ZooKeeper

Next, you will need to create a systemd service file to manage ZooKeeper service. You can do it with the following command:

nano /etc/systemd/system/zookeeper.service

Add the following lines:

[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

Save and close the file, when you are finished.

Next, reload the systemd daemon, start the ZooKeeper service and enable it to start on boot time with the following command:

systemctl daemon-reload
systemctl start zookeeper
systemctl enable zookeeper

You can check the status of ZooKeeper service with the following command:

systemctl status zookeeper

You should see the following output:

? zookeeper.service - Zookeeper Daemon
   Loaded: loaded (/etc/systemd/system/zookeeper.service; disabled; vendor preset: enabled)
   Active: active (running) since Wed 2019-03-06 15:03:01 UTC; 5s ago
     Docs: http://zookeeper.apache.org
  Process: 3909 ExecStart=/opt/zookeeper/bin/zkServer.sh start /opt/zookeeper/conf/zoo.cfg (code=exited, status=0/SUCCESS)
 Main PID: 3926 (java)
    Tasks: 17 (limit: 1113)
   CGroup: /system.slice/zookeeper.service
           ??3926 java -Dzookeeper.log.dir=. -Dzookeeper.root.logger=INFO,CONSOLE -cp /opt/zookeeper/bin/../build/classes:/opt/zookeeper/bin/..

Mar 06 15:03:00 ubuntu1804 systemd[1]: Starting Zookeeper Daemon...
Mar 06 15:03:00 ubuntu1804 zkServer.sh[3909]: ZooKeeper JMX enabled by default
Mar 06 15:03:00 ubuntu1804 zkServer.sh[3909]: Using config: /opt/zookeeper/conf/zoo.cfg
Mar 06 15:03:01 ubuntu1804 zkServer.sh[3909]: Starting zookeeper ... STARTED
Mar 06 15:03:01 ubuntu1804 systemd[1]: Started Zookeeper Daemon.

Congratulations! you have successfully installed and configured ZooKeeper single node cluster on Ubuntu 18.04 server. You can now deploy multi-node ZooKeeper cluster for production. Feel free to ask me if you have any questions.

Share this page:

0 Comment(s)