EventBridge is a serverless event bus service provided by Alibaba Cloud. EventBridge can help you develop event-driven applications for production in Knative. This topic describes how to use EventBridge to trigger a Knative Service to consume events. In this topic, Object Storage Service (OSS) events are used.
Prerequisites
EventBridge is activated and you are granted the permissions to use the EventBridge console. For more information, see Activate EventBridge and grant permissions to a RAM user.
Knative is deployed in your cluster. For more information, see Deploy Knative in an ACK cluster and Deploy Knative in an ACK Serverless cluster.
The Eventing and Kafka components are deployed for Knative. For more information, see Manage Knative components.
ossutil is installed. For more information, see Install ossutil.
Background Information
Knative is a Kubernetes-based serverless framework. It is intended for creating serverless container orchestration standards for cloud-native applications across platforms. Knative streamlines container builds (or functions), workflow management (dynamic scaling), and eventing to implement serverless standards.
EventBridge can ingest data from Alibaba Cloud services, custom applications, and Software as a service (SaaS) applications in a standardized and centralized manner. EventBridge also allows you to route events among these applications based on the standard CloudEvents 1.0 protocol. You can use EventBridge to build loosely-coupled, distributed event-driven architectures with ease.
Architecture
EventBridge supports various event sources. You can configure event buses, rules, and targets to filter, transform, and deliver events. You can use EventBridge to trigger Knative Services to consume events. This way, you can use resources on demand. The following figure shows the architecture.
You can connect OSS to EventBridge as an event source. For more information about how to connect OSS to EventBridge as an event source, see OSS events.
Step 1: Create a Knative Service
In this example, a Knative Service named event-display is used. The Service automatically records all received events.
Create a file named event-display.yaml and add the following content to the file. The file creates a Knative Service.
apiVersion: serving.knative.dev/v1 kind: Service metadata: name: event-display namespace: default spec: template: spec: containers: - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/event-display:20230207194118_3874dbd
Run the following command to deploy the Knative Service:
kubectl apply -f event-display.yaml
Run the following command to check whether the Knative Service is deployed:
kubectl get ksvc
Expected output:
NAME URL LATESTCREATED LATESTREADY READY REASON event-display http://event-display.default.example.com event-display-00001 event-display-00001 True
Obtain the domain name and gateway address of the Knative Service
Log on to the ACK console. In the left-side navigation pane, click Clusters.
On the Clusters page, find the cluster that you want to manage and click its name. In the left-side pane, choose .
On the Knative page, click the Services tab and obtain the default domain name and gateway address of the Knative Service.
Step 2: Configure a connection and an API destination
Log on to the EventBridge console.
In the left-side navigation pane, choose . In the top navigation bar, select a region.
Click the Connection Configuration tab and click Create.
In the Create Connection panel, configure the Name and Description parameters, set the Authentication parameter to None and the Network parameter to Internet, and then click OK.
Click the API Destination tab and click Create. The Create API Destination panel appears.
In the Basic Information section of the Create API Destination panel, set the Method parameter to Custom and configure the API Destination Name and Description parameters.
In the API Configuration section, set the URL parameter to the Knative gateway address obtained in Step 4 and the HTTP Method parameter to POST.
NoteIf you use an Application Load Balancer (ALB) gateway, set the URL parameter to the domain name of the ALB instance.
In the Connection Configuration section, select Select Existing Connection, select an existing connection, and then click OK.
Step 3: Create an event rule
Log on to the EventBridge console.
In the left-side navigation pane, click Event Buses. On the Event Buses page, find the default event and click Event Rules in the Actions column.
On the Event Rules page, click Create Rule.
In the Configure Basic Info step, specify the name and description of the rule and click Next Step.
In the Configure Event Pattern step, set the Event Source parameter to acs.oss and the Event Type parameter to oss:ObjectCreated:PutObject, and click Next Step.
In the Configure Targets step, configure the parameters and click Create.
Parameter
Description
Service Type
Select Triggered by API Destination.
Method
Select Use Existing Item and select the API destination that you created in Step 2.
HTTP Parameter
Specify the parameter keys and values to be passed when EventBridge delivers events to the Knative Service.
On the Header tab, set the key type to Fixed Value and the key to Host. Then, set the value type to Fixed Value and the value to the Knative Service domain name that you obtained in Step 4.
Click the Body tab, set the key type to Fixed Value, and then set the key to data. Then, set the value type to Partial Event and the value to $.data. For more information about events, see Overview.
Step 4: Check whether EventBridge can trigger the Knative Service to consume events
Before you perform the following steps, you need to install ossutil.
Run the following command to upload a file to OSS.
NoteThe OSS bucket and EventBridge must be deployed in the same region.
ossutil cp <File name> oss://<Bucket name>
After the file is uploaded, EventBridge triggers the Knative Service to consume the event.
View the event trace.
Log on to the EventBridge console.
In the left-side navigation pane, click Event Buses. On the Event Buses page, find the default event and click Event Tracking in the Actions column.
On the Query by Time Range tab, set the Time Range parameter to Last hour and click Query.
Set the Event Source parameter to OSS and the Event Type parameter to Put Object, and click Event Trace in the Operations column of the corresponding event.
On the Event Trace page, you can check whether the event is delivered.
Check whether the Knative Service consumes the event.
Run the following command to check whether the Knative Service consumes the event:
kubectl get pod
Expected output:
NAME READY STATUS RESTARTS AGE event-display-00001-deployment-56cc79****-z2vhv 2/2 Running 0 7s
Run the following command to check whether the Knative Service records the event:
kubectl logs event-display-00001-deployment-56cc79****-z2vhv user-container
Expected output:
Defaulted container "user-container" out of: user-container, queue-proxy { "data":{"eventVersion":"1.0","responseElements":{"requestId":"6524C9F5F326DB333575119B"},"eventSource":"acs:oss","eventTime":"2023-10-10T03:50:13.000Z","requestParameters":{"sourceIPAddress":"101.132.XX.XX"},"eventName":"ObjectCreated:PutObject","userIdentity":{"principalId":"203097492760048007"},"region":"cn-beijing","oss":{"bucket":{"name":"ossutil-demo-liu","arn":"acs:oss:cn-beijing:1159648454668874:ossutil-demo-liu","virtualBucket":"","ownerIdentity":"1159648454668874"},"ossSchemaVersion":"1.0","object":{"size":603050,"objectMeta":{"mimeType":"text/markdown; charset=utf-8"},"deltaSize":0,"eTag":"3298F4DC29D2A0578088FF5810C210B3","key":"RELEASENOTES.md"}}}
The output indicates that the Knative Service consumes and records the OSS upload event. This indicates that EventBridge successfully triggers the Knative Service to consume the event.