Kafka CLI Tools
Command-line tools for Apache Kafka administration and operations.
Tool Overview
Section titled “Tool Overview”| Tool | Purpose |
|---|---|
kafka-topics.sh | Topic management |
kafka-consumer-groups.sh | Consumer group management |
kafka-configs.sh | Configuration management |
kafka-acls.sh | ACL management |
kafka-reassign-partitions.sh | Partition reassignment |
kafka-leader-election.sh | Leader election |
kafka-metadata.sh | KRaft metadata inspection |
kafka-dump-log.sh | Log segment inspection |
kafka-topics.sh
Section titled “kafka-topics.sh”List Topics
Section titled “List Topics”# List all topicskafka-topics.sh --bootstrap-server kafka:9092 --list
# List with detailskafka-topics.sh --bootstrap-server kafka:9092 --describeCreate Topic
Section titled “Create Topic”kafka-topics.sh --bootstrap-server kafka:9092 \ --create \ --topic my-topic \ --partitions 12 \ --replication-factor 3
# With configurationkafka-topics.sh --bootstrap-server kafka:9092 \ --create \ --topic my-topic \ --partitions 12 \ --replication-factor 3 \ --config retention.ms=604800000 \ --config cleanup.policy=compactDescribe Topic
Section titled “Describe Topic”# Single topickafka-topics.sh --bootstrap-server kafka:9092 \ --describe --topic my-topic
# Show under-replicated partitionskafka-topics.sh --bootstrap-server kafka:9092 \ --describe --under-replicated-partitions
# Show unavailable partitionskafka-topics.sh --bootstrap-server kafka:9092 \ --describe --unavailable-partitionsAlter Topic
Section titled “Alter Topic”# Increase partitions (cannot decrease)kafka-topics.sh --bootstrap-server kafka:9092 \ --alter --topic my-topic --partitions 24Delete Topic
Section titled “Delete Topic”kafka-topics.sh --bootstrap-server kafka:9092 \ --delete --topic my-topickafka-consumer-groups.sh
Section titled “kafka-consumer-groups.sh”List Consumer Groups
Section titled “List Consumer Groups”kafka-consumer-groups.sh --bootstrap-server kafka:9092 --list
# With statekafka-consumer-groups.sh --bootstrap-server kafka:9092 \ --list --stateDescribe Consumer Group
Section titled “Describe Consumer Group”# Show members and offsetskafka-consumer-groups.sh --bootstrap-server kafka:9092 \ --describe --group my-group
# Show only memberskafka-consumer-groups.sh --bootstrap-server kafka:9092 \ --describe --group my-group --members
# Show verbose member detailskafka-consumer-groups.sh --bootstrap-server kafka:9092 \ --describe --group my-group --members --verbose
# Show statekafka-consumer-groups.sh --bootstrap-server kafka:9092 \ --describe --group my-group --stateReset Offsets
Section titled “Reset Offsets”# Reset to earliest (dry run)kafka-consumer-groups.sh --bootstrap-server kafka:9092 \ --group my-group \ --reset-offsets \ --to-earliest \ --topic my-topic \ --dry-run
# Reset to earliest (execute)kafka-consumer-groups.sh --bootstrap-server kafka:9092 \ --group my-group \ --reset-offsets \ --to-earliest \ --topic my-topic \ --execute
# Reset to specific offsetkafka-consumer-groups.sh --bootstrap-server kafka:9092 \ --group my-group \ --reset-offsets \ --to-offset 1000 \ --topic my-topic:0 \ --execute
# Reset to timestampkafka-consumer-groups.sh --bootstrap-server kafka:9092 \ --group my-group \ --reset-offsets \ --to-datetime "2024-01-15T10:00:00.000" \ --all-topics \ --execute
# Reset by shiftingkafka-consumer-groups.sh --bootstrap-server kafka:9092 \ --group my-group \ --reset-offsets \ --shift-by -100 \ --topic my-topic \ --executeDelete Consumer Group
Section titled “Delete Consumer Group”# Group must be empty (no active members)kafka-consumer-groups.sh --bootstrap-server kafka:9092 \ --delete --group my-groupkafka-configs.sh
Section titled “kafka-configs.sh”Describe Configuration
Section titled “Describe Configuration”# Broker configkafka-configs.sh --bootstrap-server kafka:9092 \ --entity-type brokers --entity-name 1 --describe
# All brokerskafka-configs.sh --bootstrap-server kafka:9092 \ --entity-type brokers --describe --all
# Topic configkafka-configs.sh --bootstrap-server kafka:9092 \ --entity-type topics --entity-name my-topic --describe
# Client quotaskafka-configs.sh --bootstrap-server kafka:9092 \ --entity-type users --entity-name my-user --describeAlter Configuration
Section titled “Alter Configuration”# Add/update broker configkafka-configs.sh --bootstrap-server kafka:9092 \ --entity-type brokers --entity-name 1 \ --alter --add-config log.cleaner.threads=4
# Add/update topic configkafka-configs.sh --bootstrap-server kafka:9092 \ --entity-type topics --entity-name my-topic \ --alter --add-config retention.ms=86400000
# Delete config (revert to default)kafka-configs.sh --bootstrap-server kafka:9092 \ --entity-type topics --entity-name my-topic \ --alter --delete-config retention.msClient Quotas
Section titled “Client Quotas”# Set producer quotakafka-configs.sh --bootstrap-server kafka:9092 \ --entity-type users --entity-name my-user \ --alter --add-config producer_byte_rate=1048576
# Set consumer quotakafka-configs.sh --bootstrap-server kafka:9092 \ --entity-type users --entity-name my-user \ --alter --add-config consumer_byte_rate=2097152kafka-acls.sh
Section titled “kafka-acls.sh”List ACLs
Section titled “List ACLs”# All ACLskafka-acls.sh --bootstrap-server kafka:9092 --list
# For specific topickafka-acls.sh --bootstrap-server kafka:9092 \ --list --topic my-topic
# For specific principalkafka-acls.sh --bootstrap-server kafka:9092 \ --list --principal User:my-userAdd ACLs
Section titled “Add ACLs”# Producer accesskafka-acls.sh --bootstrap-server kafka:9092 \ --add \ --allow-principal User:producer-app \ --operation Write \ --operation Describe \ --topic my-topic
# Consumer accesskafka-acls.sh --bootstrap-server kafka:9092 \ --add \ --allow-principal User:consumer-app \ --operation Read \ --operation Describe \ --topic my-topic \ --group my-group
# Wildcard topic accesskafka-acls.sh --bootstrap-server kafka:9092 \ --add \ --allow-principal User:admin \ --operation All \ --topic '*'Remove ACLs
Section titled “Remove ACLs”kafka-acls.sh --bootstrap-server kafka:9092 \ --remove \ --allow-principal User:producer-app \ --operation Write \ --topic my-topickafka-reassign-partitions.sh
Section titled “kafka-reassign-partitions.sh”Generate Reassignment Plan
Section titled “Generate Reassignment Plan”# Create topics JSON filecat > topics.json << 'EOF'{ "topics": [ {"topic": "my-topic"} ], "version": 1}EOF
# Generate plankafka-reassign-partitions.sh --bootstrap-server kafka:9092 \ --topics-to-move-json-file topics.json \ --broker-list "1,2,3,4" \ --generateExecute Reassignment
Section titled “Execute Reassignment”# Save generated plan to file, then executekafka-reassign-partitions.sh --bootstrap-server kafka:9092 \ --reassignment-json-file reassignment.json \ --execute
# With throttlekafka-reassign-partitions.sh --bootstrap-server kafka:9092 \ --reassignment-json-file reassignment.json \ --throttle 50000000 \ --executeVerify Reassignment
Section titled “Verify Reassignment”kafka-reassign-partitions.sh --bootstrap-server kafka:9092 \ --reassignment-json-file reassignment.json \ --verifykafka-leader-election.sh
Section titled “kafka-leader-election.sh”# Preferred leader election for all partitionskafka-leader-election.sh --bootstrap-server kafka:9092 \ --election-type preferred \ --all-topic-partitions
# For specific topickafka-leader-election.sh --bootstrap-server kafka:9092 \ --election-type preferred \ --topic my-topic
# Unclean election (data loss risk)kafka-leader-election.sh --bootstrap-server kafka:9092 \ --election-type unclean \ --topic my-topic \ --partition 0kafka-metadata.sh (KRaft)
Section titled “kafka-metadata.sh (KRaft)”# Describe clusterkafka-metadata.sh --snapshot /var/kafka-logs/__cluster_metadata-0/*.log \ --command "describe"
# List brokerskafka-metadata.sh --snapshot /var/kafka-logs/__cluster_metadata-0/*.log \ --command "brokers"
# Show topic detailskafka-metadata.sh --snapshot /var/kafka-logs/__cluster_metadata-0/*.log \ --command "topic" --topic-name my-topickafka-dump-log.sh
Section titled “kafka-dump-log.sh”# Dump log segmentkafka-dump-log.sh --files /var/kafka-logs/my-topic-0/00000000000000000000.log \ --print-data-log
# Dump indexkafka-dump-log.sh --files /var/kafka-logs/my-topic-0/00000000000000000000.index
# Verify indexeskafka-dump-log.sh --files /var/kafka-logs/my-topic-0/00000000000000000000.log \ --index-sanity-checkConsole Producer/Consumer
Section titled “Console Producer/Consumer”Console Producer
Section titled “Console Producer”kafka-console-producer.sh --bootstrap-server kafka:9092 \ --topic my-topic
# With keykafka-console-producer.sh --bootstrap-server kafka:9092 \ --topic my-topic \ --property "parse.key=true" \ --property "key.separator=:"Console Consumer
Section titled “Console Consumer”# From beginningkafka-console-consumer.sh --bootstrap-server kafka:9092 \ --topic my-topic \ --from-beginning
# With keyskafka-console-consumer.sh --bootstrap-server kafka:9092 \ --topic my-topic \ --property print.key=true \ --property print.timestamp=true
# Specific partition and offsetkafka-console-consumer.sh --bootstrap-server kafka:9092 \ --topic my-topic \ --partition 0 \ --offset 100
# Max messageskafka-console-consumer.sh --bootstrap-server kafka:9092 \ --topic my-topic \ --max-messages 10kafka-share-groups.sh
Section titled “kafka-share-groups.sh”Share groups (KIP-932) allow multiple consumers to share partition consumption.
List Share Groups
Section titled “List Share Groups”kafka-share-groups.sh --bootstrap-server kafka:9092 --listDescribe Share Group
Section titled “Describe Share Group”# Show current statekafka-share-groups.sh --bootstrap-server kafka:9092 \ --describe --group my-share-group
# Show memberskafka-share-groups.sh --bootstrap-server kafka:9092 \ --describe --group my-share-group --members
# Show state summarykafka-share-groups.sh --bootstrap-server kafka:9092 \ --describe --group my-share-group --stateReset Share Group Offsets
Section titled “Reset Share Group Offsets”# Reset to latest (dry run)kafka-share-groups.sh --bootstrap-server kafka:9092 \ --reset-offsets --group my-share-group \ --topic my-topic --to-latest --dry-run
# Reset to latest (execute)kafka-share-groups.sh --bootstrap-server kafka:9092 \ --reset-offsets --group my-share-group \ --topic my-topic --to-latest --executeDelete Share Group
Section titled “Delete Share Group”kafka-share-groups.sh --bootstrap-server kafka:9092 \ --delete --group my-share-groupkafka-groups.sh
Section titled “kafka-groups.sh”List all group types (consumer, share, streams).
# List all groups with typekafka-groups.sh --bootstrap-server kafka:9092 --list
# Output shows group type# GROUP TYPE PROTOCOL# my-consumer-group Consumer consumer# my-share-group Share sharePartition Reassignment with Throttling
Section titled “Partition Reassignment with Throttling”Limit bandwidth during data migration to reduce impact on production traffic.
Execute with Throttle
Section titled “Execute with Throttle”# Throttle inter-broker replication to 50 MB/skafka-reassign-partitions.sh --bootstrap-server kafka:9092 \ --reassignment-json-file reassignment.json \ --throttle 50000000 \ --execute
# Also throttle disk-to-disk moveskafka-reassign-partitions.sh --bootstrap-server kafka:9092 \ --reassignment-json-file reassignment.json \ --throttle 50000000 \ --replica-alter-log-dirs-throttle 100000000 \ --executeAdjust Throttle During Migration
Section titled “Adjust Throttle During Migration”# Increase throttle if migration is too slowkafka-reassign-partitions.sh --bootstrap-server kafka:9092 \ --reassignment-json-file reassignment.json \ --throttle 100000000 \ --additional \ --executeVerify and Remove Throttle
Section titled “Verify and Remove Throttle”# Verify completion - this also removes throttlekafka-reassign-partitions.sh --bootstrap-server kafka:9092 \ --reassignment-json-file reassignment.json \ --verifyThrottle Removal
Always run --verify after reassignment completes. Failing to remove the throttle can limit normal replication traffic.
View Current Throttle Settings
Section titled “View Current Throttle Settings”# View broker throttlekafka-configs.sh --bootstrap-server kafka:9092 \ --entity-type brokers --describe
# View topic throttlekafka-configs.sh --bootstrap-server kafka:9092 \ --entity-type topics --entity-name my-topic --describeIncreasing Replication Factor
Section titled “Increasing Replication Factor”Create Reassignment Plan
Section titled “Create Reassignment Plan”cat > increase-rf.json << 'EOF'{ "version": 1, "partitions": [ {"topic": "my-topic", "partition": 0, "replicas": [1, 2, 3]} ]}EOFExecute and Verify
Section titled “Execute and Verify”# Executekafka-reassign-partitions.sh --bootstrap-server kafka:9092 \ --reassignment-json-file increase-rf.json \ --execute
# Verifykafka-reassign-partitions.sh --bootstrap-server kafka:9092 \ --reassignment-json-file increase-rf.json \ --verify
# Confirm new replication factorkafka-topics.sh --bootstrap-server kafka:9092 \ --topic my-topic --describeQuota Management
Section titled “Quota Management”Set User Quotas
Section titled “Set User Quotas”# Set producer and consumer byte rate for userkafka-configs.sh --bootstrap-server kafka:9092 \ --entity-type users --entity-name my-user \ --alter --add-config 'producer_byte_rate=1048576,consumer_byte_rate=2097152'
# Set request rate quota (percentage of broker capacity)kafka-configs.sh --bootstrap-server kafka:9092 \ --entity-type users --entity-name my-user \ --alter --add-config 'request_percentage=50'Set Client-ID Quotas
Section titled “Set Client-ID Quotas”kafka-configs.sh --bootstrap-server kafka:9092 \ --entity-type clients --entity-name my-client \ --alter --add-config 'producer_byte_rate=1048576,consumer_byte_rate=2097152'Set Combined User+Client Quotas
Section titled “Set Combined User+Client Quotas”kafka-configs.sh --bootstrap-server kafka:9092 \ --entity-type users --entity-name my-user \ --entity-type clients --entity-name my-client \ --alter --add-config 'producer_byte_rate=1048576'Set Default Quotas
Section titled “Set Default Quotas”# Default for all userskafka-configs.sh --bootstrap-server kafka:9092 \ --entity-type users --entity-default \ --alter --add-config 'producer_byte_rate=1048576'
# Default for all clientskafka-configs.sh --bootstrap-server kafka:9092 \ --entity-type clients --entity-default \ --alter --add-config 'producer_byte_rate=1048576'Describe Quotas
Section titled “Describe Quotas”# Describe user quotakafka-configs.sh --bootstrap-server kafka:9092 \ --entity-type users --entity-name my-user --describe
# Describe all user quotaskafka-configs.sh --bootstrap-server kafka:9092 \ --entity-type users --describe
# Describe combined user+client quotaskafka-configs.sh --bootstrap-server kafka:9092 \ --entity-type users --entity-type clients --describeCluster Operations
Section titled “Cluster Operations”Graceful Shutdown
Section titled “Graceful Shutdown”Enable controlled shutdown for graceful broker restarts:
controlled.shutdown.enable=trueWith controlled shutdown enabled, the broker:
- Syncs all logs to disk (avoiding recovery on restart)
- Migrates leadership to other replicas before stopping
- Minimizes partition unavailability to milliseconds
Preferred Leader Election
Section titled “Preferred Leader Election”# Trigger preferred leader election for all partitionskafka-leader-election.sh --bootstrap-server kafka:9092 \ --election-type preferred \ --all-topic-partitions
# Auto-rebalance can also be enabled in broker config# auto.leader.rebalance.enable=trueRack Awareness
Section titled “Rack Awareness”Configure brokers for rack-aware replica placement:
# server.properties on each brokerbroker.rack=rack-1When rack awareness is configured:
- Replicas of each partition spread across different racks
- Partition spans
min(#racks, replication-factor)racks - Survives rack-level failures
Broker API Versions
Section titled “Broker API Versions”Check supported API versions for compatibility:
kafka-broker-api-versions.sh --bootstrap-server kafka:9092Log Verification
Section titled “Log Verification”Verify Log Integrity
Section titled “Verify Log Integrity”# Verify indexeskafka-dump-log.sh --files /var/kafka-logs/my-topic-0/00000000000000000000.log \ --index-sanity-check
# Dump transaction statekafka-dump-log.sh --files /var/kafka-logs/__transaction_state-0/00000000000000000000.log \ --print-data-logOffset Explorer
Section titled “Offset Explorer”# Get offsets for all partitionskafka-get-offsets.sh --bootstrap-server kafka:9092 \ --topic my-topic
# Get earliest and latest offsetskafka-get-offsets.sh --bootstrap-server kafka:9092 \ --topic my-topic --time -2 # earliest
kafka-get-offsets.sh --bootstrap-server kafka:9092 \ --topic my-topic --time -1 # latestKafka 4.2 CLI Changes
Section titled “Kafka 4.2 CLI Changes”Standardized CLI Arguments (KIP-1147)
Kafka 4.2 standardizes command-line arguments across all tools. --bootstrap-server and --command-config are now the canonical options for all commands. Legacy argument variants (e.g., --broker-list, --new-consumer) are deprecated and will be removed in a future release.
Additional CLI enhancements in Kafka 4.2:
| Feature | KIP | Description |
|---|---|---|
--warmup-records | KIP-1052 | Optional warmup argument for kafka-producer-perf-test.sh to exclude initial records from performance measurements |
--node-id for describeFeatures | KIP-1160 | Query supported features from a specific broker node, useful for diagnosing version inconsistencies across a cluster |
--include for ConsumerPerformance | KIP-1192 | Regex-based topic filtering in kafka-consumer-perf-test.sh |
| EndToEndLatency improvements | KIP-1172 | Named-argument parsing with key and header support in kafka-e2e-latency.sh |
Related Documentation
Section titled “Related Documentation”- Operations - Operations guide
- Configuration - Configuration reference
- Monitoring - Monitoring guide