Bootstrap

[ Spring ] Install ZooKeeper on Ubuntu24

Install JDK

install a jdk into system dirs, so all users can visit

sudo apt install openjdk-17-jdk -y
java -version
Configure JAVA_HOME
sudo open /etc/profile

replace with you own path, pirnt by java command above

export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
export PATH=$PATH:$JAVA_HOME/bin
source /etc/profile
Create ZooKeeper User
sudo adduser --system --no-create-home --group zookeeper
Set Home Directory for User
sudo mkdir /opt/zookeeper
sudo chown zookeeper:zookeeper /opt/zookeeper
Create Data Directory for ZooKeeper
sudo mkdir /var/lib/zookeeper
sudo chown zookeeper:zookeeper /var/lib/zookeeper
Download ZooKeeper Package
cd /tmp
wget https://downloads.apache.org/zookeeper/zookeeper-3.9.3/apache-zookeeper-3.9.3-bin.tar.gz
Extract ZooKeeper to Install Directory
sudo tar -xzf apache-zookeeper-3.9.3-bin.tar.gz -C /opt/zookeeper --strip-components=1
Configure Directory Permission
sudo chown -R zookeeper:zookeeper /opt/zookeeper
Verify Installation Files

this directory should contains folders like bin, conf, lib

ls -l /opt/zookeeper
Configure ZooKeeper
sudo cp /opt/zookeeper/conf/zoo_sample.cfg /opt/zookeeper/conf/zoo.cfg
sudo nano /opt/zookeeper/conf/zoo.cfg
tickTime=2000
dataDir=/var/lib/zookeeper
clientPort=2181
initLimit=5
syncLimit=2
maxClientCnxns=60
admin.enableServer=true
admin.serverPort=20001
Create System Service for ZooKeeper
sudo nano /etc/systemd/system/zookeeper.service
[Unit]
Description=Apache ZooKeeper service
Documentation=http://zookeeper.apache.org
Requires=network.target
After=network.target

[Service]
Type=forking
User=zookeeper
Group=zookeeper
ExecStart=/opt/zookeeper/bin/zkServer.sh start /opt/zookeeper/conf/zoo.cfg
ExecStop=/opt/zookeeper/bin/zkServer.sh stop
ExecReload=/opt/zookeeper/bin/zkServer.sh restart
WorkingDirectory=/opt/zookeeper

[Install]
WantedBy=multi-user.target
sudo systemctl enable zookeeper
sudo systemctl stop zookeeper
sudo systemctl start zookeeper
sudo systemctl status zookeeper
Test ZooKeeper Service

if successful, you will see a ZooKeeper command line prompt

/opt/zookeeper/bin/zkCli.sh -server 127.0.0.1:2181
ls /

also, you can launch the admin webpage

http://localhost:20001/commands
Check Startup Failure

if service start up failed, print log to get a check

journalctl -u zookeeper
;