1. Introduction
-
Kafka provides CLI tools (usually inside
bin/
orbin/windows/
) to manage:- Brokers (the Kafka servers)
- Topics (data channels)
- Producers (apps sending data)
- Consumers (apps reading data)
- Consumer groups
- Cluster configuration & monitoring
π Default path:
- Linux/Mac:
kafka/bin/
- Windows:
kafka/bin/windows/
2. Setup & Starting Kafka
Before using CLI, ensure ZooKeeper (if < 2.8) and Kafka broker are running.
# Start Zookeeper (older versions only)
bin/zookeeper-server-start.sh config/zookeeper.properties
# Start Kafka broker
bin/kafka-server-start.sh config/server.properties
π In newer versions (Kafka KRaft mode), ZooKeeper is not required.
3. Topic Management
Create a Topic
bin/kafka-topics.sh --create \
--topic first-topic \
--bootstrap-server localhost:9092 \
--partitions 3 \
--replication-factor 1
List Topics
bin/kafka-topics.sh --list --bootstrap-server localhost:9092
Describe a Topic
bin/kafka-topics.sh --describe \
--topic first-topic \
--bootstrap-server localhost:9092
Delete a Topic
bin/kafka-topics.sh --delete \
--topic first-topic \
--bootstrap-server localhost:9092
4. Producing Messages
Simple Producer
bin/kafka-console-producer.sh \
--broker-list localhost:9092 \
--topic first-topic
- Type messages directly β Press Enter β Sent to Kafka.
With Keys
bin/kafka-console-producer.sh \
--broker-list localhost:9092 \
--topic first-topic \
--property "parse.key=true" \
--property "key.separator=:"
Then:
user1:hello
user2:hi
5. Consuming Messages
Simple Consumer
bin/kafka-console-consumer.sh \
--bootstrap-server localhost:9092 \
--topic first-topic \
--from-beginning
Consumer with Group
bin/kafka-console-consumer.sh \
--bootstrap-server localhost:9092 \
--topic first-topic \
--group my-group
6. Consumer Group Management
List Groups
bin/kafka-consumer-groups.sh \
--bootstrap-server localhost:9092 --list
Describe a Group
bin/kafka-consumer-groups.sh \
--bootstrap-server localhost:9092 \
--describe --group my-group
Reset Offsets
bin/kafka-consumer-groups.sh \
--bootstrap-server localhost:9092 \
--group my-group \
--topic first-topic \
--reset-offsets --to-earliest --execute
7. Cluster & Broker Utilities
Cluster Info
bin/kafka-broker-api-versions.sh --bootstrap-server localhost:9092
Broker Config
bin/kafka-configs.sh --bootstrap-server localhost:9092 \
--entity-type brokers --entity-name 0 --describe
8. Monitoring & Debugging
Check Lag
bin/kafka-consumer-groups.sh \
--bootstrap-server localhost:9092 \
--group my-group --describe
List Partitions & Leaders
bin/kafka-topics.sh \
--bootstrap-server localhost:9092 \
--topic first-topic --describe
Dump Log Segments (advanced debugging)
bin/kafka-dump-log.sh --files /tmp/kafka-logs/first-topic-0/00000000000000000000.log
9. Admin CLI (Newer Versions)
Kafka now has kafka-cluster.sh
, kafka-storage.sh
, kafka-metadata-quorum.sh
for KRaft mode:
bin/kafka-storage.sh info -c config/kraft/server.properties
10. Hands-On Practice Plan
β
Day 1 β Install Kafka, start broker, create/delete/list topics
β
Day 2 β Use producer & consumer CLI, send & read messages
β
Day 3 β Experiment with consumer groups, offsets, reset-offsets
β
Day 4 β Explore configs, broker info, and monitor lag
β
Day 5 β Debug using dump-log & move to advanced KRaft commands
Top comments (0)