Home / How To / How to install Apache ZooKeeper on Ubuntu 20.04

How to install Apache ZooKeeper on Ubuntu 20.04



ZooKeeper is a software project by the Apache Software Foundation that provides a high-performance shared data service used to build distributed applications. It is used by a cluster to maintain shared data with robust synchronization techniques. It provides a simple interface for managing configuration information, naming, distributed synchronization, and providing group services.

In this tutorial we will show you how to install Apache ZooKeeper on an Ubuntu 20.04 server.

Conditions

  • A server running Ubuntu 20.04.
  • A root password is configured on the server.

Install Java

Apache ZooKeeper is written in Java so you need to install Java in your system. You can install it with the following command:

apt-get install default-jdk -y

After installing Java, verify the installed version of Java with the following command:

java --version

You should get the following output:

openjdk 11.0.8 2020-07-14
OpenJDK Runtime Environment (build 11.0.8+10-post-Ubuntu-0ubuntu120.04)
OpenJDK 64-Bit Server VM (build 11.0.8+10-post-Ubuntu-0ubuntu120.04, mixed mode, sharing)

Install Zookeeper

Before installing Apache zookeeper, you must create a new zookeeper user. You can create it with the following command:

useradd zookeeper -m

Then set the password for this user and add it to the sudo group with the following command:

usermod --shell /bin/bash zookeeper
passwd zookeeper
usermod -aG sudo zookeeper

Then create a zookeeper data directory and change ownership of this directory:

mkdir /zookeeper
chown -R zookeeper:zookeeper /zookeeper

Then you need to download the latest version of the zookeeper to / opt directory. You can download it with the following command:

cd /opt
wget https://mirrors.estointernet.in/apache/zookeeper/zookeeper-3.6.2/apache-zookeeper-3.6.2-bin.tar.gz

When the download is complete, extract the downloaded file with the following command:

tar -xvzf apache-zookeeper-3.6.2-bin.tar.gz

Then rename the extracted directory to zookeeper with the following command:

mv apache-zookeeper-3.6.2-bin zookeeper

Then change the ownership of the zookeeper directory with the following command:

chown -R zookeeper:zookeeper /opt/zookeeper

Configure ZooKeeper in standalone mode

Next, you need to create a ZooKeeper configuration file to set ZooKeeper to standalone mode. You can create it with the following command:

nano /opt/zookeeper/conf/zoo.cfg

Add the following lines:

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

Save and close the file when you are done. Then start the ZooKeeper service with the following command:

cd /opt/zookeeper
bin/zkServer.sh start

You should get the following output:

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

By default, ZooKeeper listens on port 2181. You can check this with the following command:

ss -ntpl | grep 2181

You should get the following output:

LISTEN    0         50                       *:2181                   *:*        users:(("java",pid=12749,fd=53)) 

You can now connect to the ZooKeeper local server with the following command:

bin/zkCli.sh -server 127.0.0.1:2181

Once connected, you should get the following output:

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: 127.0.0.1:2181(CONNECTED) 0] 

Now exit the session with the following command:

quit

Then stop the ZooKeeper service 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 need to create a systemd service file to manage the ZooKeeper service. You can create it with the following command:

nano /etc/systemd/system/zookeeper.service

Add the following lines:Advertisement

[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, and then reload the system daemon to apply the configuration changes:

systemctl daemon-reload

Then change the ownership of ZooKeeper and the data directory with the following command:

chown -R zookeeper:zookeeper /opt/zookeeper
chown -R zookeeper:zookeeper /zookeeper

Then start the ZooKeeper service and activate it when restarting the system with the following command:

systemctl start zookeeper
systemctl enable zookeeper

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

systemctl status zookeeper

You should get the following output:

? zookeeper.service - Zookeeper Daemon
     Loaded: loaded (/etc/systemd/system/zookeeper.service; disabled; vendor preset: enabled)
     Active: active (running) since Sun 2020-09-27 06:43:28 UTC; 8s ago
       Docs: http://zookeeper.apache.org
    Process: 13915 ExecStart=/opt/zookeeper/bin/zkServer.sh start /opt/zookeeper/conf/zoo.cfg (code=exited, status=0/SUCCESS)
   Main PID: 13946 (java)
      Tasks: 37 (limit: 4691)
     Memory: 50.6M
     CGroup: /system.slice/zookeeper.service
             ??13946 java -Dzookeeper.log.dir=/opt/zookeeper/bin/../logs -Dzookeeper.log.file=zookeeper-zookeeper-server-ubuntu2004.log -Dzook>

Sep 27 06:43:27 ubuntu2004 systemd[1]: Starting Zookeeper Daemon...
Sep 27 06:43:27 ubuntu2004 zkServer.sh[13915]: /usr/bin/java
Sep 27 06:43:27 ubuntu2004 zkServer.sh[13915]: ZooKeeper JMX enabled by default
Sep 27 06:43:27 ubuntu2004 zkServer.sh[13915]: Using config: /opt/zookeeper/conf/zoo.cfg
Sep 27 06:43:28 ubuntu2004 zkServer.sh[13915]: Starting zookeeper ... STARTED
Sep 27 06:43:28 ubuntu2004 systemd[1]: Started Zookeeper Daemon.

Conclusion

Congratulations! you have installed and configured ZooKeeper on Ubuntu 20.04 server. You can now easily build and deploy the distributed application with ZooKeeper.


Source link