Kafka Cluster Management
Operational procedures for managing Apache Kafka clusters.
Cluster Operations Overview
Section titled “Cluster Operations Overview”Adding Brokers
Section titled “Adding Brokers”Pre-Addition Checklist
Section titled “Pre-Addition Checklist”- New broker has same Kafka version
- Network connectivity to all existing brokers
- Sufficient disk space
- Proper configuration (broker.id, listeners, etc.)
Addition Process
Section titled “Addition Process”- Configure the new broker
broker.id=4 # Unique IDnode.id=4 # KRaft mode
# Controller connectioncontroller.quorum.voters=1@ctrl1:9093,2@ctrl2:9093,3@ctrl3:9093
# Listenerslisteners=PLAINTEXT://0.0.0.0:9092advertised.listeners=PLAINTEXT://broker4:9092
# Log directorieslog.dirs=/var/kafka-logs
# Rack (if applicable)broker.rack=rack-2- Start the broker
# Format storage (KRaft mode, first time only)kafka-storage.sh format -t <cluster-id> -c /etc/kafka/server.properties
# Start brokerkafka-server-start.sh /etc/kafka/server.properties- Verify registration
# Check broker is registeredkafka-broker-api-versions.sh --bootstrap-server broker4:9092
# Check cluster membershipkafka-metadata.sh --snapshot /var/kafka-logs/__cluster_metadata-0/*.log \ --command "brokers"- Reassign partitions to new broker
# Generate reassignment plan including new brokercat > topics.json << 'EOF'{ "topics": [{"topic": "topic1"}, {"topic": "topic2"}], "version": 1}EOF
kafka-reassign-partitions.sh --bootstrap-server kafka:9092 \ --topics-to-move-json-file topics.json \ --broker-list "1,2,3,4" \ --generate
# Execute reassignmentkafka-reassign-partitions.sh --bootstrap-server kafka:9092 \ --reassignment-json-file reassignment.json \ --throttle 100000000 \ --executeRemoving Brokers
Section titled “Removing Brokers”Pre-Removal Checklist
Section titled “Pre-Removal Checklist”- All partitions have replicas on other brokers
- Reassignment completed successfully
- No under-replicated partitions
Decommission Process
Section titled “Decommission Process”- Move all partitions off the broker
# Generate plan without the broker to remove (broker 4)kafka-reassign-partitions.sh --bootstrap-server kafka:9092 \ --topics-to-move-json-file topics.json \ --broker-list "1,2,3" \ --generate
# Executekafka-reassign-partitions.sh --bootstrap-server kafka:9092 \ --reassignment-json-file reassignment.json \ --throttle 100000000 \ --execute
# Verify completionkafka-reassign-partitions.sh --bootstrap-server kafka:9092 \ --reassignment-json-file reassignment.json \ --verify- Verify no partitions remain
# Check broker has no partitionskafka-log-dirs.sh --bootstrap-server kafka:9092 \ --describe --broker-list 4- Graceful shutdown
# Stop broker gracefully (controlled shutdown)kafka-server-stop.sh
# Orkill -TERM <kafka-pid>- Verify removal
# Check broker is no longer registeredkafka-metadata.sh --snapshot /var/kafka-logs/__cluster_metadata-0/*.log \ --command "brokers"Rolling Upgrades
Section titled “Rolling Upgrades”Upgrade Process
Section titled “Upgrade Process”Step-by-Step
Section titled “Step-by-Step”- Prepare for upgrade
# Before upgrade, set in server.propertiesinter.broker.protocol.version=3.6log.message.format.version=3.6
# Note: in Kafka 4.x, inter.broker.protocol.version and log.message.format.version are removed.- Upgrade each broker
#!/bin/bashBROKER=$1
echo "Upgrading $BROKER..."
# Stop brokerssh $BROKER "sudo systemctl stop kafka"
# Install new versionssh $BROKER "sudo yum install kafka-3.7.0" # Or appropriate command
# Start brokerssh $BROKER "sudo systemctl start kafka"
# Wait for recoverysleep 30
# Verify healthkafka-broker-api-versions.sh --bootstrap-server $BROKER:9092
# Check under-replicated partitionskafka-topics.sh --bootstrap-server $BROKER:9092 \ --describe --under-replicated-partitions
echo "Waiting for ISR to recover..."while true; do URP=$(kafka-topics.sh --bootstrap-server $BROKER:9092 \ --describe --under-replicated-partitions 2>/dev/null | wc -l) if [ "$URP" -eq 0 ]; then echo "ISR recovered" break fi sleep 10done- After all brokers upgraded
# Update protocol versioninter.broker.protocol.version=3.7log.message.format.version=3.7- Rolling restart to apply
for broker in broker1 broker2 broker3; do ./upgrade-broker.sh $brokerdonePartition Reassignment
Section titled “Partition Reassignment”Generate Reassignment Plan
Section titled “Generate Reassignment Plan”# Create topics filecat > topics.json << 'EOF'{ "topics": [ {"topic": "orders"}, {"topic": "events"} ], "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" \ --generateCustom Reassignment
Section titled “Custom Reassignment”{ "version": 1, "partitions": [ {"topic": "orders", "partition": 0, "replicas": [1,2,3]}, {"topic": "orders", "partition": 1, "replicas": [2,3,4]}, {"topic": "orders", "partition": 2, "replicas": [3,4,1]} ]}Execute with Throttling
Section titled “Execute with Throttling”# Start reassignment with throttle (100 MB/s)kafka-reassign-partitions.sh --bootstrap-server kafka:9092 \ --reassignment-json-file reassignment.json \ --throttle 100000000 \ --execute
# Monitor progresskafka-reassign-partitions.sh --bootstrap-server kafka:9092 \ --reassignment-json-file reassignment.json \ --verify
# Adjust throttle if neededkafka-reassign-partitions.sh --bootstrap-server kafka:9092 \ --reassignment-json-file reassignment.json \ --throttle 200000000 \ --execute
# Remove throttle after completionkafka-reassign-partitions.sh --bootstrap-server kafka:9092 \ --reassignment-json-file reassignment.json \ --verifyLeader Election
Section titled “Leader Election”Preferred Leader Election
Section titled “Preferred Leader Election”# All partitionskafka-leader-election.sh --bootstrap-server kafka:9092 \ --election-type preferred \ --all-topic-partitions
# Specific topickafka-leader-election.sh --bootstrap-server kafka:9092 \ --election-type preferred \ --topic my-topic
# Specific partitionkafka-leader-election.sh --bootstrap-server kafka:9092 \ --election-type preferred \ --topic my-topic \ --partition 0Unclean Leader Election
Section titled “Unclean Leader Election”Data Loss Risk
Unclean leader election allows out-of-sync replicas to become leader, potentially causing data loss.
kafka-leader-election.sh --bootstrap-server kafka:9092 \ --election-type unclean \ --topic my-topic \ --partition 0Increasing Partitions
Section titled “Increasing Partitions”# Increase partition count (cannot decrease)kafka-topics.sh --bootstrap-server kafka:9092 \ --alter \ --topic my-topic \ --partitions 24Key-Based Ordering
Increasing partitions changes key-to-partition mapping. Existing keys may route to different partitions.
Topic Management
Section titled “Topic Management”Create Topic
Section titled “Create Topic”kafka-topics.sh --bootstrap-server kafka:9092 \ --create \ --topic new-topic \ --partitions 12 \ --replication-factor 3 \ --config retention.ms=604800000 \ --config cleanup.policy=deleteDelete Topic
Section titled “Delete Topic”# Delete topic (data is permanently removed)kafka-topics.sh --bootstrap-server kafka:9092 \ --delete \ --topic old-topicModify Configuration
Section titled “Modify Configuration”# Add/update configkafka-configs.sh --bootstrap-server kafka:9092 \ --entity-type topics \ --entity-name my-topic \ --alter \ --add-config retention.ms=86400000
# Remove config (revert to default)kafka-configs.sh --bootstrap-server kafka:9092 \ --entity-type topics \ --entity-name my-topic \ --alter \ --delete-config retention.msHealth Verification
Section titled “Health Verification”Post-Operation Checks
Section titled “Post-Operation Checks”#!/bin/bashBOOTSTRAP="kafka:9092"
echo "=== Cluster Health Check ==="
# Check broker connectivityecho "Checking broker connectivity..."kafka-broker-api-versions.sh --bootstrap-server $BOOTSTRAP > /dev/null 2>&1if [ $? -eq 0 ]; then echo "✓ Broker connectivity OK"else echo "✗ Broker connectivity FAILED" exit 1fi
# Check offline partitionsOFFLINE=$(kafka-topics.sh --bootstrap-server $BOOTSTRAP \ --describe --unavailable-partitions 2>/dev/null | grep -c "Topic:")echo "Offline partitions: $OFFLINE"if [ "$OFFLINE" -gt 0 ]; then echo "✗ CRITICAL: Offline partitions detected" exit 2fi
# Check under-replicated partitionsURP=$(kafka-topics.sh --bootstrap-server $BOOTSTRAP \ --describe --under-replicated-partitions 2>/dev/null | grep -c "Topic:")echo "Under-replicated partitions: $URP"if [ "$URP" -gt 0 ]; then echo "⚠ WARNING: Under-replicated partitions"fi
echo "=== Health Check Complete ==="Related Documentation
Section titled “Related Documentation”- Operations Overview - Operations guide
- Monitoring - Metrics and alerting
- Backup and Restore - DR procedures
- Maintenance - Routine maintenance