Redhat 7 setup Apache Kafka

In this article, will show you how to install apache kafka on RHEL 7 and run it as a service in background

Prerequisites

You’ll need Java 8+ installed on your machine.

Download latest version of Apache Kafka

# Choose a folder to hold apache kafka installation, eg: /usr/share
cd /usr/share

# Download latest version of Apache Kafka
sudo wget https://dlcdn.apache.org/kafka/3.1.0/kafka_2.13-3.1.0.tgz

# Unpackage file.
sudo tar -xzf kafka_2.13-3.1.0.tgz

# Rename it to kafka
sudo mv kafka_2.13-3.1.0 kafka

Create a kafka user and grant all access to Apache Kafka files.

# Add a kafka user
sudo useradd kafka

# Change file ownership to kafka user
sudo chown -R kafka:kafka /usr/share/kafka*
sudo chown -R kafka:kafka /tmp/zookeeper*
sudo chown -R kafka:kafka /tmp/kafka-logs*

Create service file for zookeeper and kafka

# Create zookeeper service file: /etc/systemd/system/zookeeper.service
sudo vi /etc/systemd/system/zookeeper.service

# Add below content
[Unit]
Description=zookeeper
After=syslog.target network.target

[Service]
Type=simple

User=kafka
Group=kafka

ExecStart=/usr/share/kafka/bin/zookeeper-server-start.sh /usr/share/kafka/config/zookeeper.properties
ExecStop=/usr/share/kafka/bin/zookeeper-server-stop.sh

[Install]
WantedBy=multi-user.target
# Create kafka service file: /etc/systemd/system/kafka.service
sudo vi /etc/systemd/system/kafka.service

# Add below content
[Unit]
Description=Apache Kafka
Requires=zookeeper.service
After=zookeeper.service

[Service]
Type=simple

User=kafka
Group=kafka

ExecStart=/usr/share/kafka/bin/kafka-server-start.sh /usr/share/kafka/config/server.properties
ExecStop=/usr/share/kafka/bin/kafka-server-stop.sh

[Install]
WantedBy=multi-user.target

Reload systemd files

sudo systemctl daemon-reload

Start zookeeper and kafka service

# Start zookeeper
sudo systemctl start zookeeper

# Start kafka
sudo systemctl start kafka

# Check zookeeper status
sudo systemctl status zookeeper.service

# Check kafka status
sudo systemctl status kafka.service

# Enable both service on machine start
sudo systemctl enable zookeeper.service
sudo systemctl enable kafka.service

# Zookeeper default listening port : 2181
# Kafka default listening port: 9092

Add zookeeper & kafka to firewall rule

# Optional, if you want your kafka to be accessible externally, add firewall rule
sudo firewall-cmd --add-port={2181/tcp,9092/tcp} --permanent

# Reload firewall
sudo firewall-cmd --reload 

Leave a Comment