All Products
Search
Document Center

Container Service for Kubernetes:Use MnsOss event sources in Knative

Last Updated:Nov 07, 2024

In Knative, you can create an MnsOss event source to collect Object Storage Service (OSS) events and send them to Simple Message Queue (SMQ, formerly MNS) as the receiving endpoint. Then, Simple Message Queue will trigger the execution of Knative Services or functions. This solution can be used in scenarios such as facial recognition.

Prerequisites

  • Knative Serving and Knative Eventing are installed. For more information, see Deploy Knative and Deploy Knative Eventing.

  • An OSS bucket is created in the OSS console. For more information, see Create buckets.

  • SMQ is activated and the RAM user that you use is authorized to access SMQ. For more information, see Activate SMQ and authorize RAM users to access SMQ.

    SMQ is an efficient, reliable, secure, convenient, and scalable distributed messaging service. It allows developers to transfer data and messages between distributed components of applications to build loosely coupled systems.

Step 1: Deploy MnsOss

  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, find MnsOss and click Deploy in the Actions column. Follow the on-screen instructions to install the component.

Step 2: Configure OSS event notification settings

  1. Log on to the OSS console.

  2. In the left-side navigation pane, click Buckets. On the Buckets page, click the bucket that you want to use.

  3. In the left-side navigation pane of the bucket details page, choose Data Processing > Event Notification. Then, click Create Rule.

  4. In the Create Rule panel, set the following parameters, and click OK.

    Parameter

    Description

    Rule Name

    Specify a name for the event notification rule.

    The name of each event notification rule that is created by using the same Alibaba Cloud account must be unique in the same region. The name of an event notification rule must start with a letter and can contain only letters, digits, and hyphens (-). The name cannot exceed 85 characters in length.

    Event Type

    Select the event types that trigger notifications for specific objects. For example, if you want to receive a notification when a specific object is created or overwritten by copying an object, select CopyObject.

    You can configure an event notification rule for a specific object and specify multiple event types that can trigger the rule. You can also configure multiple event notification rules for an object. When you configure multiple event notification rules, take note of the following items:

    • If multiple event notification rules apply to the same object, the values of this parameter in these rules must be different. For example, if you select CopyObject for Events when you create a n event notification rule for objects whose names are prefixed with images, you cannot select CopyObject for Events when you create another event notification rule that applies to one of the objects whose names are prefixed with images.

    • If multiple event notification rules apply to different objects, the values of this parameter in these rules can be the same. For example, if you select PutObject for Events when you create an event notification rule for objects whose names are prefixed with images and suffixed with .png, you can select PutObject or DeleteObject for Events when you create another event notification rule for objects whose names are prefixed with log and suffixed with .jpg.

    Important

    If you do not specify the version ID when you delete an object from a versioned bucket, the DeleteObject or DeleteObjects event notification is not triggered. In this case, no version of the object is deleted. The current version of the object is converted into a previous version and a delete marker is added to the object.

    For more information about the object operations that correspond to the event types, see Events.

    Resource Description

    Specify the objects to which you want to apply the event notification rule.

    • Select Full Name to apply the rule to an object whose name matches the specified name.

      • To create a rule that applies to an object named exampleobject.txt in the root directory of the bucket, enter exampleobject.txt.

      • To create a rule that applies to an object named myphoto.jpg in the destdir directory within the root directory of the bucket, enter destdir/myphoto.jpg.

    • Select Prefix and Suffix to apply the rule to objects whose names contain the specified prefix and suffix.

      • To create a rule that applies to all objects in the bucket, leave Prefix and Suffix empty.

      • To create a rule that applies to all objects in the examplefolder directory within the root directory of the bucket, set Prefix to examplefolder/ and leave Suffix empty.

      • To create a rule that applies to all JPG objects in the bucket, leave Prefix empty and set Suffix to .jpg.

      • To create a rule that applies to all MP3 objects in the examplefolder directory within the root directory of the bucket, set Prefix to examplefolder/ and Suffix to .mp3.

    To create another Resource Description entry, click Add. You can create up to five Resource Description entries.

    Endpoint

    Specify the endpoint to which notifications are sent. Valid value: HTTP and Queue.

    • HTTP: Specify an HTTP endpoint. Example: http://198.51.100.1:8080. For more information about how to obtain an HTTP endpoint, see Manage topics and HttpEndpoint.

    • Queue: Enter the name of a Simple Message Queue (formerly MNS). For more information about how to create a queue, see Get started with queue-based messaging.

    To create another endpoint, click Add. You can create up to five endpoints.

  5. Click OK.

    After you configure the event notification rule, the rule takes effect after approximately 10 minutes. After you configure the OSS event notification settings, a topic is created on the Topics page in the SMQ console.

Step 3: Create a SMQ token

  1. Log on to the SMQ console.

  2. In the left-side navigation pane, choose Topic Model > Topics.

  3. In the top navigation bar, select a region.

  4. On the Topics page, click the topic that you want to manage. In the Endpoint section of the Topic Details page, copy the public endpoint for subsequent use.

  5. Obtain an AccessKey ID and AccessKey secret of the current account. For more information, see How do I obtain an AccessKey pair?

  6. Run the following command to encode the public endpoint, AccessKey ID, and AccessKey secret by using Base64. Then, a token is generated.

    echo '{ "url":"https://xxxx.mns.cn-shanghai.aliyuncs.com/", "accessKeyId":"xxx","accessKeySecret":"xx" }' | base64
  7. Create a Secret to store and manage the token.

    1. Create a file named mnsoss-secret.yaml.

      apiVersion: v1
      kind: Secret
      metadata:
        name: mnsoss-secret
      type: Opaque
      data:
        mns: eyAidXJsIjoiaHR0cHM6Ly94eHh4Lm1ucy5jbi1zaGFuZ2hhaS5hbGl5dW5jcy5jb20vIiwgImFjY2Vzc0tleUlkIjoieHh4IiwiYWNjZXNzS2V5U2VjcmV0IjoieHgi****

      Replace the value of mns with the token that is generated in Step 6.

    2. Run the following command to create a Secret:

      kubectl apply -f mnsoss-secret.yaml

Step 4: Create a service account and a service broker

  1. Use the following YAML template to create a file named mnsoss-sa.yaml. This will create a service account.

    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRoleBinding
    metadata:
      name: eventing-sources-mnsoss
    subjects:
    - kind: ServiceAccount
      name: mnsoss-sa
      namespace: default
    roleRef:
      apiGroup: rbac.authorization.k8s.io
      kind: ClusterRole
      name: eventing-sources-mnsoss-controller
    ---
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: mnsoss-sa
    kubectl apply -f mnsoss-sa.yaml
  1. Run the following command to create a service broker:

    kubectl label namespace default knative-eventing-injection=enabled

Step 5: Create an MnsOss event source

To receive MnsOss events, you must create an MnsOss event source.

  1. Create a file named mnsoss-source.yaml.

    apiVersion: sources.eventing.knative.dev/v1alpha1
    kind: MnsOssSource
    metadata:
      labels:
        controller-tools.k8s.io: "1.0"
      name: mnsoss-face
    spec:
      # Add fields here
      serviceAccountName: mnsoss-sa
      accessToken:
        secretKeyRef:
          name: mnsoss-secret
          key: mns
      sink:
        apiVersion: eventing.knative.dev/v1alpha1
        kind: Broker
        name: default
      topic: mns-en-topics-oss-face-image-2381221888dds9129 # The name of the topic that is generated in the SMQ console.
  2. Run the following command to create an MnsOss event source:

    kubectl  apply -f mnsoss-source.yaml

Step 6: Create a Knative Service

Create a Knative Service to check whether the created MnsOss event source works as normal. In this example, a Knative Service named event-display is created.

  1. Create a service.yaml file.

    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: event-dispaly
      namespace: default
    spec:
      template:
        spec:
          containers:
          - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/event-display:1215
  2. Run the following command to create a Knative Service:

    kubectl apply -f service.yaml

Step 7: Create a trigger

Create a trigger to subscribe to OSS events.

  1. Create a file named trigger.yaml.

    apiVersion: eventing.knative.dev/v1alpha1
    kind: Trigger
    metadata:
      name: oss-trigger
      namespace: default
    spec:
      subscriber:
        ref:
          apiVersion: serving.knative.dev/v1alpha1
          kind: Service
          name: event-dispaly
  2. Run the following command to create a trigger:

    kubectl apply -f trigger.yaml

Result

When you upload files to the OSS bucket, notifications are sent to pods.

2020/12/16 13:04:19 receive cloudevents.Event:
{"events": [{
            "eventName": "ObjectCreated:PostObject",
            "eventSource": "acs:oss",
            "eventTime": "2019-06-18T06:44:16.000Z",
            "eventVersion": "1.0",
            "oss": {
                "bucket": {
                    "arn": "acs:oss:cn-beijing:1041208914252405:testjian",
                    "name": "testjian",
                    "ownerIdentity": "1041208914252405",
                    "virtualBucket": ""},
                "object": {
                    "deltaSize": 0,
                    "eTag": "137138904F2E18D307D04EB38EA44CDA",
                    "key": "timg.jpg",
                    "size": 12990},
                "ossSchemaVersion": "1.0",
                "ruleId": "demo-i****"},
            "region": "cn-beijing",
            "requestParameters": {"sourceIPAddress": "42.120.7*.***"},
            "responseElements": {"requestId": "5D08884070BC12B192C6****"},
            "userIdentity": {"principalId": "104120891425****"}}]}

References

For more information about how to use other event sources, see Use Knative to manage GitHub events and Use EventBridge to trigger Knative Services to consume events.