Unlock Kafka's True Potential: Master the Zookeeper-Free Revolution, Step By Step
Discover how to deploy and manage Apache Kafka without Zookeeper, leveraging the powerful KRaft metadata mode. This guide provides a step-by-step walkthrough to simplify your Kafka operations and enhance performance.
Introduction: The Dawn of Zookeeper-Less Kafka
For years, Apache Kafka, the distributed streaming platform, relied heavily on Apache Zookeeper for its metadata management, leader election, and cluster coordination. While Zookeeper served its purpose, it introduced an additional dependency, increasing operational complexity and potential points of failure. The advent of Kafka Raft (KRaft) metadata mode marks a significant evolution, allowing Kafka to manage its own metadata internally, effectively eliminating the need for Zookeeper. This post will guide you through setting up and running Kafka in KRaft mode, providing a comprehensive, step-by-step approach to embrace this simplified and more robust architecture.
Why Kafka Without Zookeeper (KRaft)?
Migrating to KRaft mode offers several compelling advantages:
- Simplified Architecture: Removing Zookeeper means one less distributed system to manage, monitor, and troubleshoot. This significantly reduces operational overhead.
- Improved Scalability: KRaft allows for a much larger number of partitions and faster metadata operations compared to Zookeeper-based Kafka.
- Faster Controller Failovers: The KRaft controller election process is significantly faster than Zookeeper's, leading to quicker recovery times during outages.
- Unified System: Kafka becomes a self-contained system, managing its own metadata directly using the Raft consensus algorithm.
- Enhanced Security: Reduced attack surface by consolidating components.
Key Components of KRaft Architecture
Understanding the core roles within a KRaft cluster is essential:
- Controller Role: In KRaft mode, Kafka brokers can assume the controller role, responsible for managing the cluster's metadata, including topics, partitions, and leader elections. This role directly replaces Zookeeper's previous function.
- Broker Role: These are the workhorse nodes that handle message production and consumption, storing partition data.
- Combined Role: For simpler deployments (e.g., development, single-node setups), a single Kafka instance can act as both a broker and a controller. This is often referred to as a "combined" node.
- Metadata Log: Instead of Zookeeper storing metadata, KRaft brokers maintain a Raft-based metadata log. This log contains all cluster state changes and is replicated among the controller quorum for fault tolerance.
Step-by-Step Guide: Setting Up Kafka in KRaft Mode
Let's dive into setting up a single-node Kafka cluster using KRaft. This configuration will feature a combined broker and controller role.
Prerequisites
- Java 11 or higher installed.
- Apache Kafka downloaded from the official website (ensure it's a version that supports KRaft, e.g., 2.8.0 or newer).
Step 1: Download Apache Kafka
If you haven't already, download the latest stable release of Apache Kafka. For this guide, we assume you've extracted it to a directory, let's call it `KAFKA_HOME`.
wget https://downloads.apache.org/kafka/[YOUR_KAFKA_VERSION]/kafka_2.13-[YOUR_KAFKA_VERSION].tgz
tar -xzf kafka_2.13-[YOUR_KAFKA_VERSION].tgz
cd kafka_2.13-[YOUR_KAFKA_VERSION]
Step 2: Generate a Cluster ID
Every KRaft cluster requires a unique identifier. This ID is used by all nodes in the cluster to identify themselves as part of the same Kafka deployment. You only need to generate this once per cluster.
bin/kafka-storage.sh random-uuid
This command will output a UUID, e.g., 8d0e7a4f-5b1c-4e8d-8a0f-1a2b3c4d5e6f. Copy this ID, as you'll need it in the next step.
Step 3: Configure Kafka for KRaft
Navigate to the config/kraft directory. You'll find example configuration files. We'll modify server.properties (or server-kraft.properties in some distributions) to enable KRaft mode and configure our combined node. Open your chosen server.properties file and make the following changes:
# Basic node configuration
node.id=1 # Unique ID for this node within the cluster
process.roles=broker,controller # This node acts as both broker and controller
cluster.id=YOUR_GENERATED_CLUSTER_ID # Replace with the ID from Step 2
# Listener configuration
listeners=PLAINTEXT://localhost:9092
advertised.listeners=PLAINTEXT://localhost:9092
# Storage paths
log.dirs=/tmp/kraft-kafka-logs # Directory for message data
metadata.log.dir=/tmp/kraft-kafka-metadata # Directory for KRaft metadata log
# Other common settings (optional, adjust as needed)
num.partitions=1
default.replication.factor=1
min.insync.replicas=1
# Remove or comment out Zookeeper-related configurations if they exist
# zookeeper.connect=localhost:2181
# zookeeper.connection.timeout.ms=18000
Explanation of Key Properties:
node.id: A unique integer identifier for this Kafka node.process.roles: Specifies the roles this Kafka instance will play. For a single-node setup,broker,controlleris used. For a multi-node setup, you might have dedicatedcontrollernodes andbrokernodes.cluster.id: The UUID generated in Step 2. All nodes in the same cluster must share this ID.listeners&advertised.listeners: Configure how clients and other brokers connect to this node.log.dirs: The directory where Kafka stores message data.metadata.log.dir: The dedicated directory for the KRaft metadata log. This should ideally be separate fromlog.dirsfor better performance and manageability.
Step 4: Format the Storage Directories
Before starting Kafka, you need to format the storage directories defined in your server.properties with the cluster ID. This initializes the metadata log.
bin/kafka-storage.sh format -t YOUR_GENERATED_CLUSTER_ID -c config/kraft/server.properties
Replace YOUR_GENERATED_CLUSTER_ID with the actual UUID. This command will prepare the directories for Kafka's data and metadata.
Step 5: Start the Kafka Broker
Now you can start your Zookeeper-less Kafka broker:
bin/kafka-server-start.sh config/kraft/server.properties
You should see output indicating that Kafka is starting up and serving as both a broker and a controller. Look for messages about "KraftController" and "KafkaServer".
Step 6: Verify Operation
Once Kafka is running, let's verify its functionality by creating a topic, producing messages, and consuming them.
Create a Topic
Open a new terminal and run:
bin/kafka-topics.sh --create --topic my-kraft-topic --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1
You should see confirmation that the topic was created.
Produce Messages
Open another terminal and use the console producer:
bin/kafka-console-producer.sh --topic my-kraft-topic --bootstrap-server localhost:9092
Type some messages and press Enter. For example:
Hello from KRaft Kafka!
This is a test message.
Kafka without Zookeeper is awesome!
Consume Messages
In a third terminal, start the console consumer to read the messages:
bin/kafka-console-consumer.sh --topic my-kraft-topic --from-beginning --bootstrap-server localhost:9092
You should see the messages you produced appear in this terminal.
Deployment Considerations
While this guide focuses on a single-node combined setup for simplicity, production deployments typically involve a multi-node cluster with a dedicated controller quorum. For example, three controller nodes (configured with process.roles=controller) would manage the metadata, while separate broker nodes (configured with process.roles=broker) handle data.
The controller.quorum.voters property would be used in the server.properties of all controller and broker nodes to list the node.id:host:port of each controller in the quorum. For instance:
controller.quorum.voters=1@controller1.example.com:9093,2@controller2.example.com:9093,3@controller3.example.com:9093
This ensures high availability for the metadata management layer.
Conclusion
By following this guide, you’ve successfully set up and verified a Zookeeper-free Apache Kafka cluster using the KRaft metadata mode. This fundamental shift simplifies your Kafka architecture, reduces operational overhead, and paves the way for a more scalable and resilient streaming platform. Happy coding!
Show your love, follow us javaoneworld





