This tutorial provides you with step-by-step instructions to install Apache Kafka on Ubuntu 22.04 LTS Linux system. You will also learn to create topics in Kafka and run producer and consumer nodes.

Prerequisites

You must have sudo privileged account access to the Ubuntu 22.04 Linux system.

Step 1 – Installing Java

We can run the Apache Kafka server on systems that support Java. So make sure, you have Java installed on your Ubuntu system. Use the following commands to install OpenJDK on your Ubuntu system from the official repositories. Verify the current active Java version.

Step 2 – Download Latest Apache Kafka

You can download the latest Apache Kafka binary files from its official download page. Alternativaly you can download Kafka 3.2.0 with the below command. Then extract the downloaded archive file and place them under /usr/local/kafka directory.

Step 3 – Create Systemd Startup Scripts

Now, create systemd unit files for the Zookeeper and Kafka services. That will help you to start/stop the Kafka service in an easy way. First, create a systemd unit file for Zookeeper: And add the following content: Save the file and close it. Next, create a systemd unit file for the Kafka service: Add the below content. Make sure to set the correct JAVA_HOME path as per the Java installed on your system. Save the file and close. Reload the systemd daemon to apply new changes. This will reload all the systemd files in the system environment.

Step 4 – Start Zookeeper and Kafka Services

Let’s start both services one by one. First, you need to start the ZooKeeper service and then start Kafka. Use the systemctl command to start a single-node ZooKeeper instance. Verify both of the services status: That’s it. You have successfully installed the Apache Kafka server on Ubuntu 22.04 system. Next, we will create topics in the Kafka server.

Step 5 – Create a Topic in Kafka

Kafka provides multiple pre-built shell script to work on it. First, create a topic named “testTopic” with a single partition with single replica: Here:

Use –create option to create a new topic The –replication-factor describes how many copies of data will be created. As we are running with a single instance keep this value 1. Set the –partitions options as the number of brokers you want your data to be split between. As we are running with a single broker keep this value 1. The –topic define the name of the topic

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: The output looks like the below screenshot: Alternatively, instead of manually creating topics you can also configure your brokers to auto-create topics when a non-existent topic is published to.

Step 6 – Send and Receive Messages in 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. The default Kafka sends each line as a separate message. Let’s run the producer and then type a few messages into the console to send to the server. You can exit this command or keep this terminal running for further testing. Now open a new terminal to the Kafka consumer process on the next step. Kafka also has a command-line consumer to read data from the Kafka cluster and display messages to standard output. Now, If you have still running Kafka producer in another terminal. Just type some text on that producer terminal. it will immediately be visible on the consumer terminal. See the below screenshot of Kafka producer and consumer in working:

Conclusion

This tutorial helped you to install and configure the Apache Kafka server on an Ubuntu 22.04 Linux system. Additionally, you learned to create a new topic in the Kafka server and run a sample production and consumer process with Apache Kafka.

How to Install Apache Kafka on Ubuntu 22 04   TecAdmin - 98How to Install Apache Kafka on Ubuntu 22 04   TecAdmin - 26