Knatve Sink and SinkBinding

In this section we will connect the Knative Services (refer to previous section) to Kafka using Knative Sink and SinkBinding.

SinkBinding supports decoupling the source (service which produces events) from the actual sink by injecting the sink URL into the services.

1. Create Sink and SinkBinding

This solution needs a number of Sinks and SinkBinding for the various Kafka topics described in an earlier section. You will create one of them here, while the others have been preconfigured for you.

Here is a visual of how the reviews flows from the User to Kafka with Knative eventing.

  • The reviews submitted by the user are sent to the product_reviews Quarkus service through HTTP POST.

  • The product_reviews service sends this review as a CloudEvent to the reviews-sink Kafka Sink over HTTP.

  • The Quarkus service remains agnostic to the internals of the Kafka streaming platform.

  • The reviews-sink Kafka Sink sends this Cloud Event to the globex.reviews Kafka topic.

reviews keventing kafka

Now, go ahead and create the Sink and SinkBinding.

  1. Navigate to OpenShift console {openshift_cluster_console}[OpenShift Console^, window="console"] and if needed login with ({user_name}/{user_password}).

  2. From the the top right corner of the OpenShift console, click on the (+) button and then Import Yaml dropdown option

    console add yaml
  3. Copy the following YAML into the Import YAML form, and click Create to create the KafkaSink reviews-sink which will send messages to globex.reviews Kafka Topic.

    apiVersion: eventing.knative.dev/v1alpha1
    kind: KafkaSink
    metadata:
      name: reviews-sink
      namespace: globex-serverless-{user_name}
    spec:
      bootstrapServers:
        - kafka-kafka-bootstrap.globex-mw-{user_name}.svc.cluster.local:9092
      topic: globex.reviews
      numPartitions: 1
      contentMode: binary
      auth:
         secret:
           ref:
             name: kafka-secret
  4. Use the Import YAML form to create a Sink Binding from the product-reviews Quarkus Service to the KafkaSink reviews-sink that you created in the previous step.

    apiVersion: sources.knative.dev/v1
    kind: SinkBinding
    metadata:
      name: product-reviews-to-reviews-sink
      namespace: globex-serverless-{user_name}
    spec:
      sink:
        ref:
          apiVersion: eventing.knative.dev/v1alpha1
          kind: KafkaSink
          name: reviews-sink
          namespace: globex-serverless-{user_name}
      subject:
        apiVersion: apps/v1
        kind: Deployment
        name: product-reviews
        namespace: globex-serverless-{user_name}
  5. Navigate back to the {openshift_cluster_console}/topology/ns/globex-serverless-{user_name}?view=graph[Topology View, window="console", target="console"], to view the new Sink and SinkBinding you created

    sink sinkb created
  6. Here is the list of all the Kafka Sinks used in this solution.

    Sink name

    Function

    reviews-sink

    Send the reviews submitted by user (HTTP POST from globex-web app to product-reviews Quarkus service) as CloudEvents to globex.reviews Kafka topic

    moderated-reviews-sink

    Sends reviews moderated by the aiml-moderate-reviews service to topic reviews.moderated

    reviews-sentiment-sink

    Sends sentiment score of reviews by the aiml-sentiment-reviews service to topic reviews.sentiment