All Products
Search
Document Center

Container Service for Kubernetes:Use EventBridge to configure event sources for Knative

Last Updated:Nov 20, 2024

EventBridge can help you develop event-driven applications for production in Knative. You can use EventBridge to configure event buses, rules, and targets. After filtering and transforming events, EventBridge generates events that trigger the execution of Knative Services or functions to consume events. This way, you can use resources on demand.

Prerequisites

  • The Eventing and Kafka components are deployed for Knative. For more information, see Deploy a Knative component.

  • ossutil is installed. For more information, see Install ossutil.

  • 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.

    EventBridge is a serverless event bus service provided by Alibaba Cloud. EventBridge can be accessed from other Alibaba Cloud services, custom applications, and software-as-a-service (SaaS) applications in a standard and centralized manner. In addition, EventBridge can route events between these services and applications based on the standard CloudEvents 1.0 specification. You can use EventBridge to build loosely coupled and distributed event-driven architectures.

Feature introduction

EventBridge supports a wide array of event sources. You can configure event buses, rules, and targets to filter, transform, and deliver events. By using EventBridge to trigger Knative Services to consume events, you can use resources on demand. The following figure shows the architecture.

Note

You can connect Object Storage Service (OSS) to EventBridge as an event source. For more information about how to connect OSS to EventBridge as an event source, see OSS events.

image

Step 1: Create a Knative Service

This section uses a Knative Service named event-display as an example. The Service automatically records all received events.

  1. Create a file named event-display.yaml and copy the following content to the file. The file is used to create 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
    kubectl apply -f event-display.yaml
  2. 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    
  3. Obtain the domain name and gateway address of the Knative service.

    1. Log on to the ACK console. In the left-side navigation pane, click Clusters.

    2. On the Clusters page, find the cluster that you want to manage and click its name. In the left-side pane, choose Applications > Knative.

    3. 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

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

  2. In the top navigation bar, select the region where your cluster is deployed.

  3. Click the Connection Configuration tab. Click Create. In the Create Connection Configuration panel, specify a name and description for the connection, set Authentication to None, set Network to Internet, and then click OK.

  4. Click the API Destination tab. Click Create. In the Basic Information section, set Method to Custom and specify a name and description for the API destination.

  5. In the API Configuration section, set URL to the Knative gateway address obtained in Step 3, such as http://8.141.XX.XX, and HTTP Method to POST.

    Note

    If you use an Application Load Balancer (ALB) gateway, set URL to the domain name of the ALB instance.

  6. In the Connection Configuration section, click Select Existing Connection, select an existing connection, and then click OK.

Step 3: Create a rule

  1. Log on to the EventBridge console. In the left-side navigation pane, click Event Buses.

  2. On the Event Buses page, select the default event and click Event Rules in the Actions column. On the Event Rules page, click Create Rule.

  3. On the Configure Basic Info wizard page, specify a name and description for the rule and click Next Step.

  4. On the Configure Event Pattern wizard page, set Event Source to acs.oss and Event Type to oss:ObjectCreated:PutObject, and click Next Step.

  5. On the Configure Targets wizard page, 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.

    1. 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 you obtained in Step 4.

    2. Click the Body tab. Select Raw Data. Type { "data": "$.data"} in the Parameters code editor and { "data": ${data}} in the Template code editor. Select JSON (application/json) below the code editor. 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.

  1. Run the following command to upload a file to OSS.

    Note

    The 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.

  2. View the event trace.

    1. Log on to the EventBridge console. In the left-side navigation pane, click Event Buses.

    2. On the Event Buses page, select the default event and click Event Tracking in the Actions column. On the Query By Time Range tab, set Time Range to Last Hour. Click Query.

    3. Set Event Source to OSS and Event Type to Put Object, and click Event Trace in the Operations column of the corresponding event.

      image.png

      On the Event Trace page, you can check whether the event is delivered.

  3. Check whether the Knative Service consumes the event.

    1. 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
    2. Run the following command to view the Knative Service records of 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 means that EventBridge successfully triggers the Knative Service to consume the event.

References

For more information about how to use other event sources to enable event-driven capabilities, see Use GitHub events in Knative.