Kafka installation on Ubuntu
Apache Kafka is one of the most widely adopted distributed event-streaming platforms used by organizations to process, store, and analyze real-time data at scale.
Originally developed at LinkedIn and later open-sourced through the Apache Software Foundation, Kafka was designed to handle massive volumes of real-time data generated by millions of users worldwide.
In this hands-on tutorial, you will learn how to install Apache Kafka on Ubuntu, configure multiple Kafka brokers on a single host, create topics, publish messages, and consume messages using Kafka command-line tools.
What You'll Learn
By the end of this tutorial, you will be able to:
- Understand Kafka architecture and messaging concepts
- Install Apache Kafka on Ubuntu Linux
- Configure ZooKeeper for Kafka
- Create a multi-broker Kafka cluster
- Create and manage Kafka topics
- Publish messages using Kafka Producers
- Read messages using Kafka Consumers
- Verify Kafka services and network ports
What is Apache Kafka?
Apache Kafka is a distributed messaging and event-streaming platform that enables applications to exchange data reliably and in real time.
Kafka is commonly used for:
- Real-time analytics
- Log aggregation
- Event-driven architectures
- Application monitoring
- Data pipelines
- Microservices communication
- IoT data processing
Core Components
- Producer – Sends messages to Kafka Topics.
- Broker – Kafka server responsible for storing messages.
- Topic – Logical channel used for message categorization.
- Partition – Enables scalability and parallel processing.
- Consumer – Reads messages from Topics.
- Consumer Group – Multiple consumers sharing workload.
- ZooKeeper – Coordinates broker metadata (used in traditional Kafka deployments).
Prerequisites
Kafka runs on the Java Runtime Environment (JRE), so Java must be installed before Kafka can be started.
Install supporting Tools
We'll also install several useful utilities for troubleshooting and administration.
apt update apt install -y net-tools jq tree
Why These Tools?
| Tool | Purpose |
|---|---|
| netstat | Check network ports used by Kafka |
| jq | Parse JSON output |
| tree | Display directory structures |
Install Open JDK
apt install -y openjdk-21-jdk-headlessVerify and confirm the Java installation get the java version : `java -version`
![]() |
| Install Open JDK 21 on Ubuntu |
Install Apache Kafka on Ubuntu
At the time of writing this post, Apache Kafka provides multiple stable releases. You can always verify the latest version from the official Kafka download page.Download Kafka:
wget https://dlcdn.apache.org/kafka/3.9.0/kafka_2.12-3.9.0.tgz # extract the file and cd into the folder tar -xzf kafka_2.12-3.9.0.tgz cd kafka_2.12-3.9.0
tree -L 1![]() |
| Kafka installer download and extract |
Start ZooKeeper
Traditional Kafka deployments require ZooKeeper to coordinate broker metadata and cluster state information.
Default ZooKeeper configuration:
tickTime=2000 dataDir=/tmp/zookeeper clientPort=2181Start ZooKeeper using the following command:
bin/zookeeper-server-start.sh config/zookeeper.properties
Verify ZooKeeper Status
Open another terminal and run:
echo "Are you there ZooKeeper" | nc localhost 2181
Expected response:
imokimok, ZooKeeper is running successfully.Configure Multiple Kafka Brokers on a Single Host
For learning purposes, running multiple brokers on a single server is an excellent way to understand Kafka clustering concepts.
Navigate to the configuration directory::cd kafka_2.12-3.9.0/config/Most common changes are with the 3 parameters on broker configuration:
#Create separate broker configurations files: cp server.properties server1.properties # for broker1 cp server.properties server2.properties # for broker2
Configure Broker 1:
server1.properties:broker.id=1 listeners=PLAINTEXT://localhost:9092 log.dirs=/tmp/kafka-logs-1
Configure Broker 2
Edit server2.properties:
broker.id=2 listeners=PLAINTEXT://localhost:9093 log.dirs=/tmp/kafka-logs-2and then start these three servers in background as follows:
Start Kafka Brokers
Broker 1:
cd ~/kafka_2.12-3.9.0 nohup bin/kafka-server-start.sh config/server1.properties >broker1.out 2>&1 & tail -f broker1.out
Broker 2:
nohup bin/kafka-server-start.sh config/server2.properties >broker2.out 2>&1 & tail -f broker2.outVerify Kafka Processes
Check Java processes:
#confirm with process check and network stats jps -lm
Verify listening ports: netstat -tulpn|grep javaYou should see:
- ZooKeeper on port 2181
- Kafka Broker 1 on port 9092
- Kafka Broker 2 on port 9093
![]() |
| Kafka Processes and Network Ports |
Create a Kafka Topic
A Topic is the logical channel through which messages are exchanged. Add another terminal window create topic 'test' with the following commands:cd ~/kafka_2.12-3.9.0/ bin/kafka-topics.sh --create \
--bootstrap-server \ localhost:9092 --replication-factor 1 \ --partitions 1 --topic test
Expected output:
Created topic testbin/kafka-topics.sh --list \
--bootstrap-server localhost:9092Output:
test
Send Messages Using a Kafka Producer
Kafka provides a built-in console producer for testing. Kafka bin 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.Run the producer and then type some text messages into the console to send to the server.
bin/kafka-console-producer.sh \Each line is treated as an individual Kafka messages.
--broker-list localhost:9092 \
--topic test For example enter the message on topic test as follows: I love India Mera Bharat mahan Viswa guru Bharat Skill India
Consume Messages Using Kafka Consumer
Now let's read the messages from the topic.bin/kafka-console-consumer.sh \same as we did for producer, Consumer can press Ctrl+C to exit.
--bootstrap-server localhost:9092 \
--topic test
Read Messages from the Beginning bin/kafka-console-consumer.sh \
--bootstrap-server localhost:9092 \
--topic test --from-beginning
Read Messages from a Specific Offset bin/kafka-console-consumer.sh \
--bootstrap-server localhost:9092 \
--topic test -o 2
Challenge for Readers
Try the following exercises:
- Create a topic with 3 partitions.
- Increase the replication factor.
- Start a third broker on port 9094.
- Create multiple consumers within a consumer group.
- Observe how Kafka distributes messages across partitions.
What's Next?
In upcoming Kafka tutorials, we'll explore:



Comments