How To Install Apache ZooKeeper on Ubuntu 24.04 LTS
In this tutorial, we will show you how to install Apache ZooKeeper on Ubuntu 24.04 LTS. Apache ZooKeeper is a crucial component in distributed systems, providing a centralized service for maintaining configuration information, naming, and distributed synchronization. This comprehensive guide will walk you through the process of installing Apache ZooKeeper on Ubuntu 24.04 LTS, ensuring you have a robust foundation for your distributed applications.
Introduction
ZooKeeper, an open-source project under the Apache Software Foundation, offers a reliable and high-performance coordination service for distributed systems. It simplifies the development of distributed applications by providing a centralized repository for configuration management, synchronization, and group services.
Key features of Apache ZooKeeper include:
- Simple distributed coordination
- Reliability through replication
- Ordered updates
- High performance for read-dominant workloads
This guide aims to provide a step-by-step approach to installing and configuring ZooKeeper on Ubuntu 24.04 LTS, catering to both beginners and experienced system administrators.
Prerequisites
Before diving into the installation process, ensure your system meets the following requirements:
- A server running Ubuntu 24.04 LTS
- Root or sudo privileges
- Access to a terminal/command line
- An active internet connection
Minimum hardware specifications for a basic ZooKeeper setup:
- 2 CPU cores
- 4 GB RAM
- 20 GB free disk space
For production environments, consider scaling these resources based on your specific use case and expected load.
Installing Java Dependencies
ZooKeeper requires Java to run. Let’s start by installing OpenJDK, an open-source implementation of the Java Platform.
Step 1: Check Existing Java Installation
First, verify if Java is already installed on your system:
java -version
If Java is not installed, you’ll see a message indicating that the command is not found.
Step 2: Update Package Repositories
Update your system’s package index:
sudo apt update
Step 3: Install OpenJDK
Install OpenJDK 17, which is compatible with the latest ZooKeeper versions:
sudo apt install openjdk-17-jdk -y
Step 4: Verify Java Installation
After installation, verify that Java is correctly installed:
java -version
You should see output similar to:
openjdk version "17.0.x" 20xx-xx-xx
OpenJDK Runtime Environment (build 17.0.x+xx-Ubuntu-x)
OpenJDK 64-Bit Server VM (build 17.0.x+xx-Ubuntu-x, mixed mode, sharing)
Creating ZooKeeper User and Directory Structure
For security and organization, it’s best to run ZooKeeper under a dedicated user account.
Step 1: Add ZooKeeper User
Create a new system user for ZooKeeper:
sudo adduser --system --no-create-home --group zookeeper
Step 2: Set Up Home Directory
Create a home directory for ZooKeeper:
sudo mkdir /opt/zookeeper
sudo chown zookeeper:zookeeper /opt/zookeeper
Step 3: Create Data Directory
Set up a directory for ZooKeeper to store its data:
sudo mkdir /var/lib/zookeeper
sudo chown zookeeper:zookeeper /var/lib/zookeeper
Downloading and Installing ZooKeeper
Now, let’s download and install the latest stable version of Apache ZooKeeper.
Step 1: Download ZooKeeper
Navigate to the /tmp directory and download the latest ZooKeeper binary:
cd /tmp
wget https://downloads.apache.org/zookeeper/zookeeper-3.9.3/apache-zookeeper-3.9.3-bin.tar.gz
Note: Check the Apache ZooKeeper website for the latest version number and update the URL accordingly.
Step 2: Extract Archive Files
Extract the downloaded archive:
sudo tar -xzf apache-zookeeper-3.9.3-bin.tar.gz -C /opt/zookeeper --strip-components=1
Step 3: Configure Ownership and Permissions
Set the correct ownership and permissions:
sudo chown -R zookeeper:zookeeper /opt/zookeeper
Step 4: Verify Installation Files
Ensure all necessary files are present:
ls -l /opt/zookeeper
You should see directories like bin, conf, and lib, among others.
Configuring ZooKeeper
Proper configuration is crucial for ZooKeeper’s performance and security.
Step 1: Create Configuration File
Create a new configuration file based on the provided template:
sudo cp /opt/zookeeper/conf/zoo_sample.cfg /opt/zookeeper/conf/zoo.cfg
Step 2: Edit Configuration File
Open the configuration file for editing:
sudo nano /opt/zookeeper/conf/zoo.cfg
Modify or add the following essential parameters:
tickTime=2000
dataDir=/var/lib/zookeeper
clientPort=2181
initLimit=5
syncLimit=2
maxClientCnxns=60
Here’s what these parameters mean:
- tickTime: The basic time unit in milliseconds used by ZooKeeper
- dataDir: The location to store the in-memory database snapshots
- clientPort: The port to listen for client connections
- initLimit: The time limit for ZooKeeper followers to connect to a leader
- syncLimit: The time limit for followers to sync with ZooKeeper
- maxClientCnxns: The maximum number of client connections
Step 3: Advanced Configuration (Optional)
For production environments, consider adding these advanced options:
autopurge.snapRetainCount=5
autopurge.purgeInterval=24
These settings enable automatic purging of old data and snapshots, helping to manage disk space.
Security Considerations
For enhanced security, consider implementing these measures:
- Enable SSL for client-server communication
- Set up access control lists (ACLs)
- Use SASL authentication for client connections
Setting Up Systemd Service
To manage ZooKeeper as a system service, we’ll create a systemd service file.
Step 1: Create Service File
Create a new file for the ZooKeeper service:
sudo nano /etc/systemd/system/zookeeper.service
Step 2: Configure Service Parameters
Add the following content to the file:
[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
Step 3: Set Up Auto-start
Enable the ZooKeeper service to start on boot:
sudo systemctl enable zookeeper
Starting and Testing ZooKeeper
Now that we’ve configured ZooKeeper, let’s start the service and verify its operation.
Step 1: Start ZooKeeper Service
Start the ZooKeeper service:
sudo systemctl start zookeeper
Step 2: Check Service Status
Verify that ZooKeeper is running:
sudo systemctl status zookeeper
You should see output indicating that the service is active and running.
Step 3: Basic Connectivity Testing
Test the ZooKeeper connection using the command-line interface:
/opt/zookeeper/bin/zkCli.sh -server 127.0.0.1:2181
If successful, you’ll see a ZooKeeper CLI prompt.
Using ZooKeeper CLI
The ZooKeeper CLI allows you to interact with the ZooKeeper ensemble directly.
Basic CLI Commands
Here are some essential commands to get started:
ls /
: List root-level znodescreate /mynode "data"
: Create a new znodeget /mynode
: Retrieve data from a znodeset /mynode "new data"
: Update znode datadelete /mynode
: Delete a znode
Troubleshooting Connection Issues
If you encounter connection problems:
- Verify that the ZooKeeper service is running
- Check firewall settings
- Ensure the client is using the correct port
- Review ZooKeeper logs for any error messages
Troubleshooting Common Issues
Even with careful installation, you might encounter some issues. Here are solutions to common problems:
Port Conflicts
If the default port (2181) is already in use:
- Check for conflicting services:
sudo netstat -tulpn | grep 2181
- Modify the
clientPort
in zoo.cfg to use a different port - Restart the ZooKeeper service
Permission Problems
If you encounter permission denied errors:
- Verify ownership of ZooKeeper directories:
ls -l /opt/zookeeper
- Ensure the zookeeper user has appropriate permissions
- Check systemd service file for correct User and Group settings
Configuration Errors
For configuration-related issues:
- Double-check zoo.cfg for syntax errors
- Verify that all specified directories exist and are accessible
- Review ZooKeeper logs for specific error messages
Service Startup Issues
If the ZooKeeper service fails to start:
- Check system logs:
journalctl -u zookeeper
- Verify Java installation and JAVA_HOME environment variable
- Ensure all required ZooKeeper files are present and accessible
Congratulations! You have successfully installed Apache ZooKeeper. Thanks for using this tutorial for installing the Apache ZooKeeper on Ubuntu 24.04 LTS system. For additional help or useful information, we recommend you check the official Apache website.