建立 Knative 序列以簡化 ML 工作流程¶
我們將學習哪些 Knative 功能?¶
- Knative 序列
最終交付成果看起來如何?¶
- 建立 Knative 序列,其中不良字詞篩選服務為步驟 1,情感分析服務為步驟 2
- 最終結果會以序列的回覆形式傳回 Broker
實作¶
步驟 0:學習序列¶
序列提供一種定義將被調用的函式依序清單的方法。每個步驟都可以修改、篩選或建立一種新的事件。
如果您希望您的事件**以您喜歡的順序**通過不同的服務,Knative 序列是您的選擇。
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:建立序列¶
建立一個名為
sequence/config/100-create-sequence.yaml
的新 yaml 檔案,以在您的叢集中建立序列資源
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
建立序列 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:建立將事件傳遞至序列的觸發器¶
由於序列現在已準備好接受請求,我們需要告訴 Broker 將事件轉發至序列,以便新的評論將通過我們的 ML 工作流程。
建立名為 sequence/config/200-create-trigger.yaml
的觸發器 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
驗證
您可以輕鬆驗證觸發器的狀態
kubectl get triggers
您應該會看到觸發器處於 ready 狀態。
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
到目前為止,**您的叢集應該有以下您建立的觸發器**。
驗證¶
使用以下命令開啟 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"
}
下一步¶
在本教學中,您學習如何建立序列以建置 ML 管道。
接下來,我們將學習如何啟動書店的資料庫服務,同時學習使用 Knative Serving 的最佳案例。