Kafka Diagnosis
Diagnostic procedures for identifying and isolating Apache Kafka issues.
Diagnostic Workflow
Section titled “Diagnostic Workflow”Connectivity Checks
Section titled “Connectivity Checks”Basic Connectivity
Section titled “Basic Connectivity”# Test TCP connectivitync -zv kafka-host 9092
# Test with timeouttimeout 5 bash -c 'cat < /dev/null > /dev/tcp/kafka-host/9092' && echo "OK" || echo "FAILED"
# DNS resolutiondig kafka-hostnslookup kafka-hostBroker API Versions
Section titled “Broker API Versions”# List supported API versions (verifies connectivity and protocol)kafka-broker-api-versions.sh --bootstrap-server kafka:9092
# Output shows:# ApiVersion(apiKey=0, minVersion=0, maxVersion=12) -- Produce# ApiVersion(apiKey=1, minVersion=0, maxVersion=13) -- Fetch# ...SSL/TLS Connectivity
Section titled “SSL/TLS Connectivity”# Test SSL handshakeopenssl s_client -connect kafka:9093 -CAfile ca-cert.pem
# Verify certificateopenssl s_client -connect kafka:9093 -showcerts
# Check certificate expiryopenssl s_client -connect kafka:9093 2>/dev/null | \ openssl x509 -noout -datesSASL Authentication
Section titled “SASL Authentication”# Test with SASL/PLAINkafka-broker-api-versions.sh --bootstrap-server kafka:9093 \ --command-config client.properties
# client.properties:# security.protocol=SASL_SSL# sasl.mechanism=PLAIN# sasl.jaas.config=...Cluster State Verification
Section titled “Cluster State Verification”Controller Status
Section titled “Controller Status”# KRaft mode - check controller quorumkafka-metadata.sh --snapshot /var/kafka-logs/__cluster_metadata-0/*.log \ --command "quorum"
# Check active controllerkafka-metadata.sh --snapshot /var/kafka-logs/__cluster_metadata-0/*.log \ --command "describe" | grep -i controllerBroker Status
Section titled “Broker Status”# List all brokerskafka-metadata.sh --snapshot /var/kafka-logs/__cluster_metadata-0/*.log \ --command "brokers"
# Check each brokerfor broker in kafka1 kafka2 kafka3; do echo -n "$broker: " timeout 5 kafka-broker-api-versions.sh --bootstrap-server $broker:9092 \ > /dev/null 2>&1 && echo "OK" || echo "FAILED"donePartition Status
Section titled “Partition Status”# Under-replicated partitionskafka-topics.sh --bootstrap-server kafka:9092 \ --describe --under-replicated-partitions
# Unavailable partitionskafka-topics.sh --bootstrap-server kafka:9092 \ --describe --unavailable-partitions
# Under min-ISR partitionskafka-topics.sh --bootstrap-server kafka:9092 \ --describe --under-min-isr-partitions
# Describe specific topickafka-topics.sh --bootstrap-server kafka:9092 \ --describe --topic my-topicISR Verification
Section titled “ISR Verification”# Check ISR for all partitionskafka-topics.sh --bootstrap-server kafka:9092 --describe | \ grep -E "Topic:|Leader:|Isr:"
# Monitor ISR changeswatch -n 5 'kafka-topics.sh --bootstrap-server kafka:9092 \ --describe --under-replicated-partitions'Data Flow Testing
Section titled “Data Flow Testing”Producer Test
Section titled “Producer Test”# Send test messagesecho "test-message-$(date +%s)" | kafka-console-producer.sh \ --bootstrap-server kafka:9092 \ --topic test-topic
# Send with keykafka-console-producer.sh --bootstrap-server kafka:9092 \ --topic test-topic \ --property "parse.key=true" \ --property "key.separator=:" << EOFkey1:value1key2:value2EOF
# Performance test producerkafka-producer-perf-test.sh \ --topic test-topic \ --num-records 10000 \ --record-size 1024 \ --throughput -1 \ --producer-props bootstrap.servers=kafka:9092Consumer Test
Section titled “Consumer Test”# Consume from beginningkafka-console-consumer.sh --bootstrap-server kafka:9092 \ --topic test-topic \ --from-beginning \ --max-messages 10
# Consume with timestampskafka-console-consumer.sh --bootstrap-server kafka:9092 \ --topic test-topic \ --from-beginning \ --property print.timestamp=true \ --property print.key=true
# Performance test consumerkafka-consumer-perf-test.sh \ --bootstrap-server kafka:9092 \ --topic test-topic \ --messages 10000 \ --threads 1End-to-End Latency Test
Section titled “End-to-End Latency Test”# Measure end-to-end latencykafka-run-class.sh kafka.tools.EndToEndLatency \ kafka:9092 \ test-topic \ 10000 \ all \ 1024Replication Verification
Section titled “Replication Verification”# Verify replica lagkafka-replica-verification.sh \ --broker-list kafka1:9092,kafka2:9092,kafka3:9092 \ --topic-white-list ".*"
# Check log end offsetskafka-run-class.sh kafka.tools.GetOffsetShell \ --bootstrap-server kafka:9092 \ --topic my-topicConsumer Group Diagnosis
Section titled “Consumer Group Diagnosis”Group State
Section titled “Group State”# List all groupskafka-consumer-groups.sh --bootstrap-server kafka:9092 --list
# Group detailskafka-consumer-groups.sh --bootstrap-server kafka:9092 \ --describe --group my-group
# Group statekafka-consumer-groups.sh --bootstrap-server kafka:9092 \ --describe --group my-group --state
# Member detailskafka-consumer-groups.sh --bootstrap-server kafka:9092 \ --describe --group my-group --members --verboseConsumer Lag Analysis
Section titled “Consumer Lag Analysis”# Lag per partitionkafka-consumer-groups.sh --bootstrap-server kafka:9092 \ --describe --group my-group
# Sample output:# GROUP TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG# my-group my-topic 0 1000 1050 50# my-group my-topic 1 2000 2000 0Lag Interpretation
Section titled “Lag Interpretation”| Lag Behavior | Meaning | Action |
|---|---|---|
| Lag = 0 | Consumer caught up | Normal |
| Lag constant | Keeping pace | Normal |
| Lag growing | Falling behind | Scale consumers |
| Lag very large | Significantly behind | Investigate, reset offset |
Performance Diagnosis
Section titled “Performance Diagnosis”JMX Metrics Collection
Section titled “JMX Metrics Collection”# Enable JMXexport KAFKA_JMX_OPTS="-Dcom.sun.management.jmxremote \ -Dcom.sun.management.jmxremote.port=9999 \ -Dcom.sun.management.jmxremote.authenticate=false \ -Dcom.sun.management.jmxremote.ssl=false"
# Query metrics with JMX toolkafka-run-class.sh kafka.tools.JmxTool \ --jmx-url service:jmx:rmi:///jndi/rmi://localhost:9999/jmxrmi \ --object-name kafka.server:type=BrokerTopicMetrics,name=MessagesInPerSecKey Metrics to Check
Section titled “Key Metrics to Check”| Metric | Description | Alert Threshold |
|---|---|---|
UnderReplicatedPartitions | Partitions with missing replicas | > 0 |
OfflinePartitionsCount | Partitions with no leader | > 0 |
ActiveControllerCount | Controllers in cluster | ≠ 1 |
RequestQueueSize | Queued requests | Growing |
TotalTimeMs | Request latency | P99 > 100ms |
System Resource Checks
Section titled “System Resource Checks”# CPU usagetop -b -n 1 | head -20
# Memory usagefree -mvmstat 1 5
# Disk I/Oiostat -x 1 5
# Networknetstat -s | grep -i errorss -sDisk Space
Section titled “Disk Space”# Log directorieskafka-log-dirs.sh --bootstrap-server kafka:9092 \ --describe --broker-list 0,1,2
# Filesystem usagedf -h /var/kafka-logs
# Directory sizesdu -sh /var/kafka-logs/*Log Segment Verification
Section titled “Log Segment Verification”Check Log Integrity
Section titled “Check Log Integrity”# Verify log segmentkafka-dump-log.sh \ --files /var/kafka-logs/my-topic-0/00000000000000000000.log \ --verify-index-only
# Deep verificationkafka-dump-log.sh \ --files /var/kafka-logs/my-topic-0/00000000000000000000.log \ --deep-iteration
# Index sanity checkkafka-dump-log.sh \ --files /var/kafka-logs/my-topic-0/00000000000000000000.log \ --index-sanity-checkView Log Contents
Section titled “View Log Contents”# Print log entrieskafka-dump-log.sh \ --files /var/kafka-logs/my-topic-0/00000000000000000000.log \ --print-data-log
# Print specific offset rangekafka-dump-log.sh \ --files /var/kafka-logs/my-topic-0/00000000000000000000.log \ --print-data-log \ --max-message-size 1000Health Check Script
Section titled “Health Check Script”#!/bin/bashBOOTSTRAP_SERVER=${1:-"localhost:9092"}EXIT_CODE=0
echo "=========================================="echo "Kafka Cluster Health Check"echo "Timestamp: $(date)"echo "Bootstrap: $BOOTSTRAP_SERVER"echo "=========================================="
# Function to check and reportcheck() { local name=$1 local result=$2 local expected=$3
if [ "$result" == "$expected" ]; then echo "[OK] $name: $result" else echo "[FAIL] $name: $result (expected: $expected)" EXIT_CODE=1 fi}
# Broker connectivityecho ""echo "--- Connectivity ---"if kafka-broker-api-versions.sh --bootstrap-server $BOOTSTRAP_SERVER > /dev/null 2>&1; then echo "[OK] Broker connectivity"else echo "[FAIL] Broker connectivity" EXIT_CODE=2 exit $EXIT_CODEfi
# Offline partitionsecho ""echo "--- Partition Health ---"OFFLINE=$(kafka-topics.sh --bootstrap-server $BOOTSTRAP_SERVER \ --describe --unavailable-partitions 2>/dev/null | grep -c "Topic:" || echo "0")if [ "$OFFLINE" -eq 0 ]; then echo "[OK] Offline partitions: 0"else echo "[CRIT] Offline partitions: $OFFLINE" EXIT_CODE=2fi
# Under-replicated partitionsUNDER_REP=$(kafka-topics.sh --bootstrap-server $BOOTSTRAP_SERVER \ --describe --under-replicated-partitions 2>/dev/null | grep -c "Topic:" || echo "0")if [ "$UNDER_REP" -eq 0 ]; then echo "[OK] Under-replicated partitions: 0"else echo "[WARN] Under-replicated partitions: $UNDER_REP" [ $EXIT_CODE -eq 0 ] && EXIT_CODE=1fi
# Under min-ISR partitionsUNDER_ISR=$(kafka-topics.sh --bootstrap-server $BOOTSTRAP_SERVER \ --describe --under-min-isr-partitions 2>/dev/null | grep -c "Topic:" || echo "0")if [ "$UNDER_ISR" -eq 0 ]; then echo "[OK] Under-MinISR partitions: 0"else echo "[WARN] Under-MinISR partitions: $UNDER_ISR" [ $EXIT_CODE -eq 0 ] && EXIT_CODE=1fi
# Consumer groupsecho ""echo "--- Consumer Groups ---"EMPTY_GROUPS=$(kafka-consumer-groups.sh --bootstrap-server $BOOTSTRAP_SERVER \ --list --state | grep -c "Empty" || echo "0")echo "[INFO] Empty consumer groups: $EMPTY_GROUPS"
echo ""echo "=========================================="echo "Health check completed with exit code: $EXIT_CODE"echo "=========================================="
exit $EXIT_CODERelated Documentation
Section titled “Related Documentation”- Troubleshooting Overview - Troubleshooting guide
- Common Errors - Error reference
- Log Analysis - Log interpretation
- CLI Tools - Command reference