Set Up a Multi-Broker Kafka Cluster: Dedicated Controllers, Brokers and Production Layout
Target Layout
| Host | Role | Node id | Ports |
|---|---|---|---|
| ctrl1, ctrl2, ctrl3 | Dedicated controllers | 1, 2, 3 | 9093 (controller) |
| broker1, broker2, broker3 | Brokers | 101, 102, 103 | 9092 (clients) |
For learning you can run controllers and brokers on the same three machines (combined mode). For production, use dedicated controllers with small disks and modest CPU, and brokers with large fast disks.
Hardware and Sizing Guide
| Resource | Broker advice |
|---|---|
| CPU | 8 to 16 cores for busy brokers (TLS and compression use CPU) |
| RAM | 32 to 64 GB. Give the JVM 6 GB heap (Kafka mostly uses the OS page cache, so leave the rest to the operating system) |
| Disk | SSD or NVMe, XFS, one or several volumes. Size with the capacity formula (ingest x retention x replication) |
| Network | 10 GbE for busy clusters. Replication multiplies network traffic by the replication factor |
| Controllers | 4 cores, 8 GB RAM, 50 to 100 GB SSD are usually enough |
Operating System Preparation
bash
# on every node
sudo useradd -r -m -d /opt/kafka kafka
sudo mkdir -p /var/lib/kafka/data /var/lib/kafka/metadata /var/log/kafka
sudo chown -R kafka:kafka /var/lib/kafka /var/log/kafka
# file handles and memory maps (many partitions = many open files)
cat <<EOF | sudo tee /etc/security/limits.d/kafka.conf
kafka soft nofile 128000
kafka hard nofile 128000
kafka soft nproc 64000
kafka hard nproc 64000
EOF
sudo sysctl -w vm.swappiness=1
sudo sysctl -w vm.max_map_count=262144
sudo sysctl -w net.core.somaxconn=4096
# time sync is essential (timestamps, TLS)
sudo systemctl enable --now chronyd
Controller Configuration (ctrl1)
text
# /opt/kafka/config/controller.properties (change node.id and hostname on each controller)
process.roles=controller
node.id=1
controller.quorum.voters=1@ctrl1:9093,2@ctrl2:9093,3@ctrl3:9093
controller.listener.names=CONTROLLER
listeners=CONTROLLER://ctrl1:9093
log.dirs=/var/lib/kafka/metadata
Broker Configuration (broker1)
text
# /opt/kafka/config/broker.properties (change node.id and hostnames on each broker)
process.roles=broker
node.id=101
controller.quorum.voters=1@ctrl1:9093,2@ctrl2:9093,3@ctrl3:9093
controller.listener.names=CONTROLLER
listeners=PLAINTEXT://broker1:9092
advertised.listeners=PLAINTEXT://broker1:9092
inter.broker.listener.name=PLAINTEXT
listener.security.protocol.map=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
log.dirs=/var/lib/kafka/data
broker.rack=zone-a # zone-b on broker2, zone-c on broker3
# safe defaults for new topics
num.partitions=6
default.replication.factor=3
min.insync.replicas=2
offsets.topic.replication.factor=3
transaction.state.log.replication.factor=3
transaction.state.log.min.isr=2
unclean.leader.election.enable=false
auto.create.topics.enable=false # create topics deliberately
log.retention.hours=168
num.network.threads=8
num.io.threads=16
Format and Start
bash
# on ONE machine create the id and share it
bin/kafka-storage.sh random-uuid # e.g. 5L6g3nShT-eMCtK--X86sw
# on every node (controllers use controller.properties, brokers use broker.properties)
sudo -u kafka bin/kafka-storage.sh format -t 5L6g3nShT-eMCtK--X86sw -c config/controller.properties
sudo -u kafka bin/kafka-storage.sh format -t 5L6g3nShT-eMCtK--X86sw -c config/broker.properties
# start controllers first, then brokers
text
# /etc/systemd/system/kafka.service
[Unit]
Description=Apache Kafka
After=network.target
[Service]
User=kafka
Environment="KAFKA_HEAP_OPTS=-Xms6g -Xmx6g"
Environment="KAFKA_JVM_PERFORMANCE_OPTS=-XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35"
ExecStart=/opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/broker.properties
ExecStop=/opt/kafka/bin/kafka-server-stop.sh
LimitNOFILE=128000
Restart=on-failure
TimeoutStopSec=300
[Install]
WantedBy=multi-user.target
bash
sudo systemctl daemon-reload && sudo systemctl enable --now kafka
sudo journalctl -u kafka -f
Verify the Cluster
bash
BS=broker1:9092,broker2:9092,broker3:9092
bin/kafka-metadata-quorum.sh --bootstrap-server $BS describe --status
bin/kafka-broker-api-versions.sh --bootstrap-server $BS | grep -c "(id:" # expect 3 brokers
bin/kafka-topics.sh --bootstrap-server $BS --create --topic test --partitions 6 --replication-factor 3
bin/kafka-topics.sh --bootstrap-server $BS --describe --topic test
text
Topic: test PartitionCount: 6 ReplicationFactor: 3 Configs: min.insync.replicas=2
Partition: 0 Leader: 101 Replicas: 101,102,103 Isr: 101,102,103
Partition: 1 Leader: 102 Replicas: 102,103,101 Isr: 102,103,101
... (leaders spread over all three brokers, all replicas in the ISR)
Failure Test
- Start a producer with
acks=allwriting continuously and a consumer reading the topic - Stop
broker2(systemctl stop kafka) - Observe: leaders move, the producer continues (ISR is 2, which meets
min.insync.replicas=2), consumers continue - Stop
broker3as well: producer now fails withNotEnoughReplicas(this is the safe behaviour) - Start both brokers again and check that the ISR returns to 3 (
--describe)
Practice Cluster with Docker Compose (Combined Mode)
yaml
x-kafka: &kafka
image: apache/kafka:latest
environment: &env
KAFKA_PROCESS_ROLES: broker,controller
KAFKA_CONTROLLER_QUORUM_VOTERS: 1@kafka1:9093,2@kafka2:9093,3@kafka3:9093
KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 3
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 3
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 2
KAFKA_DEFAULT_REPLICATION_FACTOR: 3
KAFKA_MIN_INSYNC_REPLICAS: 2
services:
kafka1:
<<: *kafka
ports: ["9092:9092"]
environment: { <<: *env, KAFKA_NODE_ID: 1, KAFKA_LISTENERS: "PLAINTEXT://:9092,CONTROLLER://:9093", KAFKA_ADVERTISED_LISTENERS: "PLAINTEXT://localhost:9092" }
kafka2:
<<: *kafka
ports: ["9094:9092"]
environment: { <<: *env, KAFKA_NODE_ID: 2, KAFKA_LISTENERS: "PLAINTEXT://:9092,CONTROLLER://:9093", KAFKA_ADVERTISED_LISTENERS: "PLAINTEXT://localhost:9094" }
kafka3:
<<: *kafka
ports: ["9095:9092"]
environment: { <<: *env, KAFKA_NODE_ID: 3, KAFKA_LISTENERS: "PLAINTEXT://:9092,CONTROLLER://:9093", KAFKA_ADVERTISED_LISTENERS: "PLAINTEXT://localhost:9095" }
Docker image variables: The official image maps
KAFKA_* environment variables to broker properties. Check the image documentation for your version, especially for cluster id handling.
Never expose Kafka to the internet: Place brokers in a private network, and add TLS and authentication (security lessons) before any real traffic.
Conclusion
A production cluster has three dedicated controllers and at least three brokers across zones, with safe topic defaults (replication 3, min ISR 2, no unclean elections, no auto topic creation). Tune the OS, run Kafka under systemd, verify with the quorum and describe tools, and test failures.
Quick Summary: 3 controllers + 3 brokers; RF3/minISR2; format with one id; systemd; test broker failure.

