Install Kafka: KRaft Mode, Docker and Your First Broker
What You Need
| Item | Advice |
|---|---|
| Java | Install a recent JDK. Brokers in Kafka 4.x need Java 17 or newer, and the Java clients need Java 11 or newer (check the documentation of your exact version) |
| Operating system | Linux or macOS. On Windows use WSL2 (Ubuntu) or Docker |
| Disk and RAM | 2 GB RAM and a few GB of disk are enough for learning |
| Ports | 9092 for clients, 9093 for the controller |
Option 1: Docker (Fastest)
bash
docker run -d --name kafka -p 9092:9092 apache/kafka:latest
docker logs -f kafka # wait for "Kafka Server started"
# open a shell inside the container to use the Kafka tools
docker exec -it kafka bash
cd /opt/kafka/bin
./kafka-topics.sh --bootstrap-server localhost:9092 --list
Option 2: Download and Run
bash
# 1. check Java
java -version
# 2. download and unpack the binary release from the Apache Kafka downloads page (choose the Scala 2.13 build)
tar -xzf kafka_2.13-4.x.y.tgz
cd kafka_2.13-4.x.y
# 3. create a cluster id and format the storage (one time)
KAFKA_CLUSTER_ID="$(bin/kafka-storage.sh random-uuid)"
bin/kafka-storage.sh format --standalone -t $KAFKA_CLUSTER_ID -c config/server.properties
# 4. start the broker
bin/kafka-server-start.sh config/server.properties
What does
format do? In KRaft mode each node has a metadata log on disk. The format command prepares the log directory and stamps it with the cluster id. Without it the broker refuses to start. The --standalone option creates a single-node controller quorum for development.
Leave this terminal running. Open a second terminal and test:
bash
bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic hello --partitions 1 --replication-factor 1
bin/kafka-topics.sh --bootstrap-server localhost:9092 --list
# hello
Docker Compose File
yaml
services:
kafka:
image: apache/kafka:latest
container_name: kafka
ports:
- "9092:9092"
environment:
KAFKA_NODE_ID: 1
KAFKA_PROCESS_ROLES: broker,controller
KAFKA_LISTENERS: PLAINTEXT://:9092,CONTROLLER://:9093
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
KAFKA_CONTROLLER_QUORUM_VOTERS: 1@localhost:9093
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
volumes:
- kafka-data:/var/lib/kafka/data
volumes:
kafka-data:
Important Settings in server.properties
text
# who am I and what do I do
node.id=1
process.roles=broker,controller # combined mode (fine for development). Production: separate controller and broker nodes
controller.quorum.voters=1@localhost:9093
# network
listeners=PLAINTEXT://:9092,CONTROLLER://:9093
advertised.listeners=PLAINTEXT://localhost:9092 # the address CLIENTS use to reach this broker
controller.listener.names=CONTROLLER
inter.broker.listener.name=PLAINTEXT
# storage
log.dirs=/var/lib/kafka/data # NOT /tmp for anything you want to keep
num.partitions=3 # default partitions for auto-created topics
log.retention.hours=168 # keep data 7 days
| Setting | Meaning |
|---|---|
node.id | Unique number of this node in the cluster |
process.roles | broker, controller or both |
listeners | Addresses the server binds to |
advertised.listeners | Addresses the server tells clients to use. The most common source of connection problems |
log.dirs | Where the data is stored |
log.retention.hours / log.retention.bytes | How long or how much data to keep |
Do not leave data in /tmp: The default sample configuration may store data in a temporary folder that is cleaned on reboot. Set
log.dirs to a permanent path for anything you want to keep.
Folder Layout
text
kafka_2.13-4.x.y/
bin/ kafka-*.sh scripts (kafka-topics.sh, kafka-console-producer.sh, ...)
config/ server.properties (broker/controller), producer.properties, consumer.properties, connect-*.properties
libs/ Java libraries
logs/ server logs
/var/lib/kafka/data/ topic data (one folder per partition, like orders-0) and the metadata log
Common Startup Errors
| Error | Cause and fix |
|---|---|
No readable meta.properties files found | Storage not formatted. Run kafka-storage.sh format |
Cluster ID mismatch | The data folder was formatted for another cluster. Use a clean folder or the original id |
Address already in use | Another process uses 9092 or 9093. Stop it or change the port |
UnsupportedClassVersionError | Java is too old. Install the Java version required by your Kafka version |
| Clients connect but time out afterwards | Wrong advertised.listeners (for example localhost inside Docker). Use an address the client can reach |
OutOfMemoryError on startup | Set KAFKA_HEAP_OPTS="-Xmx1G -Xms1G" before starting |
Conclusion
Kafka 4.x runs in KRaft mode: generate a cluster id, format the storage once and start the broker. Docker is the fastest way to start. Remember advertised.listeners, which tells clients where to connect.
Quick Summary: format storage once; start broker; advertised.listeners = client address; keep data out of /tmp.

