Create Knative Sequence to Streamline ML Workflows

image

What Knative features will we learn about?

  • Knative Sequence

What does the final deliverable look like?

image

  • Create a Knative Sequence with bad word filter service as step 1 and sentiment analysis service as step 2
  • The final result is sent back to Broker as reply of the Sequence

Implementation

Step 0: Learn Sequence

Sequence provides a way to define an in-order list of functions that will be invoked. Each step can modify, filter or create a new kind of an event.

If you hope your event to pass through different services in an order you like, Knative Sequence is your choice.

image

  1. apiVersion: flows.knative.dev/v1
  2. kind: Sequence
  3. metadata:
  4. name: sequence
  5. spec:
  6. channelTemplate:
  7. apiVersion: messaging.knative.dev/v1
  8. kind: InMemoryChannel
  9. steps:
  10. - ref:
  11. apiVersion: serving.knative.dev/v1
  12. kind: Service
  13. name: first
  14. - ref:
  15. apiVersion: serving.knative.dev/v1
  16. kind: Service
  17. name: second
  18. reply:
  19. ref:
  20. kind: Service
  21. apiVersion: serving.knative.dev/v1
  22. name: event-display

Step 1: Create the Sequence

image Create a new yaml file named sequence/config/100-create-sequence.yaml to create Sequence resource in your cluster:

sequence/config/100-create-sequence.yaml

  1. apiVersion: flows.knative.dev/v1
  2. kind: Sequence
  3. metadata:
  4. name: sequence
  5. spec:
  6. channelTemplate: # Under the hood, the Sequence will create a Channel for each step in the Sequence
  7. apiVersion: messaging.knative.dev/v1
  8. kind: InMemoryChannel
  9. steps:
  10. - ref: # This is the first step of the Sequence, it will send the event to the bad-word-filter service
  11. apiVersion: serving.knative.dev/v1
  12. kind: Service
  13. name: bad-word-filter
  14. - ref: # This is the second step of the Sequence, it will send the event to the sentiment-analysis-app service
  15. apiVersion: serving.knative.dev/v1
  16. kind: Service
  17. name: sentiment-analysis-app
  18. reply: # This is the last step of the Sequence, it will send the event back to the Broker as reply
  19. ref:
  20. kind: Broker
  21. apiVersion: eventing.knative.dev/v1
  22. name: bookstore-broker

Create the Sequence yaml file and apply it to your cluster.

  1. kubectl apply -f sequence/config/100-create-sequence.yaml

After applying the configuration, you should see the following output:

  1. sequence.flows.knative.dev/sequence created

Verify

You can verify the status of the Sequence very easily

  1. kubectl get sequences

You should expect the Ready state for sequence to be True.

  1. NAME URL AGE READY REASON
  2. sequence http://sequence-kn-sequence-0-kn-channel.default.svc.cluster.local 159m True

Step 2: Create the Trigger that pass the event to Sequence

image

As the Sequence is ready to accept the request now, we need to tell the Broker to forward the events to the Sequence, so that new comments will go through our ML workflows.

Create the Trigger yaml file named sequence/config/200-create-trigger.yaml

sequence/config/200-create-trigger.yaml

  1. apiVersion: eventing.knative.dev/v1
  2. kind: Trigger
  3. metadata:
  4. name: sequence-trigger
  5. spec:
  6. broker: bookstore-broker
  7. filter:
  8. attributes:
  9. type: new-review-comment # This is the filter that will be applied to the event, only events with the ce-type new-review-comment will be processed
  10. subscriber:
  11. ref:
  12. apiVersion: flows.knative.dev/v1
  13. kind: Sequence
  14. name: sequence

Apply it to your cluster.

  1. kubectl apply -f sequence/config/200-create-trigger.yaml

And you should see the following output:

  1. trigger.eventing.knative.dev/sequence-trigger created

Verify

You can verify the status of the Trigger very easily

  1. kubectl get triggers

You should see the Trigger in ready state.

  1. NAME BROKER SUBSCRIBER_URI AGE READY REASON
  2. sequence-trigger bookstore-broker http://sequence-kn-sequence-0-kn-channel.default.svc.cluster.local 162m True
  3. log-trigger bookstore-broker http://event-display.default.svc.cluster.local 164m True

And until this point, your cluster should have the following Triggers that are created by you. image

Verification

image

Open the log for event-display with the following command:

  1. kubectl logs event-display-XXXXX -f

Type something in the comment box in the UI and click the submit button. All the events that the bookstore-broker received will be displayed in the event-display.

Verify

The comment should appear in the event-display service with the following output:

  1. ☁️cloudevents.Event
  2. Validation: valid
  3. Context Attributes,
  4. specversion: 1.0
  5. type: moderated-comment
  6. source: sentiment-analysis
  7. id: 2f703218-15d4-4ff8-b2bc-11200e209315
  8. time: 2024-04-21T01:26:27.608365Z
  9. datacontenttype: application/json
  10. Extensions,
  11. badwordfilter: bad
  12. knativearrivaltime: 2024-04-21T01:26:27.617405597Z
  13. sentimentresult: negative
  14. Data,
  15. {
  16. "reviewText": "XXXXXXXXXXXX",
  17. "badWordResult": "bad",
  18. "sentimentResult": "negative"
  19. }

Next Step

image

In this tutorial, you learned how to create a Sequence to build a ML pipeline.

Next, we’ll be learning how to spin up book store’s database services, while learning what will be the best case to use Knative Serving.

Go to Deploy Database Service 4 - Create Sequence - 图9