Learn Labs
Foundations

Docker & Setup

A 3-node KRaft cluster, listeners, and the tools around it

As with every module, there's a minimal compose file and a production-shaped one in kafka/.

The minimal stack

services:
kafka:
  image: apache/kafka:4.3.1
  ports:
    - "9092:9092"
  volumes:
    - kafka_data:/var/lib/kafka/data
volumes:
kafka_data:

The official image's defaults start a single node that's both broker and controller, listening on localhost:9092. Good enough to try the CLI:

cd kafka
docker compose up -d
docker compose exec kafka /opt/kafka/bin/kafka-topics.sh --bootstrap-server localhost:9092 --list

A single node has no replication, so nothing about durability or failover can be learned on it. Stop it before starting the cluster, since both use port 9092.

The cluster

docker compose down
docker compose -f compose_prod.yaml up -d --build
compose_prod.yaml services
kafka-1, kafka-2, kafka-3brokers AND KRaft controllers; RF=3, min.insync.replicas=2schema-registryAvro/Protobuf/JSON schemas, stored in the _schemas topic. :8081kafka-uiweb UI: topics, messages, groups, schemas, connectors. :8086kafka-exporterconsumer lag as Prometheus metrics. :9308cdc-postgres, connectprofile "connect": Debezium change data capturelabsprofile "labs": the Python labs

The brokers are built from broker/: the official image plus the Prometheus JMX exporter agent, so each broker serves its internal metrics on port 7071 (published as 7071-7073).

Cluster-wide defaults

Every broker gets the same settings, from a YAML anchor in the compose file. The apache/kafka image turns each KAFKA_FOO_BAR variable into the server property foo.bar:

KAFKA_PROCESS_ROLES: broker,controllereach node stores data AND votes in the metadata quorumKAFKA_CONTROLLER_QUORUM_VOTERS: 1@kafka-1:29093,2@kafka-2:29093,3@kafka-3:29093KAFKA_DEFAULT_REPLICATION_FACTOR: 3new topics get 3 copiesKAFKA_MIN_INSYNC_REPLICAS: 2acks=all needs 2 copies to succeedKAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 3consumer offsets survive a broker lossKAFKA_UNCLEAN_LEADER_ELECTION_ENABLE: falsenever elect a replica that's missing dataKAFKA_AUTO_CREATE_TOPICS_ENABLE: falsea typo in a topic name is an error, not a new topicKAFKA_LOG_RETENTION_HOURS: 168keep 7 days...KAFKA_LOG_RETENTION_BYTES: 1073741824...or 1 GiB per partition, whichever comes first

Listeners: the part everyone gets wrong

Each broker has three listeners:

ListenerAddressUsed by
INTERNALkafka-N:19092Other containers, and broker-to-broker replication
EXTERNALlocalhost:9092 / 9094 / 9096Clients on your Mac
CONTROLLERkafka-N:29093KRaft quorum traffic, never exposed

So:

# from your Mac
--bootstrap-server localhost:9092,localhost:9094,localhost:9096

# from another container (the labs, Connect, Schema Registry)
--bootstrap-server kafka-1:19092,kafka-2:19092,kafka-3:19092

The CLI

The Kafka tools are shell scripts inside every broker, in /opt/kafka/bin. A shell function saves typing:

K() { docker exec kafka-1 /opt/kafka/bin/"$@"; }

K kafka-topics.sh --bootstrap-server localhost:19092 --list
K kafka-metadata-quorum.sh --bootstrap-server localhost:19092 describe --status

The labs

docker compose -f compose_prod.yaml --profile labs build labs
docker compose -f compose_prod.yaml run --rm labs python producer_basics.py

Everyday commands

docker compose -f compose_prod.yaml ps
docker logs -f kafka-1
docker compose -f compose_prod.yaml --profile connect stop      # stop, keep data
docker compose -f compose_prod.yaml --profile connect down -v   # delete everything

Open http://localhost:8086 for the UI. It's handy for browsing messages and groups; the lessons use the CLI so you learn what the UI is doing.

On this page