- Create a simple Kafka environment using Docker Compose.
- Develop a Python-based Kafka producer that generates messages.
- Build a Kafka consumer to read and process those messages.
Environment Setup
First, update your system and install the necessary packages for Python 3 and virtual environments. Run the following commands in your KodeKloud playground labs terminal:Creating a virtual environment helps prevent conflicts with system-wide packages and ensures a smooth dependency management experience.
Setting Up Kafka with Docker Compose
Next, configure Kafka and Zookeeper using Docker Compose. Create a file nameddocker-compose.yaml and paste the content below. This configuration uses the Zookeeper image (required for managing the Kafka cluster) and the Confluent Kafka image that relies on Zookeeper.
Please note that a terminal view is provided in the image below for illustration. The file name and its content remain unchanged.

Creating and Validating Kafka Topics
With the Kafka cluster running, proceed to list the available topics:sample-topic:
Adjust the number of partitions and the replication factor as needed. These parameters are critical for achieving higher throughput and ensuring fault tolerance in production environments.
Producing Messages with a Kafka Producer
Now, let’s create a Python script to produce sample events to our Kafka topic. Open a text editor (e.g., usingvim) and create a file named python-kafka-producer.py.
An optional terminal screenshot is shown below for visual reference. Follow the written instructions to enter the code.

Consuming Messages with a Kafka Consumer
In a separate terminal, activate the Python virtual environment and create a new file namedpython-kafka-consumer.py:
sample-topic and begin printing any messages it receives from Kafka.
Demo Overview
In summary, this lesson demonstrated how to:- Set up a Kafka cluster using Docker Compose.
- Create and validate Kafka topics.
- Develop Python scripts for both producing and consuming messages.