Skip to content

Instantly share code, notes, and snippets.

@vipmax
Last active April 29, 2024 12:45
Show Gist options
  • Star 84 You must be signed in to star a gist
  • Fork 26 You must be signed in to fork a gist
  • Save vipmax/9ceeaa02932ba276fa810c923dbcbd4f to your computer and use it in GitHub Desktop.
Save vipmax/9ceeaa02932ba276fa810c923dbcbd4f to your computer and use it in GitHub Desktop.

Kafka installation with systemd

0. Create kafka user

sudo adduser kafka
sudo adduser kafka sudo
su -l kafka

1. Download and Install kafka archive

cd /opt                                                              # go to /opt folder

curl  https://dlcdn.apache.org/kafka/3.2.0/kafka_2.13-3.2.0.tgz -O   # download archive to folder
tar -zxvf kafka*.tgz                                                 # extract archive 
rm kafka*.tgz                                                        # remove archive 
ln -s kafka_2.13-3.2.0/ kafka                                        # create symbolic link  

2. Create zookeeper service file replace {{ }} with the correct value in your case

# just creates a file with the following content  
cat <<EOF > /etc/systemd/system/kafka-zookeeper.service            

[Unit]
Description=Apache Zookeeper server (Kafka)
Documentation=http://zookeeper.apache.org
Requires=network.target remote-fs.target
After=network.target remote-fs.target

[Service]
Type=simple
User={{USER || kafka}}
Environment=JAVA_HOME={{must be JAVA_HOME here || /usr/lib/jvm/java-1.8.0-openjdk}}
ExecStart=/opt/kafka/bin/zookeeper-server-start.sh /opt/kafka/config/zookeeper.properties
ExecStop=/opt/kafka/bin/zookeeper-server-stop.sh

[Install]
WantedBy=multi-user.target

EOF

3. Create kafka service file

# just creates a file with the following content  
cat <<EOF > /etc/systemd/system/kafka.service

[Unit]
Description=Apache Kafka server (broker)
Documentation=http://kafka.apache.org/documentation.html
Requires=network.target remote-fs.target
After=network.target remote-fs.target kafka-zookeeper.service

[Service]
Type=simple
User={{USER || kafka}}
Environment=JAVA_HOME={{must be JAVA_HOME here || /usr/lib/jvm/java-1.8.0-openjdk}}
ExecStart=/opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/server.properties
ExecStop=/opt/kafka/bin/kafka-server-stop.sh

[Install]
WantedBy=multi-user.target

EOF

3 * Edit service files

nano /etc/systemd/system/kafka-zookeeper.service
nano /etc/systemd/system/kafka.service

4. Edit kafka settings

nano /opt/kafka/config/server.properties
listeners=PLAINTEXT://{{IP || 192.168.0.103}}:9092

5. Reload and start the systemd services

systemctl daemon-reload
systemctl enable kafka-zookeeper.service
systemctl enable kafka.service
systemctl start kafka-zookeeper.service
systemctl start kafka.service

systemctl status kafka-zookeeper.service
systemctl status kafka.service

6. Check services state

systemctl status kafka-zookeeper.service
systemctl status kafka.service
@mlugaliki
Copy link

Very helpful. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment