Module 6: Kafka Setup & Hands-On
Chapter 6 β’ Beginner
Kafka Setup & Hands-On
You now understand Kafka's core concepts and architecture. In this module, you'll get hands-on experience by installing Kafka locally, managing topics, and using producers and consumers from the command line.
This is where Kafka moves from "theory" to "I actually ran it on my machine."
π― What You Will Learn
By the end of this module, you will be able to:
- Set up Kafka locally using manual install, Docker, or KRaft mode
- Create, list, describe, and delete topics using CLI tools
- Use console producers and consumers to send and read messages
- Work with consumer groups and see load balancing in action
- Understand and modify key Kafka configuration files
- Use basic monitoring and management commands
- Troubleshoot common issues youβll see in real environments
βοΈ Installation Methods
You can run Kafka in different ways depending on your environment and preferences.
β Method 1: Manual Installation
Prerequisites
- Java 8 or higher
- At least 4GB RAM
- Disk space for Kafka logs
Steps
- Download Kafka
- Visit: https://kafka.apache.org/downloads
- Download the latest binary (for example:
kafka_2.13-3.5.0.tgz)
- Extract and Setup
tar -xzf kafka_2.13-3.5.0.tgz
cd kafka_2.13-3.5.0
- Start Zookeeper (if using Zookeeper mode)
bin/zookeeper-server-start.sh config/zookeeper.properties
- Start Kafka Broker
bin/kafka-server-start.sh config/server.properties
β Method 2: Docker Installation (Recommended for Dev)
Using Docker simplifies setup and cleanup, especially for local development.
Docker Compose Setup
version: '3.8'
services:
zookeeper:
image: confluentinc/cp-zookeeper:latest
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
kafka:
image: confluentinc/cp-kafka:latest
depends_on:
- zookeeper
ports:
- "9092:9092"
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
Start with Docker
docker-compose up -d
This will bring up Zookeeper + Kafka with minimal effort.
β Method 3: KRaft Mode (Kafka 2.8+)
Modern Kafka can run without Zookeeper using KRaft mode.
Start Kafka in KRaft Mode
# Generate cluster ID
bin/kafka-storage.sh format -t $(bin/kafka-storage.sh random-uuid) -c config/kraft/server.properties
# Start Kafka in KRaft mode
bin/kafka-server-start.sh config/kraft/server.properties
KRaft mode is recommended for new clusters.
π§ Basic Kafka Operations
Once Kafka is running, you can interact with it using CLI tools.
1οΈβ£ Creating Topics
Using Command Line
# Create a topic
bin/kafka-topics.sh --create --topic user-events --bootstrap-server localhost:9092 --partitions 3 --replication-factor 1
# List topics
bin/kafka-topics.sh --list --bootstrap-server localhost:9092
# Describe topic
bin/kafka-topics.sh --describe --topic user-events --bootstrap-server localhost:9092
Topic Configuration Basics
- Partitions β Number of parallel streams (default: 1)
- Replication Factor β Number of copies of each partition (default: 1)
- Retention β How long messages are kept (default: 7 days, unless changed)
2οΈβ£ Producing Messages
Using Console Producer
bin/kafka-console-producer.sh --topic user-events --bootstrap-server localhost:9092
Type messages and press Enter to send them.
Useful Producer Options
- Key separator β
--property key.separator=:for key:value input - Acknowledgments β
--property acks=allfor durability - Batch Size β
--property batch.size=16384for performance tuning
3οΈβ£ Consuming Messages
Using Console Consumer
# Basic consumer
bin/kafka-console-consumer.sh --topic user-events --from-beginning --bootstrap-server localhost:9092
# Consumer with consumer group
bin/kafka-console-consumer.sh --topic user-events --group my-group --bootstrap-server localhost:9092
# Consumer printing keys and values
bin/kafka-console-consumer.sh --topic user-events --from-beginning --property print.key=true --bootstrap-server localhost:9092
Useful Consumer Options
- Read from beginning β
--from-beginning - Use consumer group β
--group group-nameto enable group coordination - Print keys β
--property print.key=trueto see keys + values
π§ͺ Hands-On Exercises
These exercises will make you comfortable with real Kafka commands.
π Exercise 1: Basic Topic Operations
Goal: Create, inspect, and delete topics.
Tasks:
- Create a topic called
orderswith 3 partitions - Create a topic called
paymentswith 2 partitions - List all topics
- Describe the
orderstopic - Delete the
paymentstopic
Commands:
# 1. Create orders topic
bin/kafka-topics.sh --create --topic orders --bootstrap-server localhost:9092 --partitions 3 --replication-factor 1
# 2. Create payments topic
bin/kafka-topics.sh --create --topic payments --bootstrap-server localhost:9092 --partitions 2 --replication-factor 1
# 3. List topics
bin/kafka-topics.sh --list --bootstrap-server localhost:9092
# 4. Describe orders topic
bin/kafka-topics.sh --describe --topic orders --bootstrap-server localhost:9092
# 5. Delete payments topic
bin/kafka-topics.sh --delete --topic payments --bootstrap-server localhost:9092
π§ͺ Exercise 2: ProducerβConsumer Workflow
Goal: Send and receive messages across topics.
Tasks:
- Start a consumer in one terminal
- Start a producer in another terminal
- Send messages with keys
- Observe what is received by the consumer
Terminal 1 β Consumer:
bin/kafka-console-consumer.sh --topic orders --from-beginning --property print.key=true --bootstrap-server localhost:9092
Terminal 2 β Producer:
bin/kafka-console-producer.sh --topic orders --property key.separator=: --bootstrap-server localhost:9092
Test Messages (type into producer):
user1:Order placed for laptop
user2:Order placed for mouse
user1:Order updated - laptop
user3:Order placed for keyboard
π§ͺ Exercise 3: Consumer Groups in Action
Goal: See load balancing and rebalancing in a real group.
Tasks:
- Create a topic with 3 partitions
- Start multiple consumers using the same group id
- Send messages and watch how they are distributed
- Add/remove consumers and observe rebalancing
Commands:
# Create topic
bin/kafka-topics.sh --create --topic user-actions --bootstrap-server localhost:9092 --partitions 3 --replication-factor 1
# Consumer 1
bin/kafka-console-consumer.sh --topic user-actions --group analytics --bootstrap-server localhost:9092
# Consumer 2 (new terminal)
bin/kafka-console-consumer.sh --topic user-actions --group analytics --bootstrap-server localhost:9092
# Consumer 3 (new terminal)
bin/kafka-console-consumer.sh --topic user-actions --group analytics --bootstrap-server localhost:9092
Now produce messages to user-actions and watch how they get split across consumers.
ποΈ Configuration Files
Understanding basic configs helps when you move beyond default setups.
Server Properties β config/server.properties
# Broker ID
broker.id=0
# Listeners
listeners=PLAINTEXT://localhost:9092
# Log directories
log.dirs=/tmp/kafka-logs
# Default replication factor
default.replication.factor=1
# Default partitions
num.partitions=1
# Log retention
log.retention.hours=168
log.retention.bytes=1073741824
# Zookeeper connection (Zookeeper mode)
zookeeper.connect=localhost:2181
Producer Properties
# Bootstrap servers
bootstrap.servers=localhost:9092
# Serializers
key.serializer=org.apache.kafka.common.serialization.StringSerializer
value.serializer=org.apache.kafka.common.serialization.StringSerializer
# Acknowledgments
acks=all
# Retries
retries=3
# Batch size
batch.size=16384
# Compression
compression.type=lz4
Consumer Properties
# Bootstrap servers
bootstrap.servers=localhost:9092
# Deserializers
key.deserializer=org.apache.kafka.common.serialization.StringDeserializer
value.deserializer=org.apache.kafka.common.serialization.StringDeserializer
# Group ID
group.id=my-consumer-group
# Auto offset reset
auto.offset.reset=latest
# Enable auto commit
enable.auto.commit=true
auto.commit.interval.ms=5000
π Monitoring and Management
Kafka includes built-in tools to inspect consumers, topics, and logs.
Consumer Group Management
# List consumer groups
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
# Describe a consumer group
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group analytics --describe
# Reset consumer group offsets
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group analytics --reset-offsets --to-earliest --topic user-actions --execute
Log and Offset Management
# Check log directories and usage
bin/kafka-log-dirs.sh --bootstrap-server localhost:9092 --describe
# Get topic offsets
bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 --topic user-actions
JMX Metrics
Kafka exposes many metrics via JMX, such as:
- Throughput β Messages per second
- Latency β End-to-end or per-request
- Consumer Lag β Delay between latest and processed messages
- Disk Usage β Per broker/topic
These can be scraped into Prometheus/Grafana or other monitoring tools.
π©Ί Troubleshooting Common Issues
Issue 1: "Topic already exists"
# Check existing topics
bin/kafka-topics.sh --list --bootstrap-server localhost:9092
# Delete and recreate if needed
bin/kafka-topics.sh --delete --topic my-topic --bootstrap-server localhost:9092
Issue 2: "Consumer group not found"
# List all consumer groups
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
# Describe a specific group
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group my-group --describe
Issue 3: "Connection refused"
- Check if Kafka is running:
ps aux | grep kafka - Check port availability:
netstat -tlnp | grep 9092 - Check firewall settings
- Verify
bootstrap.serversconfiguration matches where Kafka is listening
β Best Practices
For Development
- Use Docker β Easy to start/stop and reset state
- Single Broker β Enough for local development
- Replication Factor = 1 β No need for replication in dev
- Short Retention β Save disk space locally
For Production
- Multiple Brokers β For fault tolerance and scalability
- Replication Factor β 3 β For durability
- Proper Monitoring β JMX + dashboards + alerts
- Security β SSL/TLS, authentication, and authorization
- Resource Planning β CPU, memory, disk, and network capacity
π Next Steps
Now that you have Kafka running locally and can:
- Create and manage topics
- Produce and consume messages
- Work with consumer groups
- Use basic monitoring commands
Youβre ready to:
- Integrate Kafka into applications using official client libraries
- Build real microservices that communicate via Kafka topics
- Experiment with scaling β more brokers, more partitions, more consumers
- Add observability β dashboards, metrics, and alerts for your Kafka cluster
This hands-on experience is the foundation for using Kafka confidently in real-world systems.
Hands-on Examples
Docker Compose Setup
# docker-compose.yml
version: '3.8'
services:
zookeeper:
image: confluentinc/cp-zookeeper:latest
hostname: zookeeper
container_name: zookeeper
ports:
- "2181:2181"
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
volumes:
- zookeeper-data:/var/lib/zookeeper/data
- zookeeper-logs:/var/lib/zookeeper/log
kafka:
image: confluentinc/cp-kafka:latest
hostname: kafka
container_name: kafka
depends_on:
- zookeeper
ports:
- "9092:9092"
- "9101:9101"
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:29092,PLAINTEXT_HOST://localhost:9092
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
KAFKA_JMX_PORT: 9101
KAFKA_JMX_HOSTNAME: localhost
volumes:
- kafka-data:/var/lib/kafka/data
kafka-ui:
image: provectuslabs/kafka-ui:latest
container_name: kafka-ui
depends_on:
- kafka
ports:
- "8080:8080"
environment:
KAFKA_CLUSTERS_0_NAME: local
KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS: kafka:29092
KAFKA_CLUSTERS_0_ZOOKEEPER: zookeeper:2181
volumes:
zookeeper-data:
zookeeper-logs:
kafka-data:
# Start the environment
docker-compose up -d
# Check status
docker-compose ps
# View logs
docker-compose logs kafka
# Stop the environment
docker-compose downThis Docker Compose setup provides a complete Kafka development environment with management UI accessible at http://localhost:8080.