Knative Broker for Apache Kafka

The Knative Broker for Apache Kafka is an implementation of the Knative Broker API natively targeting Apache Kafka to reduce network hops and offering a better integration with Apache Kafka for the Broker and Trigger API model.

Notable features are:

The Knative Kafka Broker stores incoming CloudEvents as Kafka records, using the binary content mode, because it is more efficient due to its optimizations for transport or routing, as well avoid JSON parsing. Using binary content mode means all CloudEvent attributes and extensions are mapped as headers on the Kafka record, while the data of the CloudEvent corresponds to the actual value of the Kafka record. This is another benefit of using binary content mode over structured content mode as it is less obstructive and therefore compatible with systems that do not understand CloudEvents.

Prerequisites

  1. You have installed Knative Eventing.
  2. You have access to an Apache Kafka cluster.

Tip

If you need to set up a Kafka cluster, you can do this by following the instructions on the Strimzi Quickstart page.

Installation

  1. Install the Kafka controller by entering the following command:

    1. kubectl apply --filename https://github.com/knative-extensions/eventing-kafka-broker/releases/download/knative-v1.14.9/eventing-kafka-controller.yaml
  2. Install the Kafka Broker data plane by entering the following command:

    1. kubectl apply --filename https://github.com/knative-extensions/eventing-kafka-broker/releases/download/knative-v1.14.9/eventing-kafka-broker.yaml
  3. Verify that kafka-controller, kafka-broker-receiver and kafka-broker-dispatcher are running, by entering the following command:

    1. kubectl get deployments.apps -n knative-eventing

    Example output:

    1. NAME READY UP-TO-DATE AVAILABLE AGE
    2. eventing-controller 1/1 1 1 10s
    3. eventing-webhook 1/1 1 1 9s
    4. kafka-controller 1/1 1 1 3s
    5. kafka-broker-dispatcher 1/1 1 1 4s
    6. kafka-broker-receiver 1/1 1 1 5s

Create a Kafka Broker

A Kafka Broker object looks like this:

  1. apiVersion: eventing.knative.dev/v1
  2. kind: Broker
  3. metadata:
  4. annotations:
  5. # case-sensitive
  6. eventing.knative.dev/broker.class: Kafka
  7. # Optional annotation to point to an externally managed kafka topic:
  8. # kafka.eventing.knative.dev/external.topic: <topic-name>
  9. name: default
  10. namespace: default
  11. spec:
  12. # Configuration specific to this broker.
  13. config:
  14. apiVersion: v1
  15. kind: ConfigMap
  16. name: kafka-broker-config
  17. namespace: knative-eventing
  18. # Optional dead letter sink, you can specify either:
  19. # - deadLetterSink.ref, which is a reference to a Callable
  20. # - deadLetterSink.uri, which is an absolute URI to a Callable (It can potentially be out of the Kubernetes cluster)
  21. delivery:
  22. deadLetterSink:
  23. ref:
  24. apiVersion: serving.knative.dev/v1
  25. kind: Service
  26. name: dlq-service

Configure a Kafka Broker

The spec.config should reference any ConfigMap in any namespace that looks like the following:

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: kafka-broker-config
  5. namespace: knative-eventing
  6. data:
  7. # Number of topic partitions
  8. default.topic.partitions: "10"
  9. # Replication factor of topic messages.
  10. default.topic.replication.factor: "3"
  11. # A comma separated list of bootstrap servers. (It can be in or out the k8s cluster)
  12. bootstrap.servers: "my-cluster-kafka-bootstrap.kafka:9092"

This ConfigMap is installed in the Knative Eventing SYSTEM_NAMESPACE in the cluster. You can edit the global configuration depending on your needs. You can also override these settings on a per broker base, by referencing a different ConfigMap on a different namespace or with a different name on your Kafka Broker’s spec.config field.

Note

The default.topic.replication.factor value must be less than or equal to the number of Kafka broker instances in your cluster. For example, if you only have one Kafka broker, the default.topic.replication.factor value should not be more than 1.

Knative supports the full set of topic config options that your version of Kafka supports. To set any of these, you need to add a key to the configmap with the default.topic.config. prefix. For example, to set the retention.ms value you would modify the ConfigMap to look like the following:

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: kafka-broker-config
  5. namespace: knative-eventing
  6. data:
  7. # Number of topic partitions
  8. default.topic.partitions: "10"
  9. # Replication factor of topic messages.
  10. default.topic.replication.factor: "3"
  11. # A comma separated list of bootstrap servers. (It can be in or out the k8s cluster)
  12. bootstrap.servers: "my-cluster-kafka-bootstrap.kafka:9092"
  13. # Here is our retention.ms config
  14. default.topic.config.retention.ms: "3600"

Set as default broker implementation

To set the Kafka broker as the default implementation for all brokers in the Knative deployment, you can apply global settings by modifying the config-br-defaults ConfigMap in the knative-eventing namespace.

This allows you to avoid configuring individual or per-namespace settings for each broker, such as metadata.annotations.eventing.knative.dev/broker.class or spec.config.

The following YAML is an example of a config-br-defaults ConfigMap using Kafka broker as the default implementation.

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: config-br-defaults
  5. namespace: knative-eventing
  6. data:
  7. default-br-config: |
  8. clusterDefault:
  9. brokerClass: Kafka
  10. apiVersion: v1
  11. kind: ConfigMap
  12. name: kafka-broker-config
  13. namespace: knative-eventing
  14. namespaceDefaults:
  15. namespace1:
  16. brokerClass: Kafka
  17. apiVersion: v1
  18. kind: ConfigMap
  19. name: kafka-broker-config
  20. namespace: knative-eventing
  21. namespace2:
  22. brokerClass: Kafka
  23. apiVersion: v1
  24. kind: ConfigMap
  25. name: kafka-broker-config
  26. namespace: knative-eventing

Security

Apache Kafka supports different security features, Knative supports the followings:

To enable security features, in the ConfigMap referenced by broker.spec.config, we can reference a Secret:

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: kafka-broker-config
  5. namespace: knative-eventing
  6. data:
  7. # Other configurations
  8. # ...
  9. # Reference a Secret called my_secret
  10. auth.secret.ref.name: my_secret

The Secret my_secret must exist in the same namespace of the ConfigMap referenced by broker.spec.config, in this case: knative-eventing.

Note

Certificates and keys must be in PEM format.

Authentication using SASL

Knative supports the following SASL mechanisms:

  • PLAIN
  • SCRAM-SHA-256
  • SCRAM-SHA-512

To use a specific SASL mechanism replace <sasl_mechanism> with the mechanism of your choice.

Authentication using SASL without encryption

  1. kubectl create secret --namespace <namespace> generic <my_secret> \
  2. --from-literal=protocol=SASL_PLAINTEXT \
  3. --from-literal=sasl.mechanism=<sasl_mechanism> \
  4. --from-literal=user=<my_user> \
  5. --from-literal=password=<my_password>

Authentication using SASL and encryption using SSL

  1. kubectl create secret --namespace <namespace> generic <my_secret> \
  2. --from-literal=protocol=SASL_SSL \
  3. --from-literal=sasl.mechanism=<sasl_mechanism> \
  4. --from-file=ca.crt=caroot.pem \
  5. --from-literal=user=<my_user> \
  6. --from-literal=password=<my_password>

Encryption using SSL without client authentication

  1. kubectl create secret --namespace <namespace> generic <my_secret> \
  2. --from-literal=protocol=SSL \
  3. --from-file=ca.crt=<my_caroot.pem_file_path> \
  4. --from-literal=user.skip=true

Authentication and encryption using SSL

  1. kubectl create secret --namespace <namespace> generic <my_secret> \
  2. --from-literal=protocol=SSL \
  3. --from-file=ca.crt=<my_caroot.pem_file_path> \
  4. --from-file=user.crt=<my_cert.pem_file_path> \
  5. --from-file=user.key=<my_key.pem_file_path>

Note

ca.crt can be omitted to fallback to use system’s root CA set.

Bring your own topic

By default the Knative Kafka Broker creates its own internal topic, however it is possible to point to an externally managed topic, using the kafka.eventing.knative.dev/external.topic annotation:

  1. apiVersion: eventing.knative.dev/v1
  2. kind: Broker
  3. metadata:
  4. annotations:
  5. # case-sensitive
  6. eventing.knative.dev/broker.class: Kafka
  7. kafka.eventing.knative.dev/external.topic: <my-topic-name>
  8. name: default
  9. namespace: default
  10. spec:
  11. # other spec fields ...

Note

When using an external topic, the Knative Kafka Broker does not own the topic and is not responsible for managing the topic. This includes the topic lifecycle or its general validity. Other restrictions for general access to the topic may apply. See the documentation about using Access Control Lists (ACLs).

Consumer Offsets Commit Interval

Kafka consumers keep track of the last successfully sent events by committing offsets.

Knative Kafka Broker commits the offset every auto.commit.interval.ms milliseconds.

Note

To prevent negative impacts to performance, it is not recommended committing offsets every time an event is successfully sent to a subscriber.

The interval can be changed by changing the config-kafka-broker-data-plane ConfigMap in the knative-eventing namespace by modifying the parameter auto.commit.interval.ms as follows:

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: config-kafka-broker-data-plane
  5. namespace: knative-eventing
  6. data:
  7. # Some configurations omitted ...
  8. config-kafka-broker-consumer.properties: |
  9. # Some configurations omitted ...
  10. # Commit the offset every 5000 millisecods (5 seconds)
  11. auto.commit.interval.ms=5000

Note

Knative Kafka Broker guarantees at least once delivery, which means that your applications may receive duplicate events. A higher commit interval means that there is a higher probability of receiving duplicate events, because when a Consumer restarts, it restarts from the last committed offset.

Kafka Producer and Consumer configurations

Knative exposes all available Kafka producer and consumer configurations that can be modified to suit your workloads.

You can change these configurations by modifying the config-kafka-broker-data-plane ConfigMap in the knative-eventing namespace.

Documentation for the settings available in this ConfigMap is available on the Apache Kafka website, in particular, Producer configurations and Consumer configurations.

Enable debug logging for data plane components

The following YAML shows the default logging configuration for data plane components, that is created during the installation step:

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: kafka-config-logging
  5. namespace: knative-eventing
  6. data:
  7. config.xml: |
  8. <configuration>
  9. <appender name="jsonConsoleAppender" class="ch.qos.logback.core.ConsoleAppender">
  10. <encoder class="net.logstash.logback.encoder.LogstashEncoder"/>
  11. </appender>
  12. <root level="INFO">
  13. <appender-ref ref="jsonConsoleAppender"/>
  14. </root>
  15. </configuration>

To change the logging level to DEBUG, you must:

  1. Apply the following kafka-config-logging ConfigMap or replace level="INFO" with level="DEBUG" to the ConfigMap kafka-config-logging:

    1. apiVersion: v1
    2. kind: ConfigMap
    3. metadata:
    4. name: kafka-config-logging
    5. namespace: knative-eventing
    6. data:
    7. config.xml: |
    8. <configuration>
    9. <appender name="jsonConsoleAppender" class="ch.qos.logback.core.ConsoleAppender">
    10. <encoder class="net.logstash.logback.encoder.LogstashEncoder"/>
    11. </appender>
    12. <root level="DEBUG">
    13. <appender-ref ref="jsonConsoleAppender"/>
    14. </root>
    15. </configuration>
  2. Restart the kafka-broker-receiver and the kafka-broker-dispatcher, by entering the following commands:

    1. kubectl rollout restart deployment -n knative-eventing kafka-broker-receiver
    2. kubectl rollout restart deployment -n knative-eventing kafka-broker-dispatcher

Configuring the order of delivered events

When dispatching events, the Kafka broker can be configured to support different delivery ordering guarantees.

You can configure the delivery order of events using the kafka.eventing.knative.dev/delivery.order annotation on the Trigger object:

  1. apiVersion: eventing.knative.dev/v1
  2. kind: Trigger
  3. metadata:
  4. name: my-service-trigger
  5. annotations:
  6. kafka.eventing.knative.dev/delivery.order: ordered
  7. spec:
  8. broker: my-kafka-broker
  9. subscriber:
  10. ref:
  11. apiVersion: serving.knative.dev/v1
  12. kind: Service
  13. name: my-service

The supported consumer delivery guarantees are:

  • unordered: An unordered consumer is a non-blocking consumer that delivers messages unordered, while preserving proper offset management. Useful when there is a high demand of parallel consumption and no need for explicit ordering. One example could be processing of click analytics.
  • ordered: An ordered consumer is a per-partition blocking consumer that waits for a successful response from the CloudEvent subscriber before it delivers the next message of the partition. Useful when there is a need for more strict ordering or if there is a relationship or grouping between events. One example could be processing of customer orders.

The unordered delivery is the default ordering guarantee.

Data plane Isolation vs Shared Data plane

Knative Kafka Broker implementation has 2 planes: control plane and data plane. Control plane consists of controllers that talk to Kubernetes API, watch for custom objects and manage the data plane.

Data plane is the collection of components that listen for incoming events, talk to Apache Kafka and also sends events to the event sinks. This is where the events flow. Knative Kafka Broker data plane consists of kafka-broker-receiver and kafka-broker-dispatcher deployments.

When using the Broker class Kafka, the Knative Kafka Broker uses a shared data plane. That means, kafka-broker-receiver and kafka-broker-dispatcher deployments in knative-eventing namespace is used for all Kafka Brokers in the cluster.

However, when KafkaNamespaced is set as the Broker class, Kafka broker controller creates a new data plane for each namespace that there is a broker exists. This data plane is used by all KafkaNamespaced brokers in that namespace.

That provides isolation between the data planes, which means that the kafka-broker-receiver and kafka-broker-dispatcher deployments in the user namespace are only used for the broker in that namespace.

Note

As a consequence of separate data planes, this security feature creates more deployments and uses more resources. Unless you have such isolation requirements, it is recommended to go with regular Broker with Kafka class.

To create a KafkaNamespaced broker, you must set the eventing.knative.dev/broker.class annotation to KafkaNamespaced:

  1. apiVersion: eventing.knative.dev/v1
  2. kind: Broker
  3. metadata:
  4. annotations:
  5. # case-sensitive
  6. eventing.knative.dev/broker.class: KafkaNamespaced
  7. name: default
  8. namespace: my-namespace
  9. spec:
  10. config:
  11. # the referenced `configmap` must be in the same namespace with the `Broker` object, in this case `my-namespace`
  12. apiVersion: v1
  13. kind: ConfigMap
  14. name: my-config
  15. # namespace: my-namespace # no need to define, defaults to Broker's namespace

Note

The configmap that is specified in spec.config must be in the same namespace with the Broker object:

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: my-config
  5. namespace: my-namespace
  6. data:
  7. ...

Upon the creation of the first Broker with KafkaNamespaced class, the kafka-broker-receiver and kafka-broker-dispatcher deployments are created in the namespace. After that, all the brokers with KafkaNamespaced class in the same namespace use the same data plane. When there are no brokers of KafkaNamespaced class in the namespace, the data plane in the namespace will be deleted.

Configuring KafkaNamespaced brokers

All the configuration mechanisms that are available for the Kafka Broker class are also available for the brokers with KafkaNamespaced class with these exceptions:

  • This page describes how producer and consumer configurations is done by modifying the config-kafka-broker-data-plane configmap in the knative-eventing namespace. Since Kafka Broker controller propagates this configmap into the user namespace, currently there is no way to configure producer and consumer configurations per namespace. Any value set in the config-kafka-broker-data-plane ConfigMap in the knative-eventing namespace will be also used in the user namespace.
  • Because of the same propagation, it is also not possible to configure consumer offsets commit interval per namespace.
  • A few more configmaps are propagated: config-tracing and kafka-config-logging. This means, tracing and logging are also not configurable per namespace.
  • Similarly, the data plane deployments are propagated from the knative-eventing namespace to the user namespace. This means that the data plane deployments are not configurable per namespace and will be identical to the ones in the knative-eventing namespace.

Additional information