00:00

Apache Kafka Tools and CLI Commands

Last updated:

Apache Kafka ships with a set of command-line tools in its bin directory. They let you create and inspect topics, send and read test messages, check consumer lag, reset offsets, inspect the KRaft quorum and measure performance, all without writing code.

This tutorial walks through the tools you use most often, in the order you would use them on a new cluster, with real output. Every tool connects with --bootstrap-server; the old --zookeeper options were removed in Kafka 4.0. The outputs shown were produced with the Kafka 4.0 tools against a local single-broker cluster; IDs, ports and numbers will differ on yours.

1. What Are the Kafka Tools?

The tools are shell scripts (.sh on Linux and macOS, .bat under bin/windows) that run Java classes from the Kafka distribution. Most of them use the Admin, producer or consumer APIs, so anything they do can also be done from code.

Tip: Package managers sometimes install the tools without the .sh suffix, for example kafka-topics with Homebrew. The options are the same.

2. Starting a Local KRaft Cluster

A single-node cluster for development needs a cluster ID and formatted storage before the first start:

Terminal
KAFKA_CLUSTER_ID="$(bin/kafka-storage.sh random-uuid)"
bin/kafka-storage.sh format --standalone -t $KAFKA_CLUSTER_ID -c config/server.properties
bin/kafka-server-start.sh config/server.properties

3. Managing Topics with kafka-topics.sh

Terminal
bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic invoices --partitions 3 --replication-factor 1
bin/kafka-topics.sh --bootstrap-server localhost:9092 --list
bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic invoices
Output
Created topic invoices.
invoices
Topic: invoices	TopicId: GHz919jiS0OcZaKCo8FopA	PartitionCount: 3	ReplicationFactor: 1	Configs: min.insync.replicas=1
	Topic: invoices	Partition: 0	Leader: 0	Replicas: 0	Isr: 0	Elr: 	LastKnownElr: 
	Topic: invoices	Partition: 1	Leader: 0	Replicas: 0	Isr: 0	Elr: 	LastKnownElr: 
	Topic: invoices	Partition: 2	Leader: 0	Replicas: 0	Isr: 0	Elr: 	LastKnownElr: 

--describe shows the leader, replicas and ISR of each partition; recent versions also show the Eligible Leader Replicas (Elr). Other useful options are --alter --partitions 6 (partitions can only be increased), --delete, --under-replicated-partitions and --unavailable-partitions. Production topics should use --replication-factor 3.

4. Changing Configuration with kafka-configs.sh

Terminal
bin/kafka-configs.sh --bootstrap-server localhost:9092 --alter --entity-type topics \
  --entity-name invoices --add-config retention.ms=86400000
bin/kafka-configs.sh --bootstrap-server localhost:9092 --describe --entity-type topics --entity-name invoices
Output
Completed updating config for topic invoices.
Dynamic configs for topic invoices are:
  retention.ms=86400000 sensitive=false synonyms={DYNAMIC_TOPIC_CONFIG:retention.ms=86400000}

Use --delete-config retention.ms to return to the default. The same tool changes dynamic broker configs (--entity-type brokers), client quotas (--entity-type users or clients) and SCRAM credentials.

5. Sending and Reading Messages

The console producer reads one record per line. With parse.key=true, the part before the separator becomes the key:

Terminal
bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic invoices \
  --property parse.key=true --property key.separator=:
>INV-1:{"amount":120}
>INV-2:{"amount":75}
>INV-3:{"amount":310}

The console consumer prints records. --from-beginning applies only when the group has no committed offsets:

Terminal
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic invoices --group invoice-audit \
  --from-beginning --property print.key=true --property print.partition=true --timeout-ms 8000
Output
Partition:1	INV-3	{"amount":310}
Partition:0	INV-1	{"amount":120}
Partition:0	INV-2	{"amount":75}
Processed a total of 3 messages

Order is guaranteed only within a partition: INV-1 and INV-2 share partition 0 and appear in order, while INV-3 from partition 1 can appear before them. Without --timeout-ms or --max-messages, the consumer runs until you press Ctrl+C.

6. Consumer Groups and Lag

Terminal
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group invoice-audit
Output
invoice-audit

Consumer group 'invoice-audit' has no active members.

GROUP           TOPIC           PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID     HOST            CLIENT-ID
invoice-audit   invoices        1          1               1               0               -               -               -
invoice-audit   invoices        0          2               2               0               -               -               -
invoice-audit   invoices        2          0               0               0               -               -               -

LAG is LOG-END-OFFSET - CURRENT-OFFSET, the number of records the group has not yet processed. While consumers are running, the last three columns show which member owns each partition. Add --members --verbose to list members and their assignments, or --state to see the group state.

6.1 Resetting offsets

Terminal
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group invoice-audit \
  --topic invoices --reset-offsets --to-earliest --dry-run
Output
GROUP                          TOPIC                          PARTITION  NEW-OFFSET     
invoice-audit                  invoices                       1          0              
invoice-audit                  invoices                       0          0              
invoice-audit                  invoices                       2          0              

--dry-run only shows the plan. Replace it with --execute to apply it; the group must have no active members. More details are in Kafka Offset Management.

7. Partition End Offsets with kafka-get-offsets.sh

Terminal
bin/kafka-get-offsets.sh --bootstrap-server localhost:9092 --topic invoices
Output
invoices:0:2
invoices:1:1
invoices:2:0

The format is topic:partition:offset. By default it prints the latest offset; --time -2 gives the earliest offset and --time <epoch-ms> the first offset at or after a timestamp.

8. Checking the KRaft Quorum

Terminal
bin/kafka-metadata-quorum.sh --bootstrap-server localhost:9092 describe --status
Output
ClusterId:              D59JgZPbRrq3ROvNY99_2A
LeaderId:               0
LeaderEpoch:            1
HighWatermark:          215
MaxFollowerLag:         0
MaxFollowerLagTimeMs:   0
CurrentVoters:          [{"id": 0, "directoryId": null, "endpoints": ["CONTROLLER://localhost:55775"]}]
CurrentObservers:       []

LeaderId is the active controller. In a healthy multi-controller cluster, MaxFollowerLag stays close to zero. describe --replication shows the lag of every voter and observer. See Leader Election in Apache Kafka for how the controller is chosen.

9. Measuring Performance

Terminal
bin/kafka-producer-perf-test.sh --topic invoices --num-records 10000 --record-size 200 \
  --throughput -1 --producer-props bootstrap.servers=localhost:9092
Output
10000 records sent, 60241.0 records/sec (11.49 MB/sec), 26.32 ms avg latency, 102.00 ms max latency, 30 ms 50th, 42 ms 95th, 44 ms 99th, 45 ms 99.9th.

These numbers come from a single-broker test cluster on a laptop and only show the format. Run perf tests against a cluster similar to production, with realistic record sizes, acks and compression; kafka-consumer-perf-test.sh measures the read side.

10. Other Useful Tools

ToolPurpose
kafka-reassign-partitions.shMove partitions between brokers or change replica lists
kafka-leader-election.shTrigger preferred or unclean leader elections
kafka-log-dirs.shShow partition sizes per broker and log directory
kafka-dump-log.shPrint the contents of segment files for debugging
kafka-delete-records.shDelete records before an offset
kafka-acls.shManage access control lists
kafka-transactions.shList, describe and abort hanging transactions
kafka-features.shShow and upgrade feature levels such as metadata.version
kafka-share-groups.shInspect share groups (KIP-932, Kafka 4.x)
kafka-streams-application-reset.shReset a Kafka Streams application for reprocessing

Every tool prints its options with --help. Pass security settings with --command-config client.properties (or --producer.config and --consumer.config for the console tools).

11. Common Mistakes

  • Using --zookeeper options from old blog posts; they no longer exist in Kafka 4.x.
  • Resetting offsets with --execute while consumers run, which fails; stop the group first.
  • Reducing partitions with --alter, which is not supported; create a new topic instead.
  • Treating console consumer order as topic order; order is per partition.
  • Running admin commands without TLS/SASL settings on secured clusters and getting timeouts instead of clear errors.

12. Conclusion

Kafka's CLI tools cover the full life cycle of a cluster: kafka-storage to format it, kafka-topics and kafka-configs to manage topics, the console tools to test data flow, kafka-consumer-groups and kafka-get-offsets to follow progress, kafka-metadata-quorum to check KRaft, and the perf tools to measure capacity. Keep commands in scripts or runbooks, always try --dry-run where it exists, and use the Admin API when you need the same operations from code.

13. References