×
Community Blog Event-driven Practice of Knative: Trigger Events Through EventBridge

Event-driven Practice of Knative: Trigger Events Through EventBridge

This article describes how to use EventBridge's event to trigger a Knative service, using the example event of uploading files to Object Storage Service (OSS).

By Peng Li (Yuanyi)

Knative is a serverless framework that is built on Kubernetes. Its main objective is to create a cloud-native and cross-platform orchestration standard for serverless containers. This serverless standard is implemented by Knative through the integration of container building (or function), workload management (dynamic scaling), and event driving.

To provide Knative with production-level event-driven capability, one can leverage the use of the event bus EventBridge. EventBridge is a serverless event bus service offered by Alibaba Cloud. It supports Alibaba Cloud services, custom applications, and software-as-a-service (SaaS) applications in a standard and centralized manner. Furthermore, EventBridge can route events between these services and applications using the standard CloudEvents 1.0 specification. Developers can build loosely coupled and distributed event-driven architectures using EventBridge.

This article describes how to use EventBridge's event to trigger a Knative service, using the example event of uploading files to Object Storage Service (OSS).

Technical Architecture

EventBridge supports multiple data sources. You can trigger Knative services by configuring event buses, event rules, and event targets to filter and transform events. By utilizing EventBridge's event-driven capability to consume events, you can use resources on demand. The following architecture diagram illustrates the process:

1

Prerequisites

• You have activated EventBridge and granted permissions to a RAM user.
• You have activated the Object Storage Service (OSS). Click here to learn more about the events supported by OSS.
• You have deployed Knative.

Procedure

Step 1: Create a Knative Service

In this section, we will use an example Knative Service called "event-display," which directly prints the content of the received event.

1.  Create a service.yaml file.

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

2.  Run the following command to create a Knative Service:

kubectl apply -f service.yaml

3.  Verify the service

$ kubectl get ksvc
NAME            URL                                        LATESTCREATED         LATESTREADY           READY   REASON
event-display   http://event-display.default.example.com   event-display-00009   event-display-00009   True

Step 2: Create a Connection Configuration

1.  Log on to the EventBridge console. In the left-side navigation pane, choose Integration Center > API Endpoint.

2.  In the top navigation bar, select the region where you want to configure the connection. Then, click the Connection Configuration tab.

3.  On the Connection Configuration tab, click Create. In the Create Connection panel, configure the following parameters and click OK.

  • Connection Name: Configure the name of the connection.
  • Connection Description: Configure connection description.
  • Authentication Configuration: Select Optional.
  • Network: Internet is selected by default.

Step 3: Create an API Endpoint

1.  After the connection configuration is created, click the API Endpoint tab and click Create.

2.  In the Create API Endpoint panel, configure the following parameters and click OK.

  • In the Basic Information section, set the Method parameter to Custom. Then, specify the name and description of the API destination.
  • In the API Configuration section, set URL to the Knative gateway address. (If the ALB gateway is used, set the ALB DNS domain name. If the Kourier gateway is used, set the SLB IP address). In this example, the http://alb-1ag1spsdeze7m1eysm.cn-hangzhou.alb.aliyuncs.com is used to access the Knative service. Set HTTP Method to POST.
  • In the Connection Configuration section, click Select Existing Connection and select the connection that you created in step 2.

Step 4: Create an Event Rule

  1. In the left-side navigation pane, click Event Buses. On the Event Buses page, select the default bus because the OSS event source is used.
  2. In the Default Bus panel, set the following parameters and click Create.

a) In the Event Rules tab, configure Name and Description and click Next Step.

b) Configure the event mode and select an official Alibaba Cloud event source.

  • Event source: acs.oss

    • Event type: oss:ObjectCreated:PutObject.

a) Configure the event target, and set the following parameters.

  • Service Type: Select Triggered by API Operation.

    • Method: Select Use Existing Item.
    • HTTP Parameters: Configure parameters and the parameter content that are transmitted when the event is pushed to the Knative service.
  • Header: The key is the fixed value Host, and the value is the domain name of the Knative service (event-dispaly.default.example.com ).

2

Verify the Configuration Results

Upload files through OSS to trigger events. Here we upload a demo.data file.

ossutil cp demo.data oss://knative

Log on to the EventBridge console. On the Event Tracking page, you can view the event trace. The event is delivered.

3

View the Knative service in kubectl. You can see that the automatic trigger creates a new pod.

$ kubectl get  po
NAME                                              READY   STATUS    RESTARTS         AGE
event-display-00009-deployment-56cc79666d-z2vhv   2/2     Running   0                7s

The event content has been printed.

$ kubectl logs event-display-00009-deployment-56cc79666d-z2vhv user-container
{"data":{"eventVersion":"1.0","responseElements":{"requestId":"63E21F5FEE852133319101AD"},"eventSource":"acs:oss","eventTime":"2023-02-07T09:52:31.000Z","requestParameters":{"sourceIPAddress":"106.11.167.116"},"eventName":"ObjectCreated:PutObject","userIdentity":{"principalId":"1118324452360952"},"region":"cn-hangzhou","oss":{"bucket":{"name":"knative","arn":"acs:oss:cn-hangzhou:1581204543170042:knative","virtualBucket":"","ownerIdentity":"1581204543170042"},"ossSchemaVersion":"1.0","object":{"size":225496,"objectMeta":{"mimeType":"application/octet-stream"},"deltaSize":0,"eTag":"B350C082843DAC7E9E634193437EBA30","key":"demo.data"}}}}

Summary

We can see that EventBridge can be seamlessly integrated with Knative, making it easy for Knative to trigger automatic scaling based on events. We expect to further integrate with EventBridge to lower the threshold for configuration.

0 1 0
Share on

Alibaba Container Service

120 posts | 26 followers

You may also like

Comments

Alibaba Container Service

120 posts | 26 followers

Related Products