Platform Engineering · Lesson 147

Kafka on Kubernetes with Strimzi: KRaft Clusters, Rolling Changes, Topics, and Users

Running Kafka on Kubernetes by hand means StatefulSets, certificates, listeners, and a careful broker-by-broker restart every time a setting changes. Strimzi turns that into an operator: you describe the cluster in a Kafka resource and its node pools, topics in KafkaTopic resources, and users with their ACLs in KafkaUser resources, and the Cluster Operator does the rest. Red Hat ships the same project as Streams for Apache Kafka, formerly AMQ Streams. This lesson follows one configuration change through a three-broker KRaft cluster, shows how the operator rolls brokers without losing writes, why it refuses to roll when a broker is already down, and what clients with unsafe settings do during the roll.

Difficulty: Advanced · Know this first: Lesson 144, Lesson 145, Lesson 101

Run it as:

When it breaks, I check

One thing to remember

Where the time goes

This bar splits one rolling configuration change across three brokers, in seconds. It is a worked example, not a measurement. Waiting for each broker to catch up is most of it, and it is time well spent.

Try it: can producers still write?

Choose the replication factor, min.insync.replicas, and how many brokers holding a partition are down. Producers with acks=all need min.insync.replicas copies in sync to write.

acks=all writes succeed while RF − down ≥ min.insync.replicas · no replica left means no leader · min.insync.replicas 1 lets a single copy accept writes

2replicas in sync
yesacks=all writes
0more failures tolerated

Writes continue; no further restart is safe until this one catches up

Check yourself

Three questions. No score is saved anywhere — this is just you against the material.

Basic to master

Strimzi grows in four levels: first what the operator manages, then a cluster with node pools, listeners, topics, and users, then safe changes, upgrades, and rebalancing, and finally running Kafka as a platform service.

Basic — Kafka described as Kubernetes resources

At the basic level you know the resources. Kafka describes the cluster: version, listeners, authorization, and config. KafkaNodePool describes groups of nodes: roles (controller, broker, or both), replicas, and storage. KafkaTopic and KafkaUser describe topics and users. Optional ones add Kafka Connect, MirrorMaker 2, the Kafka Bridge, and Cruise Control rebalances.

KRaft: current Strimzi runs Kafka without ZooKeeper; controller nodes hold the metadata in a Raft quorum.

Where it breaks: editing pods or config inside them by hand; the operator puts them back.

Try it: Basic

Which resource says how many broker nodes there are and what disks they use?

Now do it: Read the Kafka and KafkaNodePool manifests in Go deeper and say which nodes are controllers and which are brokers.

Intermediate — Listeners, topics, users, and metrics

At the intermediate level you build a usable cluster.

  • Listeners: internal TLS for in-cluster clients, and an external listener (route on OpenShift, loadbalancer, nodeport, or ingress) with TLS and authentication.
  • Authentication and authorization: mTLS or SCRAM-SHA-512 per listener, and authorization: simple for ACLs.
  • Topics: KafkaTopic with partitions, replication factor 3, and min.insync.replicas: 2.
  • Users: KafkaUser with ACLs for exactly the topics and groups each application needs.
  • Metrics: Prometheus metrics for brokers and the Kafka Exporter for consumer lag, with the Strimzi Grafana dashboards.

Try it: Intermediate

An application needs to consume from topic orders as group billing. What should its KafkaUser grant?

Now do it: Write the KafkaUser in Go deeper for a producer to orders instead, and check which operations change.

Advanced — Safe changes, upgrades, and rebalancing

At the advanced level you change clusters without losing writes.

  • Rolling restarts: one broker at a time, only when every partition keeps min.insync.replicas; check under-replicated partitions are zero first.
  • Upgrades: upgrade the operator, then spec.kafka.version (a roll), then the metadata version once all clients are tested.
  • Rebalancing: Cruise Control through KafkaRebalance: review the proposal, approve it, and throttle replication so moves do not starve clients.
  • Node maintenance: PodDisruptionBudgets and the Strimzi Drain Cleaner so Kubernetes drains go through the operator.
  • Clients: acks=all, idempotence on, default retries, and delivery.timeout.ms of minutes.

Failure scenarios: a full disk on one broker blocks every roll; an under-sized PVC cannot be shrunk; JBOD volumes can be added but not removed casually.

Try it: Advanced

A Kafka version upgrade is applied and the roll stalls with the operator reporting it cannot roll broker 1. What do you check first?

Now do it: Run kafka_availability.py in Go deeper and find the settings at which a roll is never safe.

Master — Kafka as a platform service

At the master level you run Kafka for many teams.

  • Tenancy: topics and users created through Git with naming rules and quotas (produce and fetch byte rates per user) so one team cannot starve others.
  • Capacity: partitions per broker, disk retention × throughput, and network for replication (each byte is written three times).
  • Disaster recovery: MirrorMaker 2 to another cluster or region, with consumer offset translation, and a tested failover runbook.
  • Security: mTLS everywhere, short-lived certificates rotated by the operator, ACLs reviewed, and network policies around the brokers.
  • Operations: alerts on under-replicated and offline partitions, under-min-ISR, disk at 70 percent, controller quorum health, and consumer lag by group.

Trade-off: one large shared cluster is efficient and makes noisy neighbours everyone's problem; several clusters isolate teams and multiply the operations.

Try it: Master

One team's batch job saturates the brokers' network every night and other teams' consumer lag grows. What fixes it at the platform level?

Now do it: For your cluster, compute disk needed: daily bytes in × retention days × replication factor, and compare with what the PVCs hold.

Go deeper

Kafka's write availability is arithmetic on three numbers. This script prints when acks=all producers can write, whether a rolling restart is safe, and how long a consumer group needs to catch up after a pause. Standard library only; python3 kafka_availability.py.

Replication, restarts, and lag

# kafka_availability.py - can producers with acks=all still write, and is a rolling restart safe?
def writes_ok(rf, min_isr, down):
    return rf - down >= min_isr

print("RF  min.insync  brokers down  acks=all writes")
for rf, mi in [(3, 2), (3, 1), (1, 1)]:
    for down in range(0, rf + 1):
        verdict = "ok" if writes_ok(rf, mi, down) else "fail: no leader, partition offline" if down == rf else "fail: NotEnoughReplicas"
        if mi == 1 and rf > 1 and down == rf - 1:
            verdict += " (one copy left: the next failure loses data)"
        print(f"{rf:>2}  {mi:>10}  {down:>12}  {verdict}")

# A rolling restart takes one more broker away, on purpose, for a few minutes.
def safe_to_roll(rf, min_isr, already_down):
    return writes_ok(rf, min_isr, already_down + 1)

for already in (0, 1):
    print(f"RF 3, min.insync 2, {already} broker(s) already down -> restarting one more is "
          f"{'safe' if safe_to_roll(3, 2, already) else 'NOT safe: wait or fix the broken broker first'}")

# Consumer lag after a pause: how long until the group catches up?
lag, incoming, consumers, per_consumer = 1_200_000, 18_000, 12, 2_000   # messages and messages/s
drain = consumers * per_consumer - incoming
print(f"lag {lag:,} messages, consuming {consumers * per_consumer:,}/s against {incoming:,}/s arriving "
      f"-> caught up in about {lag / drain / 60:.0f} minutes")
$ python3 kafka_availability.py
RF  min.insync  brokers down  acks=all writes
 3           2             0  ok
 3           2             1  ok
 3           2             2  fail: NotEnoughReplicas
 3           2             3  fail: no leader, partition offline
 3           1             0  ok
 3           1             1  ok
 3           1             2  ok (one copy left: the next failure loses data)
 3           1             3  fail: no leader, partition offline
 1           1             0  ok
 1           1             1  fail: no leader, partition offline
RF 3, min.insync 2, 0 broker(s) already down -> restarting one more is safe
RF 3, min.insync 2, 1 broker(s) already down -> restarting one more is NOT safe: wait or fix the broken broker first
lag 1,200,000 messages, consuming 24,000/s against 18,000/s arriving -> caught up in about 3 minutes

Read it like this: RF 3 with min.insync.replicas 2 survives one broker down, which is exactly the room a rolling restart uses, so a roll with a broker already down must wait; min.insync.replicas 1 keeps writing with one copy at the cost of losing data on the next failure; and a group consuming 6,000 messages a second faster than they arrive clears 1.2 million messages of lag in a few minutes.

A KRaft cluster with node pools, a topic, and a user

apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaNodePool
metadata:
  name: brokers
  namespace: kafka
  labels:
    strimzi.io/cluster: shop
spec:
  replicas: 3
  roles: ["broker"]
  storage:
    type: persistent-claim
    size: 500Gi
    deleteClaim: false
---
apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaNodePool
metadata:
  name: controllers
  namespace: kafka
  labels:
    strimzi.io/cluster: shop
spec:
  replicas: 3
  roles: ["controller"]
  storage:
    type: persistent-claim
    size: 20Gi
---
apiVersion: kafka.strimzi.io/v1beta2
kind: Kafka
metadata:
  name: shop
  namespace: kafka
  annotations:
    strimzi.io/node-pools: enabled
    strimzi.io/kraft: enabled
spec:
  kafka:
    version: 4.0.0
    listeners:
      - name: tls
        port: 9093
        type: internal
        tls: true
        authentication:
          type: tls
    authorization:
      type: simple
    config:
      default.replication.factor: 3
      min.insync.replicas: 2
      offsets.topic.replication.factor: 3
  entityOperator:
    topicOperator: {}
    userOperator: {}
---
apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaTopic
metadata:
  name: orders
  namespace: kafka
  labels:
    strimzi.io/cluster: shop
spec:
  partitions: 24
  replicas: 3
  config:
    min.insync.replicas: 2
    retention.ms: 604800000
---
apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaUser
metadata:
  name: billing
  namespace: kafka
  labels:
    strimzi.io/cluster: shop
spec:
  authentication:
    type: tls
  authorization:
    type: simple
    acls:
      - resource: {type: topic, name: orders}
        operations: [Read, Describe]
      - resource: {type: group, name: billing}
        operations: [Read]

The manifests parse as YAML and use Strimzi's v1beta2 API. The two annotations were required to enable KRaft and node pools in Strimzi releases before KRaft became the only mode; check the documentation for your Strimzi version, and the Kafka versions it supports, before applying. Try them on kind or minikube with the Strimzi quick start first.

Read the cluster's state

kubectl get kafka,kafkanodepool,kafkatopic,kafkauser -n kafka
kubectl get pods -n kafka -l strimzi.io/cluster=shop -w
kubectl -n kafka exec shop-brokers-0 -- bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --under-replicated-partitions
kubectl -n kafka logs deploy/strimzi-cluster-operator | grep -i roll

One time it broke

This is a composite of a failure pattern that keeps repeating; the numbers are illustrative. A team ran Kafka on Kubernetes with a plain StatefulSet before moving to an operator.

Timeline

  • Tue 10:00A Kubernetes node upgrade drains nodes two at a time. Two of three Kafka pods are evicted within a minute of each other.
  • Tue 10:01Partitions with replicas on both drop below min.insync.replicas. acks=all producers fail; the order service stops accepting orders.
  • Tue 10:09The pods are rescheduled and catch up. Orders resume after eight minutes.

What changed after

Kafka moved to Strimzi, which creates PodDisruptionBudgets and checks replica safety before every restart, and the Strimzi Drain Cleaner was installed so node drains hand evictions to the operator. The node upgrade process now waits for under-replicated partitions to reach zero between nodes.

If you remember nothing else

Design exercise

Design a Kafka platform on Kubernetes with Strimzi for 20 teams, carrying orders, payments, and clickstream events, with a second region for disaster recovery.

Clarifying questions to ask

What throughput and retention does each team need? Which topics need acks=all and min.insync.replicas 2? Who may create topics and users? How will node upgrades and Kafka upgrades be done? What must keep working if a region fails?

Rough sizing

Say 150 MB/s in at peak, 7 days of retention, replication factor 3, 3 zones, and 2,000 partitions.

Shape of the answer

Three controllers and six brokers in node pools spread across zones with rack awareness; 150 MB/s × 7 days × 3 copies ≈ 270 TB of disk with headroom, on fast persistent volumes; TLS listeners with mTLS users and ACLs from Git; quotas per user; Cruise Control for rebalancing; Drain Cleaner for node drains; MirrorMaker 2 to the second region with offset sync and a rehearsed failover; alerts on under-replicated, offline and under-min-ISR partitions, disk, and lag.

Trade-offs to name

A shared cluster is efficient and needs quotas and careful tenancy; separate clusters isolate teams and multiply the operations. Running Kafka on Kubernetes keeps one platform and needs fast local or network storage and drain discipline; a managed Kafka service removes that work and costs more per byte. Cross-region replication protects data and adds lag and the complexity of offset translation at failover.

Saved on this device, in this browser.
Next in this series · Lesson 148
Schema Registry for Kafka: Schema IDs, Compatibility Modes, and Safe Schema Evolution

A Kafka topic outlives every program that writes to it, so the shape of its records has to change without breaking the readers. A schema registry makes that safe.

Go to Lesson 148 →

Or browse the full course from the Lessons index.