Monitoring

Scrape broker metrics and consumer lag into Prometheus, alert in Alertmanager, and use the Streams for Apache Kafka console.

Updated CompTech Team

A broker that is up can still be a cluster that is stuck. The signals that matter are under-replicated partitions, a controller that is not there, and consumer lag that is growing. This lab turns those on for the lab cluster, writes one alert, and opens the Streams for Apache Kafka console so an operator can see the same facts without a query.

OpenShift already runs Prometheus. Kafka joins it as another scrape target. The user-workload monitoring stack must be enabled, which is a cluster setting, not a per-project one.

Prerequisites

  • The lab cluster Ready
  • OpenShift user-workload monitoring enabled, so a PodMonitor in kafka is scraped
  • Permission to create a PrometheusRule in kafka

Export the metrics

Kafka Exporter publishes consumer-group lag. The JMX exporter publishes broker internals. Streams for Apache Kafka ships example JMX ConfigMaps with the operator install. Reference one from the cluster rather than pasting a few hundred lines into the lab. The shape is:

apiVersion: kafka.strimzi.io/v1beta2
kind: Kafka
metadata:
  name: lab
  namespace: kafka
spec:
  kafka:
    version: 4.2.0
    metricsConfig:
      type: jmxPrometheusExporter
      valueFrom:
        configMapKeyRef:
          name: kafka-metrics
          key: kafka-metrics-config.yml
  kafkaExporter:
    topicRegex: ".*"
    groupRegex: ".*"

Keep the listeners, node pools and config you already applied. This fragment is the addition. A PodMonitor selects the broker pods and the exporter pod on the metrics ports the operator exposes. After the next scrape, these queries should return series:

kafka_server_replicamanager_underreplicatedpartitions
kafka_consumergroup_lag

Under-replicated partitions should be zero while you are not bouncing a broker. Lag should move when you start the posting consumer and go quiet when it catches up. If lag is absent, the exporter is not scraping that group. Check groupRegex and that the consumer has committed at least once. A group that never commits has no lag to report.

The console is the other view of the same cluster. Install the Streams for Apache Kafka console operator and point it at lab. It lists brokers, topics and consumer groups for people who will not open a PromQL window. It does not page anyone. The alert does.

Alerts that mean something

A threshold copied from another estate will page you for a normal payroll run, or never page you at all. Start with the conditions that are wrong at any size, and add a lag alert only after you have seen a week's lag for that group.

apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: kafka
  namespace: kafka
spec:
  groups:
    - name: kafka
      rules:
        - alert: KafkaUnderReplicatedPartitions
          expr: sum(kafka_server_replicamanager_underreplicatedpartitions) > 0
          for: 10m
          labels:
            severity: warning
          annotations:
            summary: A partition on the lab cluster is under-replicated
        - alert: KafkaPostingLag
          expr: sum(kafka_consumergroup_lag{consumergroup="posting", topic="payments"}) > 1000
          for: 15m
          labels:
            severity: warning
          annotations:
            summary: The posting group is behind on payments

The threshold of 1000 records is a stand-in until you have a week of this group. Divide the group's normal records per minute by how long you can afford it to be down, and use that product. A fraud group from Kafka for AI gets a much shorter for than a warehouse group. Alert on the dead-letter topic's produce rate separately. Lag on payments.dlt is not the problem. Any traffic on it is.

for: 10m avoids a page during a rolling restart, which the scaling and upgrades lab will cause on purpose. Under-replication that lasts through the restart is the page.

What to look at first

Symptom Look at
Under-replicated partitions and one broker down That broker's pod and its volume. Do not add brokers yet
Lag growing, processing time flat The consumer is not running, or it is stuck on one partition. A single poison record blocks that partition. See delivery guarantees
Lag growing, processing time up The consumer is slower than the producer. Scale the group, if there are partitions spare, or look at the database it writes to
Produce errors, under-replication zero Authorisation, or min.insync.replicas not met. Security and the broker count
The console and Prometheus disagree They are looking at different clusters, or the exporter's regex dropped the group

Dashboards are useful after the alerts are boring. Build a Grafana board from the same metrics: under-replicated partitions, produce and fetch rate, and lag by group. The board is not the alert.

What's next

  1. A lag alert during a restart should clear on its own after a rolling upgrade.
  2. The posting group's lag only means what you think if the consumer commits after the ledger write, from delivery guarantees.
  3. The DR cluster needs the same rules, pointed at its own brokers, in DC/DR.