本文介紹如何整合阿里雲Object Storage Service與阿里雲輕量訊息佇列(原 MNS),通過將檔案上傳至Object Storage Service中,自動觸發工作流程處理檔案,並產生結果。
前提條件
-
已開啟事件驅動功能。
-
已建立OSS事件通知。
-
已建立工作流程叢集。
-
已安裝阿里雲Argo CLI。
步驟一:建立Event Bus
Event Bus可以被命名空間中的事件驅動工作流程共用。如果已經建立,可跳過該步驟,直接執行步驟二:建立Event Source。
-
使用輕量訊息佇列(原 MNS)方式建立Event Bus時,不會建立Pod。
-
如需使用Trigger功能,請使用NATS方式建立EventBus。目前輕量訊息佇列(原 MNS)方式不支援開源Argo Event的Sensor Trigger。
方式一:使用NATS
-
建立
event-bus.yaml檔案。Event Bus範例程式碼如下所示:apiVersion: argoproj.io/v1alpha1 kind: EventBus metadata: name: default spec: nats: native: replicas: 3 auth: token -
執行以下命令,建立EventBus。
kubectl apply -f event-bus.yaml說明命令執行成功後,會在default命名空間下建立Event Bus Pod。後續操作需在同一命名空間下。
-
執行以下命令,查看Event Bus Pod是否正常啟動。
kubectl get pod
方式二:使用輕量訊息佇列(原 MNS)
-
在主題列表頁面建立主題argoeventbus,並在主題詳情頁面的存取點地區擷取Endpoint。
使用Resource Access Management員登入RAM控制台。
-
建立RAM使用者,授權
AliyunMNSFullAccess許可權,並擷取RAM使用者的AK和SK。 -
執行以下命令,建立Secret用於儲存AK和SK。
kubectl create secret generic mns-secret\ --from-literal=accesskey=*** \ --from-literal=secretkey=*** -
建立
event-bus-mns.yaml檔案,EventBus範例程式碼如下所示:apiVersion: argoproj.io/v1alpha1 kind: EventBus metadata: name: default spec: alimns: accessKey: key: accesskey name: mns-secret secretKey: key: secretkey name: mns-secret topic: argoeventbus # 對應輕量訊息佇列(原 MNS)中的主題名稱。 endpoint: http://165***368.mns.<region>.aliyuncs.com -
執行以下命令,建立
event-bus-mns.yaml。kubectl apply -f event-bus-mns.yaml
步驟二:建立Event Source
使用Resource Access Management員登入RAM控制台。
-
建立RAM使用者,為其授予
AliyunMNSFullAccess許可權,並擷取RAM使用者的AK和SK。具體操作,請參見建立RAM使用者、管理RAM使用者的許可權、建立AccessKey和查看RAM使用者的AccessKey資訊。 -
建立
event-source.yaml檔案,Event Source範例程式碼如下所示:-
queue:需替換為輕量訊息佇列(原 MNS)的名稱。 -
endpoint:需替換為輕量訊息佇列(原 MNS)的存取點。
apiVersion: argoproj.io/v1alpha1 kind: EventSource metadata: name: ali-mns spec: mns: example: jsonBody: true accessKey: key: accesskey name: mns-secret secretKey: key: secretkey name: mns-secret queue: oss-event-queue # 輕量訊息佇列(原 MNS)名稱。 waitTimeSeconds: 20 endpoint: http://165***368.mns.<region>.aliyuncs.com # 輕量訊息佇列(原 MNS)存取點。 -
-
執行以下命令,建立Event Source。
kubectl apply -f event-source.yaml -
執行以下命令,查看Event Source Pod是否正常啟動。
kubectl get pod
步驟三:建立Event Sensor
-
建立
event-sensor.yaml檔案,在Event Sensor中嵌入待執行的工作流程定義。範例程式碼如下所示: -
執行以下命令,建立Event Sensor。
kubectl apply -f event-sensor.yaml -
執行以下命令,查看Event Sensor Pod是否正常啟動。
kubectl get pod
使用輕量訊息佇列(原 MNS)方式建立Eventbus時,在Event Sensor建立完成後,會自動建立一個輕量訊息佇列(原 MNS)與之對應,隊列命名格式為:ackone-argowf-<namespace>-<sensor-name>-<sensor-uid>。
步驟四:驗證向OSS上傳檔案觸發工作流程
登入OSS管理主控台。
-
向OSS Bucket中上傳以下2個檔案(該檔案需自備),觸發工作流程運行。
-
datafile:資料檔案,文字格式設定,內容自訂。
-
datafile.complete:trigger檔案,可以是空檔案。
-
-
執行以下命令,在工作流程叢集中查看工作流程運行情況。
argo list預期輸出如下:
NAME STATUS AGE DURATION PRIORITY process-oss-file-kmb4k Running 13s 13s 0 -
執行以下命令,擷取工作流程日誌。
argo logs process-oss-file-kmb4k重要-
該命令中的工作流程名稱必須和上一步驟中返回的工作流程名稱一致,
ali-mns-workflow-5prz7僅為樣本值,請您修改為實際環境中的傳回值。 -
訊息內容使用Base64編碼。
預期輸出如下:
.kube % argo logs process-oss-file-vtdfb process-oss-file-vtdfb-parse-event-body- : time="2023-12-14T08:09:25.816Z" level=info msg="capturing logs" argo=true process-oss-file-vtdfb-parse-event-body- : Event body: process-oss-file-vtdfb-parse-event-body- : {"events": [{ process-oss-file-vtdfb-parse-event-body- : "eventName": "ObjectCreated:PostObject", process-oss-file-vtdfb-parse-event-body- : "eventSource": "acs:oss", process-oss-file-vtdfb-parse-event-body- : "eventTime": "2023-12-14T08:00:21.000Z", process-oss-file-vtdfb-parse-event-body- : "eventVersion": "1.0", process-oss-file-vtdfb-parse-event-body- : "oss": { process-oss-file-vtdfb-parse-event-body- : "bucket": { process-oss-file-vtdfb-parse-event-body- : "arn": "acs:oss:cn-hangzhou:xxx:bucket-workflow-artifact xxx", process-oss-file-vtdfb-parse-event-body- : "name": "bucket-workflow-artifactxxx", process-oss-file-vtdfb-parse-event-body- : "ownerIdentity": "xxx", process-oss-file-vtdfb-parse-event-body- : "virtualBucket": ""}, process-oss-file-vtdfb-parse-event-body- : "object": { process-oss-file-vtdfb-parse-event-body- : "deltaSize": 0, process-oss-file-vtdfb-parse-event-body- : "eTag": "DA6xxx", process-oss-file-vtdfb-parse-event-body- : "key": "data.complete", process-oss-file-vtdfb-parse-event-body- : "objectMeta": {"mimeType": "application/octet-stream"}, process-oss-file-vtdfb-parse-event-body- : "size": 14}, process-oss-file-vtdfb-parse-event-body- : "ossSchemaVersion": "1.0", process-oss-file-vtdfb-parse-event-body- : "ruleId": "process-data"}, process-oss-file-vtdfb-parse-event-body- : "region": "cn-hangzhou", process-oss-file-vtdfb-parse-event-body- : "requestParameters": {"sourceIPAddress": "106.11.xxx"}, process-oss-file-vtdfb-parse-event-body- : "responseElements": {"requestId": "657AB6155Cxxx"}, process-oss-file-vtdfb-parse-event-body- : "userIdentity": {"principalId": "165304082xxx"}}]} process-oss-file-vtdfb-parse-event-body- : TriggerFileName from event is data.complete process-oss-file-vtdfb-parse-event-body- : DataFileName after cutting .complete is data, and pass file name to next step process-oss-file-vtdfb-parse-event-body- : time="2023-12-14T08:09:26.821Z" level=info msg="sub-process exited" argo=true error="<nil>" process-oss-file-vtdfb-parse-event-body- : time="2023-12-14T08:09:26.821Z" level=info msg="/tmp/file-name.txt -> /var/run/argo/outputs/parameters//tmp/file-name.txt" argo=true process-oss-file-vtdfb-process-file : time="2023-12-14T08:09:46.262Z" level=info msg="capturing logs" argo=true process-oss-file-vtdfb-process-file : Show data-file: process-oss-file-vtdfb-process-file : -rw-r------ 1 root root 3 Dec 14 08:00 /mnt/vol/data process-oss-file-vtdfb-process-file : Content of data file: process-oss-file-vtdfb-process-file : pi process-oss-file-vtdfb-process-file : process-oss-file-vtdfb-process-file : Finished process-oss-file-vtdfb-process-file : time="2023-12-14T08:09:47.267Z" level=info msg="sub-process exited" argo=true error="<nil>" process-oss-file-vtdfb-sparkapp- : time="2023-12-14T08:10:42.033Z" level=info msg="capturing logs" argo=true process-oss-file-vtdfb-sparkapp- : WARNING: An illegal reflective access operation has occurred process-oss-file-vtdfb-sparkapp- : WARNING: Illegal reflective access by org.apache.spark.unsafe.Platform (file:/opt/spark/jars/spark-unsafe_2.12-3.2.1.jar) to constructor java.nio.DirectByteBuffer process-oss-file-vtdfb-sparkapp- : WARNING: Please consider reporting this to the maintainers of org.apache.spark.unsafe.Platform process-oss-file-vtdfb-sparkapp- : WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations process-oss-file-vtdfb-sparkapp- : WARNING: All illegal access operations will be denied in a future release process-oss-file-vtdfb-sparkapp- : 23/12/14 08:10:45 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable process-oss-file-vtdfb-sparkapp- : Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties process-oss-file-vtdfb-sparkapp- : 23/12/14 08:10:45 INFO SparkKubernetesClientFactory: Auto-configuring K8S client using current context from users K8S config.file process-oss-file-vtdfb-sparkapp- : 23/12/14 08:10:46 INFO KerberosConfDriverFeatureStep: You have not specified a krb5.conf file locally or via a ConfigMap. Make sure that you have the krb5.conf l the driver image. process-oss-file-vtdfb-sparkapp- : 23/12/14 08:10:48 INFO LoggingPodStatusWatcherImpl: State changed, new state: process-oss-file-vtdfb-sparkapp- : pod name: sparkapp-xxx-driver process-oss-file-vtdfb-sparkapp- : namespace: default process-oss-file-vtdfb-sparkapp- : labels: spark-app-selector -> spark-796bd3axxx, spark-role -> driver process-oss-file-vtdfb-sparkapp- : pod uid: 3d63bb8d -
步驟五:清除Event相關資源
-
依次執行以下命令,清除Event相關資源。
kubectl delete sensor process-oss-file kubectl delete eventsource ali-mns kubectl delete eventbus default -
執行以下命令查看Pod,確認所有資源已清除。
kubectl get pod