Create a cronjob in Knative publishing messages to Kafka

287 Views Asked by At

I would like to create a cronjob via Knative that sends healthcheck messages to my Kafka topic every 10 minutes.
Then we will have a separate endpoint created, that will receive these messages and pass some response to a receiver (healthcheck).
Tried using KafkaBinding, which seems suitable, but cannot apply this template (TLS): https://github.com/knative/docs/tree/main/code-samples/eventing/kafka/binding
I also find it odd, that regular KafkaBinding template contains apiVersion: bindings.knative.dev/v1beta1, while the one with TLS: sources.knative.dev/v1beta1.
Haven't found much documentation on how to create a Cronjob sending messages on Kafka, which is then grabbed using KafkaSource and passed using broker+trigger to my service on K8s.
Anyone got it implemented right? Or found another way for this?
The idea is test the whole flow including KafkaSource, which seems a little bit unstable.

1

There are 1 best solutions below

0
On

The way to do this without writing any custom code is to create a PingSource + KafkaSink, that would send a fixed event periodically to a Kafka topic.

Configure the event source object:

# See reference API for more details https://knative.dev/docs/eventing/sources/ping-source/reference/
apiVersion: sources.knative.dev/v1
kind: PingSource
metadata:
  name: ping-name-kafka-sink
spec:
  schedule: "*/10 * * * *" # Every 10 minutes
  contentType: "text/plain"
  data: 'healthcheck' # whatever you'd like to send
  # Send event to KafkaSink
  sink:
    ref:
      apiVersion: eventing.knative.dev/v1alpha1
      kind: KafkaSink
      name: my-kafka-sink

Configure the event sink object:

# See docs for more details https://knative.dev/docs/eventing/sinks/kafka-sink/
apiVersion: eventing.knative.dev/v1alpha1
kind: KafkaSink
metadata:
  name: my-kafka-sink
spec:
  topic: mytopic
  bootstrapServers: # Kafka cluster URL
   - my-cluster-kafka-bootstrap.kafka:9092