how to install Apache Kafka on Ubuntu 22.04

Apache Kafka is a streaming platform developed by the Apache Software Foundation. The platform is open source and distributed and can be installed on any platform as long as it supports Java. Next to, Apache Kafka is written in Java and Scala. This guide provides a handy installation tutorial Apache Kafka on Ubuntu 22.04.

Start with Apache Kafka

Before installing the Apache Kafka, you must have Java installed and have a user account with sudo privileges. In addition, 2GB or more RAM is recommended for Kafka to run properly.

The following steps will guide you through the installation of Apache Kafka.

Install Java

Java is required to install Kafka. Check if your Ubuntu has Java installed by checking the version with the following command:

$ Java –Execution

If Java is not installed, use the following commands to install Java OpenJDK.

$ sudo appropriate update

$ sudo suitable To install default-jdk

Creating a User Account

If Java is already installed, create a non-root user account. We have to give him something too sudo Privileges by adding the user to sudo Group with the following command:

$ sudo adduser linuxhint

$ sudo adduser linuxhint sudo

Sign in to the newly created user account.

$ so -l Linux hint

Install Kafka

You must download the latest one Apache Kafka from the official download page. Load the binaries with the wget Command as shown below:

$ wget https://downloads.apache.org/Kafka/3.2.3/kafka_2.12-3.2.3.tgz

After the binary is downloaded, extract it with the tar command and move the extracted directory to the /opt/kafka.

Next, create the systemd scripts for the zookeeper and the Kafka Services that help start and stop the services.

Use an editor of your choice to create the systemd scripts and paste the following content. Start with the Zookeeper:

$ sudo nano /Etc/systemd/system/zookeeper.service

Paste the following:

[Unit]

description=Apache Zookeeper Server

documentation=https://zookeeper.apache.org

Requires=network.destination remote fs.destination

To=network.destination remote fs.destination

[Service]

Type=simple

ExecStart=/opt/Kafka/container/zookeeper-server-start.sh /opt/Kafka/config/zookeeper.properties

ExecStop=/opt/Kafka/container/zookeeper-server-stop.sh

Start anew=on-abnormal

[Install]

Wanted by=multi-user.target


Save the file. Create the systemd file for Kafka and paste the following content:

$ sudo nano /Etc/systemd/system/kafka.service

When pasting, make sure you set the correct path for the Java you have installed on your system.

[Unit]

description=Apache Kafka server

documentation=https://kafka.apache.org/Documentation.html

Requires=zookeeper.service

[Service]

Type=simple

Surroundings=“JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64”

ExecStart=/opt/Kafka/container/kafka-server-start.sh /opt/Kafka/config/server.properties

ExecStop=/opt/Kafka/container/kafka-server-stop.sh

Start anew=on-abnormal

[Install]

Wanted by=multi-user.target

When finished, apply the changes by reloading the systemd daemon:

$ sudo systemctl daemon-reload

Next, enable the Zookeeper service and start it with the following commands:

$ sudo systemctl enable zookeeper

$ sudo start systemctl zookeepe<strong>rightstrong>

You also have to do the same for Kafka:

$ sudo systemctl enable Kafka

$ sudo systemctl starts kafka

Once you start the services you can check their status before we create a theme in Kafka.

The good thing about Kafka is that there are several scripts that you can use.

Let’s create a new theme named linuxhint1 Use of kafka-topics.sh Script with one partition and one replication. Use the following command:

$ sudo -u Linux hint /opt/Kafka/container/kafka-topics.sh –create –bootstrap-server local host:9092 –replication-factor 1 –Partitions 1 –Theme linuxhint1

Note that our theme is being created. We can see the previous message to verify this.

Alternatively, you can also use to list the available themes -list option in the following command. It should return the topic we created:S

$ sudo -u Linux hint /opt/Kafka/container/kafka-topics.sh –list –bootstrap-server local host:9092

After the Kafka topic is created, you can start writing the Streams data to the Kafka-console-producer.sh and see if it is reflected in yours consumer.sh.

Open your shell and access the theme we created using the producer.sh as shown below:

$ sudo -u Linux hint /opt/Kafka/container/kafka-console-producer.sh –Broker list local host:9092 –Theme linuxhint1

Next, open another shell and access the Kafka theme using the consumer.sh file.

$ sudo -u Linux hint /opt/Kafka/container/kafka-console-consumer.sh –bootstrap-server local host:9092 –Theme linuxhint1 –from the beginning

With the two shells open, you can send messages on the producer console. Whatever you type will appear in the consumer console, confirming that our Apache Kafka is running.

Conclusion

With this guide you now have all the steps to follow to install Apache Kafka in your Ubuntu 22.04. Hope you managed to follow each step and install yours Apache Kafka and create themes to run simple consumer and producer production. You can implement the same in a large production.

Related Posts