跳至内容

创建 Knative 序列以简化 ML 工作流

image

我们将学习哪些 Knative 功能?

  • Knative 序列

最终成果是什么样的?

image

  • 创建一个 Knative 序列,其中包含屏蔽脏话服务的步骤 1 和情感分析服务的步骤 2
  • 最终结果作为序列的回复发送回 Broker

实施

步骤 0:学习序列

序列提供了一种定义将被调用的函数的有序列表的方法。每个步骤都可以修改、过滤或创建一种新的事件。

如果您希望您的事件以您喜欢的顺序通过不同的服务,Knative 序列是您的选择。

image

apiVersion: flows.knative.dev/v1
kind: Sequence
metadata:
  name: sequence
spec:
  channelTemplate:
    apiVersion: messaging.knative.dev/v1
    kind: InMemoryChannel
  steps:
    - ref:
        apiVersion: serving.knative.dev/v1
        kind: Service
        name: first
    - ref:
        apiVersion: serving.knative.dev/v1
        kind: Service
        name: second
  reply:
    ref:
      kind: Service
      apiVersion: serving.knative.dev/v1
      name: event-display

步骤 1:创建序列

image 在您的集群中创建一个名为 sequence/config/100-create-sequence.yaml 的新 yaml 文件以创建 Sequence 资源

sequence/config/100-create-sequence.yaml
apiVersion: flows.knative.dev/v1
kind: Sequence
metadata:
  name: sequence
spec:
  channelTemplate: # Under the hood, the Sequence will create a Channel for each step in the Sequence
    apiVersion: messaging.knative.dev/v1
    kind: InMemoryChannel
  steps:
    - ref: # This is the first step of the Sequence, it will send the event to the bad-word-filter service
        apiVersion: serving.knative.dev/v1
        kind: Service
        name: bad-word-filter
    - ref: # This is the second step of the Sequence, it will send the event to the sentiment-analysis-app service
        apiVersion: serving.knative.dev/v1
        kind: Service
        name: sentiment-analysis-app
  reply: # This is the last step of the Sequence, it will send the event back to the Broker as reply
    ref:
      kind: Broker
      apiVersion: eventing.knative.dev/v1
      name: bookstore-broker

创建 Sequence yaml 文件并将其应用到您的集群中。

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

应用配置后,您应该看到以下输出

sequence.flows.knative.dev/sequence created
验证

您可以轻松地验证序列的状态

kubectl get sequences

您应该期望 sequence 的 Ready 状态为 True。

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

步骤 2:创建将事件传递到序列的 Trigger

image

由于 Sequence 现在已准备好接受请求,我们需要告诉 Broker 将事件转发到 Sequence,以便新评论将经过我们的 ML 工作流程。

创建名为 sequence/config/200-create-trigger.yaml 的 Trigger yaml 文件

sequence/config/200-create-trigger.yaml
apiVersion: eventing.knative.dev/v1
kind: Trigger
metadata:
  name: sequence-trigger
spec:
  broker: bookstore-broker
  filter:
    attributes:
      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
  subscriber:
    ref:
      apiVersion: flows.knative.dev/v1
      kind: Sequence
      name: sequence

将其应用到您的集群中。

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

您应该看到以下输出

trigger.eventing.knative.dev/sequence-trigger created
验证

您可以轻松地验证 Trigger 的状态

kubectl get triggers

您应该看到 Trigger 处于就绪状态。

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

到目前为止,您的集群应该具有以下由您创建的 Triggerimage

验证

image

使用以下命令打开 event-display 的日志

kubectl logs event-display-XXXXX -f

在 UI 的评论框中输入内容,然后单击提交按钮。bookstore-broker 收到的所有事件都将显示在 event-display 中。

验证

评论应该出现在 event-display 服务中,输出如下

☁️cloudevents.Event
Validation: valid
Context Attributes,
specversion: 1.0
type: moderated-comment
source: sentiment-analysis
id: 2f703218-15d4-4ff8-b2bc-11200e209315
time: 2024-04-21T01:26:27.608365Z
datacontenttype: application/json
Extensions,
badwordfilter: bad
knativearrivaltime: 2024-04-21T01:26:27.617405597Z
sentimentresult: negative
Data,
{
    "reviewText": "XXXXXXXXXXXX",
    "badWordResult": "bad",
    "sentimentResult": "negative"
}

下一步

image

在本教程中,您学习了如何创建序列来构建 ML 管道。

接下来,我们将学习如何启动书店的数据库服务,同时了解何时使用 Knative Serving 最合适。

转到部署数据库服务

我们使用分析和 Cookie 来了解网站流量。关于您使用我们网站的信息将与 Google 共享,以便用于该目的。 了解更多。