Use Simple Message Queue (formerly MNS) to trigger a workflow
Workflow Clusters can integrate with Alibaba Cloud Simple Message Queue (formerly MNS). Use Simple Message Queue (formerly MNS) as a broker to connect to a variety of event sources and trigger workflow runs based on events. When a new event occurs, such as an Object Storage Service (OSS) event or an EventBridge event, Simple Message Queue (formerly MNS) receives the corresponding message. Argo automatically triggers the workflow run based on the trigger conditions, which automates workflow processing.
Prerequisites
Activate the following services and features.
Simple Message Queue (formerly MNS): Activation and permissions. This feature incurs charges for Simple Message Queue (formerly MNS). For billing details, see Billing.
Create a workflow cluster and download the Alibaba Cloud Argo CLI.
Step 1: Create an event bus
An event bus can be shared by event-driven workflows in a namespace. You can create an event bus by using NATS or Simple Message Queue (formerly MNS). If an event bus already exists, proceed directly to Step 2: Create an event source.
Create by using NATS
-
Create an
event-bus.yamlfile. Sample configuration:apiVersion: argoproj.io/v1alpha1 kind: EventBus metadata: name: default spec: nats: native: replicas: 3 auth: token -
Run the following command to create the EventBus:
kubectl apply -f event-bus.yamlNoteAfter the command succeeds, an EventBus pod is created in the
defaultnamespace. Subsequent operations must be performed in the same namespace. -
Run the following command to verify that the EventBus pod has started:
kubectl get pod
Create by using Simple Message Queue (formerly MNS)
Log on to the SMQ console.
On the Topics page, create a topic named argoeventbus. Then, on the Topic Details page, obtain the endpoint from the Access Point section.
-
Log on to the RAM console as a RAM administrator.
Create a Resource Access Management (RAM) user, grant the
AliyunMNSFullAccesspermission to the RAM user, and obtain the RAM user's AccessKey ID and AccessKey secret.For more information, see Create a RAM user, Manage RAM user permissions, Create an AccessKey, and View RAM user AccessKey information.
Run the following command to create a Secret for storing the AccessKey ID and AccessKey secret.
kubectl create secret generic mns-secret\ --from-literal=accesskey=*** \ --from-literal=secretkey=***Create an
event-bus-mns.yamlfile and replace the parameters in the example with the values for your environment.topic: Replace the value with the name of the topic created in step 2 of Simple Message Queue (formerly MNS).endpoint: Replace the value with the endpoint obtained in step 2.
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 # The name of the topic in Simple Message Queue (formerly MNS). endpoint: http://165***368.mns.<region>.aliyuncs.com # The endpoint of Simple Message Queue (formerly MNS).Run the following command to apply the
event-bus-mns.yamlfile and create the event bus resource.kubectl apply -f event-bus-mns.yaml
Step 2: Create an event source
Log on to the SMQ console.
On the Queues page, create a queue named test-event-queue. Then, on the Queue Details page, obtain the endpoint from the Access Point section.
NoteIf you created the event bus by using Simple Message Queue (formerly MNS), you can skip steps 3 through 5 and proceed directly to step 6.
-
Log on to the RAM console as a RAM administrator.
Create a Resource Access Management (RAM) user, grant the
AliyunMNSFullAccesspermission to the RAM user, and obtain the RAM user's AccessKey ID and AccessKey secret.For more information, see Create a RAM user, Manage RAM user permissions, Create an AccessKey, and View RAM user AccessKey information.
Run the following command to create a Secret for storing the AccessKey ID and AccessKey secret.
kubectl create secret generic mns-secret\ --from-literal=accesskey=*** \ --from-literal=secretkey=***Create an
event-source.yamlfile and replace the parameters in the example with the values for your environment.topic: Replace the value with the name of the topic created in step 2 of Simple Message Queue (formerly MNS).endpoint: Replace the value with the endpoint obtained in step 2.
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: test-event-queue # The name of the queue in Simple Message Queue (formerly MNS). waitTimeSeconds: 20 endpoint: http://165***368.mns.<region>.aliyuncs.com # The endpoint of Simple Message Queue (formerly MNS).Run the following command to apply the
event-source.yamlfile and create the event source.kubectl apply -f event-source.yamlRun the following command to check whether the event source Pod starts as expected.
kubectl get pod
Step 3: Create an event sensor
Create an
event-sensor.yamlfile and embed the workflow definition to run in the event sensor. The following sample code shows the event sensor configuration:Apply the
event-sensor.yamlfile to create the event sensor.kubectl apply -f event-sensor.yamlCheck whether the event sensor Pod starts as expected.
kubectl get pod
When you create an event bus by using Simple Message Queue (formerly MNS), a corresponding Simple Message Queue (formerly MNS) queue is automatically created after the event sensor is created. The queue name uses the following format: ackone-argowf-<namespace>-<sensor-name>-<sensor-uid>.
Step 4: Verify that sending a message to Simple Message Queue (formerly MNS) triggers the workflow
Log on to the SMQ console.
On the Queues page, find the queue named test-event-queue. In the Actions column, click Send Messages.
On the quick-start page for sending and receiving messages, enter
test trigger argo workflowas the message content and then click Send Message.View the workflow run in the workflow cluster.
argo listThe expected output is as follows:
NAME STATUS AGE DURATION PRIORITY ali-mns-workflow-5prz7 Running 6s 6s 0Obtain the workflow logs to view the message content.
argo logs ali-mns-workflow-5prz7ImportantThe workflow name in this command must match the name returned in the previous step.
ali-mns-workflow-5prz7is only an example value. Replace it with the value returned in your environment.The message content is Base64-encoded.
The expected output is as follows:
ali-mns-workflow-5prz7-whalesay-2429203954: time="2023-12-14T08:33:37.964Z" level=info msg="capturing logs" argo=true ali-mns-workflow-5prz7-whalesay-2429203954: ali-mns-workflow-5prz7-whalesay-2429203954: < dGVzdCB0cmlnZ2VyIGFyZ28gd29ya2Zsb3c= > ali-mns-workflow-5prz7-whalesay-2429203954: ----------------------------------------- ali-mns-workflow-5prz7-whalesay-2429203954: \ ali-mns-workflow-5prz7-whalesay-2429203954: \ ali-mns-workflow-5prz7-whalesay-2429203954: \ ali-mns-workflow-5prz7-whalesay-2429203954: ## . ali-mns-workflow-5prz7-whalesay-2429203954: ## ## ## == ali-mns-workflow-5prz7-whalesay-2429203954: ## ## ## ## === ali-mns-workflow-5prz7-whalesay-2429203954: /""""""""""""""""___/ === ali-mns-workflow-5prz7-whalesay-2429203954: ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~ ali-mns-workflow-5prz7-whalesay-2429203954: \______ o __/ ali-mns-workflow-5prz7-whalesay-2429203954: \ \ __/ ali-mns-workflow-5prz7-whalesay-2429203954: \____\______/ ali-mns-workflow-5prz7-whalesay-2429203954: time="2023-12-14T08:33:38.979Z" level=info msg="sub-process exited" argo=true error="<nil>"
Step 5: Delete event-related resources
Delete the related resources.
Delete the event sensor.
kubectl delete sensor ali-mnsDelete the event source.
kubectl delete eventsource ali-mnsDelete the event bus.
kubectl delete eventbus default
Check the Pod status to confirm that all resources have been deleted.
kubectl get pod