Kafka on OpenShift

Install Streams for Apache Kafka and create a KRaft cluster, broker and controller node pools, and topics as custom resources.

Updated CompTech Team

Streams for Apache Kafka is the operator that runs Apache Kafka on OpenShift. You do not install brokers by hand. You apply a Kafka resource and two KafkaNodePool resources, and the operator creates the pods, the Services and the storage. Topics are KafkaTopic resources, so they can live in Git next to the applications that use them.

This lab builds a three-controller, three-broker cluster in KRaft mode. Kafka 4 has no ZooKeeper. Our lab runs Kafka 4.2. The security lab replaces the plain listener. Leave this one inside the cluster.

Prerequisites

  • OpenShift 4.x with the Streams for Apache Kafka operator installed
  • A project kafka and a storage class for the broker volumes
  • oc

The operator watches the project you installed it into, or all projects, depending on how it was subscribed. The resources below go in kafka.

Node pools and a cluster

Controllers hold the KRaft metadata. Brokers hold your partitions. Separate pools let you scale brokers without growing the quorum. A lab that is short on machines can use one pool with both roles and three replicas. The manifest here is the shape to keep.

apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaNodePool
metadata:
  name: controller
  namespace: kafka
  labels:
    strimzi.io/cluster: lab
spec:
  replicas: 3
  roles: [controller]
  storage:
    type: jbod
    volumes:
      - id: 0
        type: persistent-claim
        size: 20Gi
        kraftMetadata: shared
        deleteClaim: false
---
apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaNodePool
metadata:
  name: broker
  namespace: kafka
  labels:
    strimzi.io/cluster: lab
spec:
  replicas: 3
  roles: [broker]
  storage:
    type: jbod
    volumes:
      - id: 0
        type: persistent-claim
        size: 100Gi
        deleteClaim: false
---
apiVersion: kafka.strimzi.io/v1beta2
kind: Kafka
metadata:
  name: lab
  namespace: kafka
spec:
  kafka:
    version: 4.2.0
    listeners:
      - name: plain
        port: 9092
        type: internal
        tls: false
      - name: tls
        port: 9093
        type: internal
        tls: true
    config:
      offsets.topic.replication.factor: 3
      transaction.state.log.replication.factor: 3
      transaction.state.log.min.isr: 2
      default.replication.factor: 3
      min.insync.replicas: 2
  entityOperator:
    topicOperator: {}
    userOperator: {}

kraftMetadata: shared puts the metadata log on the controller volume. deleteClaim: false keeps the disk if someone deletes the pool. min.insync.replicas: 2 with a replication factor of 3 means a produce with acks=all succeeds only when at least two replicas have the record.

oc apply -f kafka.yaml
oc wait kafka/lab -n kafka --for=condition=Ready --timeout=600s
oc get pods -n kafka -l strimzi.io/cluster=lab

The bootstrap Service is lab-kafka-bootstrap.kafka.svc:9092. Clients inside the cluster use that name. Pods are named lab-broker-0 and lab-controller-1.

A topic is a custom resource

apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaTopic
metadata:
  name: payments
  namespace: kafka
  labels:
    strimzi.io/cluster: lab
spec:
  partitions: 6
  replicas: 3
  config:
    retention.ms: "604800000"
    min.insync.replicas: "2"
oc apply -f topic.yaml
oc get kafkatopic payments -n kafka

The topic operator creates the topic in the cluster. If someone creates a topic with a client tool and the operator is in the default unidirectional mode, that topic is not in Git and will not be recreated with the cluster. Prefer the custom resource. Why this topic has six partitions is topic design.

Produce one record

oc exec -n kafka -it lab-broker-0 -- bin/kafka-console-producer.sh \
  --bootstrap-server lab-kafka-bootstrap:9092 --topic payments

Type {"id":"p1","account":"a1","amount":100} and Ctrl-D. Read it back:

oc exec -n kafka -it lab-broker-0 -- bin/kafka-console-consumer.sh \
  --bootstrap-server lab-kafka-bootstrap:9092 --topic payments --from-beginning --max-messages 1

If the consumer prints the line, the cluster, the pool and the topic are doing their jobs. Applications should use a real client, which is the producers and consumers lab, not a shell on a broker.

What's next

  1. Choose the partition count, the key and the retention in topic design.
  2. Turn the plain listener off once security has a TLS listener and a KafkaUser.
  3. Add brokers later without recreating the cluster, in scaling and upgrades.