Kafka Connect

Run Kafka Connect on OpenShift as a KafkaConnect resource, and move records between a topic and PostgreSQL with a connector.

Updated CompTech Team

Kafka Connect moves data between Kafka and another system without a service you have to write. A source connector tails a database or a bucket and produces records. A sink connector consumes a topic and writes rows or objects. The Connect workers are a KafkaConnect resource. Each connector is a KafkaConnector resource. Both belong in Git.

This lab runs a Connect cluster and a JDBC sink that writes payments into PostgreSQL. The matching source, from the database log, is CDC with Debezium.

Prerequisites

  • The lab cluster and the payments topic
  • A PostgreSQL the cluster can reach. In our lab that is a Crunchy Postgres cluster
  • The Streams for Apache Kafka operator, which also reconciles KafkaConnect

A Connect cluster that builds its own image

Strimzi builds a Connect image when you list plugins. The build pushes to the internal registry, so the connect-build pull and push rights have to exist for the kafka project before the first reconcile. The operator documentation for your install shows the exact role binding. The plugin below is the Debezium JDBC sink, which writes records with ordinary INSERT statements.

apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaConnect
metadata:
  name: connect
  namespace: kafka
  annotations:
    strimzi.io/use-connector-resources: "true"
spec:
  version: 4.2.0
  replicas: 1
  bootstrapServers: lab-kafka-bootstrap.kafka.svc:9092
  config:
    group.id: connect
    offset.storage.topic: connect-offsets
    config.storage.topic: connect-configs
    status.storage.topic: connect-status
    config.storage.replication.factor: 3
    offset.storage.replication.factor: 3
    status.storage.replication.factor: 3
  build:
    output:
      type: docker
      image: image-registry.openshift-image-registry.svc:5000/kafka/connect:latest
    plugins:
      - name: debezium-jdbc
        artifacts:
          - type: maven
            group: io.debezium
            artifact: debezium-connector-jdbc
            version: 2.7.3.Final
oc apply -f connect.yaml
oc wait kafkaconnect/connect -n kafka --for=condition=Ready --timeout=900s

The three internal topics are Connect's own state: which offset it has read up to, the connector config, and the task status. Replication factor 3 matches the cluster. Do not point retention at them in a way that drops that state.

One replica is enough to see a task run. Use two or more when a connector's tasksMax is greater than one, so a dead worker does not stop the sink. JDBC sink tasks are split by topic partition.

A sink

The password in this manifest is for the lab. Move it to a Secret and the Kubernetes Secret config provider before this file is committed. The provider is config.providers.secrets.class: io.strimzi.kafka.KubernetesSecretConfigProvider on the KafkaConnect, and the value ${secrets:kafka/sink-db:password} in the connector.

apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaConnector
metadata:
  name: payments-sink
  namespace: kafka
  labels:
    strimzi.io/cluster: connect
spec:
  class: io.debezium.connector.jdbc.JdbcSinkConnector
  tasksMax: 1
  config:
    topics: payments
    connection.url: jdbc:postgresql://postgres.payments.svc:5432/payments
    connection.username: sink
    connection.password: sink
    insert.mode: upsert
    primary.key.mode: record_key
    primary.key.fields: account
    auto.create: "true"
    auto.evolve: "false"
    delete.enabled: "false"

insert.mode: upsert and a primary key make a redelivered record update the row instead of failing or doubling it. That is the same idea as delivery guarantees, applied to a connector. auto.evolve: false so a new field in the JSON does not silently alter the table. Change the table on purpose, after the schema has a new version.

oc apply -f sink.yaml
oc get kafkaconnector payments-sink -n kafka
oc logs -n kafka -l strimzi.io/kind=KafkaConnect --tail=50

Produce a record with key a1 and confirm a row in Postgres. Produce it again and confirm there is still one row.

Files and object storage

The same KafkaConnect runs other plugins beside the JDBC sink. A sink whose class writes to S3-compatible storage is how a topic lands as objects for the lakehouse, one connector per destination, on this same cluster. Give that connector its own tasksMax, its own errors topic, and a bucket the worker's service account can write. Do not add a second KafkaConnect cluster unless the plugin set or the blast radius is genuinely different. Each cluster is a build, a set of internal topics and a thing to upgrade.

Set errors.tolerance only when you also set errors.deadletterqueue.topic.name. A connector that skips bad records and forgets them is a quiet form of data loss. The dead-letter topic from the delivery lab is the pattern.

What's next

  1. The other direction, from Postgres into Kafka, is CDC with Debezium.
  2. Keys and retention on payments come from topic design.
  3. Connector offsets have to fail over with the topics. DC/DR includes the Connect internal topics if Connect runs on one site only.