In this tutorial, we will show you how to install Apache Kafka on CentOS 8. For those of you who didn’t know, Apache Kafka is a distributed message agent designed to deal with huge volumes of real-time information effectively. Unlike traditional agents like ActiveMQ and RabbitMQ, Kafka functions as a bunch of one or more servers that makes it highly scalable and because of the distributed nature, it’s inbuilt fault-tolerance whilst providing greater throughput when compared to its counterparts.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘
sudo‘ to the commands to get root privileges. I will show you the step-by-step installation of Apache Kafka on a CentOS 8 server.
Install Apache Kafka on CentOS 8
Step 1. First, let’s start by ensuring your system is up-to-date.
sudo dnf update
Step 2. Installing Java.
Apache Kafka requires Java to be installed on your server. You can install OpenJDK on your machine by executing the following command:
sudo dnf install java-11-openjdk
Verify the Java version by running the following command:
Step 3. Installing Apache Kafka on CentOS 8.
First, download and extract Kafka from the Apache website. You can use
wget to download Kafka:
Then extract the archive file:
tar xzf kafka_2.13-2.4.0.tgz mv kafka_2.13-2.4.0 /usr/local/kafka
Step 4. Setup Kafka Systemd Unit Files.
systemd unit file for Zookeeper with the below command:
Add below content:
[Unit] Description=Apache Zookeeper server Documentation=http://zookeeper.apache.org Requires=network.target remote-fs.target After=network.target remote-fs.target [Service] Type=simple ExecStart=/usr/local/kafka/bin/zookeeper-server-start.sh /usr/local/kafka/config/zookeeper.properties ExecStop=/usr/local/kafka/bin/zookeeper-server-stop.sh Restart=on-abnormal [Install] WantedBy=multi-user.target
Then, to create a Kafka
systemd unit file using the following command:
Add the below content:
[Unit] Description=Apache Kafka Server Documentation=http://kafka.apache.org/documentation.html Requires=zookeeper.service [Service] Type=simple Environment="JAVA_HOME=/usr/lib/jvm/jre-11-openjdk" ExecStart=/usr/local/kafka/bin/kafka-server-start.sh /usr/local/kafka/config/server.properties ExecStop=/usr/local/kafka/bin/kafka-server-stop.sh [Install] WantedBy=multi-user.target
systemd daemon to apply changes:
Step 5. Start Kafka Server.
Kafka required ZooKeeper so first, start a ZooKeeper server on your system:
sudo systemctl start zookeeper
Next, start the Kafka server and view the running status:
sudo systemctl start kafka sudo systemctl status kafka
Step 6. Creating Topics in Apache Kafka.
The “producer” is the process responsible for put data into our Kafka. The Kafka comes with a command-line client that will take input from a file or from standard input and send it out as messages to the Kafka cluster:
cd /usr/local/kafka bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic testTopic Created topic NewTopic.
You can create multiple topics by running the same command as above. After that, you can see the created topics on Kafka by the running below command:
bin/kafka-topics.sh --list --zookeeper localhost:2181 NewTopic KafkaonCentOS8 KafkaonCentOS8
Step 7. Apache Kafka Producer and Consumer.
Let’s run the producer and then type a few messages into the console to send to the server:
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic NewTopic >Welcome to kafka >This is my new topic >
Kafka also has a command-line consumer to read data from Kafka cluster and display messages to standard output:
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic NewTopic --from-beginning Welcome to kafka This is my new topic
Congratulations! You have successfully installed Apache Kafka. Thanks for using this tutorial for installing Apache Kafka in CentOS 8 system. For additional help or useful information, we recommend you to check the official Apache Kafka website.