All Products
Search
Document Center

Container Service for Kubernetes:Use EventBridge to trigger Knative Services to consume events

Last Updated:Jun 05, 2024

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

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.

Note

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.

image

Step 1: Create a Knative Service

In this example, a Knative Service named event-display is used. The Service automatically records all received events.

  1. 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
  2. Run the following command to deploy the Knative Service:

    kubectl apply -f event-display.yaml
  3. 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    
  4. 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.

  2. In the left-side navigation pane, choose Integration Center > API Destination. In the top navigation bar, select a region.

  3. Click the Connection Configuration tab and click Create.

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

  5. Click the API Destination tab and click Create. The Create API Destination panel appears.

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

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

    Note

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

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

Step 3: Create an event rule

  1. Log on to the EventBridge console.

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

  3. On the Event Rules page, click Create Rule.

  4. In the Configure Basic Info step, specify the name and description of the rule and click Next Step.

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

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

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

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

  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.

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

    3. On the Query by Time Range tab, set the Time Range parameter to Last hour and click Query.

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

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