All Products
Search
Document Center

Container Service for Kubernetes:Use the event-driven capabilities of EventBridge in Knative

Last Updated:Dec 09, 2024

Knative Eventing provides an event model to simplify the process of ingesting events from external event systems. You can deploy the Knative Eventing components in an Container Service for Kubernetes (ACK) cluster and create triggers to enable the event-driven capabilities. After an event is accessed, it is forwarded in the system in the standard CloudEvents format within the system. This topic describes how to use EventBridge to provide the event-driven capabilities in a production environment for Knative.

Prerequisites

  • The version of Knative Serving is 1.12.7 or later.

  • ossutil is installed and a bucket is created. For more information, see Install ossutil and Create a bucket.

  • 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: Deploy the Eventing and EventBridge components

Knative provides the event-driven capabilities of EventBridge and supports Alibaba Cloud service event sources and event forwarding.

  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 navigation pane, choose Applications > Knative.

  3. On the Components tab, deploy the Eventing and EventBridge components.

    1. On the Components tab, find Eventing and click Deploy in the Actions column.

    2. After Eventing is deployed, find EventBridge, and click Deploy in the Actions column. In the dialog box that appears, enter the AccessKey ID and AccessKey secret, and then click OK.

      For more information about how to obtain an AccessKey pair, see Create an AccessKey pair.

      Important

      To ensure the security of your account, we recommend that you perform the following operations at the earliest opportunity:

      • Follow the on-screen instructions to change the password of the account administrator. By default, you need to change the password of a RAM user upon the first logon.

      • We recommend that you bind a multi-factor authentication (MFA) device to the account administrator for secondary authentication. MFA is a security enhancement that adds an extra layer of protection in addition to the username and password. For more information, see Bind an MFA device to a RAM user.

    If Deployed is displayed in the Status column of the component, the component is deployed.

Step 2: Create a Knative Service

This section uses a Knative Service named event-display as an example. The Service automatically records all received events. The following content describes how to create a Knative Service in the ACK console and kubectl.

Use the ACK console

  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 navigation pane, choose Applications > Knative.

  3. Click the Services tab and then click Create from Template to create a Knative Service.

    The following sample code provides an example:

    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

    On the Services tab, if Created is displayed in the Status column of the Knative Service, the Knative Service is created.

Use kubectl

  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 created:

    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    

Step 3: Create a trigger

Use the ACK console

  1. On the Knative page, click the Services tab, and then click the name of the Knative Service. On the page that appears, click the Trigger tab.

  2. Click Create Trigger. On the Configure page, configure the parameters based on the instructions. The following table describes the parameters.

    Parameter

    Description

    Example

    Name

    Enter a name for the trigger.

    my-service-trigger

    Broker

    You can select EventBridge or Other.

    EventBridge

    Event Source

    Multiple event sources are supported.

    OSS

    Event Type

    Multiple event types are supported.

    oss:ActionTrail:ConsoleOperation

    After the trigger is created, you can view the trigger on the Triggers tab.

    image

Use kubectl

  1. Create a file named my-service-trigger.yaml, copy the following content to the file, and then deploy the file to the cluster. The file is used to create a trigger.

    apiVersion: eventing.knative.dev/v1
    kind: Trigger
    metadata:
      name: my-service-trigger
    spec:
      broker: eventbridge-default-broker
      filter:
        attributes:
          source: acs.oss
          type: 'oss:ActionTrail:ConsoleOperation'
      subscriber:
        ref:
          apiVersion: serving.knative.dev/v1
          kind: Service
          name: helloworld-go
          namespace: default
    • broker: Set the name to eventbridge-default-broker, which corresponds to the default event bus.

    • source: Specify the event sources supported by EventBridge.

    • type: Specify the event type of the event source. Separate multiple event types with commas (, ).

    • subscriber: Specify the subscribed service.

    kubectl apply -f my-service-trigger.yaml
  2. Run the following command to check whether the trigger is created:

     kubectl get triggers

    Expected output:

    NAME                         BROKER                       SUBSCRIBER_URI                                   AGE   READY   REASON
    my-service-trigger           eventbridge-default-broker   http://helloworld-go.default.svc.cluster.local   42h   True   

Step 4: Check whether EventBridge can trigger the Knative Service to consume events

Before you perform the following steps, you must 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, configure the Time Range parameter. 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:

      {"data":{"eventVersion":"1.0","responseElements":{"requestId":"63E21F5FEE852133319101AD"},"eventSource":"acs:oss","eventTime":"2023-02-07T09:52:31.000Z","requestParameters":{"sourceIPAddress":"XX.XXX.XX.XXX"},"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"}}}}

    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.