Community Blog Knative: Serverless Practices in AI Event-Driven Scenarios

Knative: Serverless Practices in AI Event-Driven Scenarios

This article describes how to use Knative to implement practices with event-driven framework for users in different scenarios.

By Yuanyi


As the mainstream open-source Serverless Framework, Knative provides application deployment, automatic elasticity, and event-driven capabilities. In AI scenarios, users often have high requirements for event processing and resource cost savings. This article describes how to use Knative to implement practices with event-driven, event distribution, and automatic elasticity, meeting the requirements of users in different scenarios.

Three aspects will be introduced in detail:

  • Event-Driven Framework: Knative Eventing
  • Event-Driven Engine: Event sources
  • Practices in AI Event-Driven Scenarios

Knative Model

There are two core modules in Knative, namely, Serving and Eventing. Serving provides application models with functions of multi-version management, automatic elasticity, and canary release base on traffic through Knative Service. Eventing provides an event-driven framework, including the event sources and Broker and Trigger models. In this article, Knative Eventing is the main focus.


Event-Driven Framework: Eventing

Knative Eventing provides a complete event model, which can access events in various external systems easily. After accessing, events are forwarded internally through the CloudEvent standard, combining the Broker and Trigger mechanism to provide an ideal way to handle events.


  • Knative Eventing provides loosely coupled services and can be developed and deployed independently. Services work across Kubernetes, VMs, SaaS, and FaaS platforms.
  • Event producers and consumers are independent of each other.
  • The community provides a variety of out-of-the-box event sources.
  • The third-party message system is supported for flexible scalability.

Key Features – Event Rules

In Eventing, Broker and Trigger are used for event forwarding and distribution. Distribution cannot be separated from rules, while Trigger provides abundant rule-setting capabilities, as shown in the figure below:


  • The Trigger filter filters the event content.
  • It can filter the attribute of an event and the data content.
  • It can filter the expression of common expression language (CEL).
  • It can filter by SourceAndType.

Event-Driven Engine: Event Sources

Event sources serve as the event-driven engine. The Knative community provides a wide range of event sources, such as Kafka and GitHub. Moreover, event sources of cloud products for message delivery have been accessed as well, such as Message Service (MNS) and RocketMQ.


This complete event system can implement the following event-driven scenarios easily:

  • Automatic CI/CD
  • AI audio and video scenarios
  • Connection with events of various cloud products, so status updates of products trigger a service automatically.

Apache RocketMQ Event Sources

Alibaba Cloud Message Queue for Apache RocketMQ is a distributed messaging middleware based on Apache RocketMQ. It features low latency, high concurrency, high availability, and high reliability. The message queue provides asynchronous decoupling and load shifting for distributed application systems. Furthermore, it supports features for Internet applications, including massive message accumulation, high throughput, and reliable retry.

RocketMQSource is the RocketMQ event source of Knative. It can forward messages of RocketMQ clusters to Knative in real-time in CloudEvent format. It is a connector between Apache RocketMQ and Knative.


  • Access to the RocketMQ message system
  • Distribute messages to Knative Service

Apache Kafka Event Sources

Alibaba Cloud Message Queue for Apache Kafka is a distributed message queue with high-throughput and scalable features based on open-source Apache Kafka. The message queue is widely used in big data scenarios, such as log collection, data aggregation monitoring, streaming data processing, and online and offline analysis. It has become an indispensable part of the big data ecosystem. Alibaba Cloud provides fully hosted services without O&M for users, making the product more professional, reliable, and secure.

The Knative community provides Kafka event sources and supports open-source Kafka and Alibaba Cloud Kafka products:

  • Access to the Kafka message system
  • Distribute messages to Knative Service


Practices in AI Event-Driven Scenarios

Let's take a Kafka event source as an example to introduce a typical message-driven scenario.

The customer has an online livestreaming system with a large number of visitors every day. The number of visits fluctuates elastically based on the popularity of live broadcasts, while also irregular surges exist. Meanwhile, the livestreaming system supports online interaction for users. Interaction behaviors are pushed to the server in real-time by Kafka for data and AI processing. The processing result is pushed back to the video stream in real-time for livestreaming. Message data processing has the following technical challenges:

  1. Elastic business fluctuation with high message concurrency
  2. Real-time interaction response with low latency


Alibaba Cloud Knative was chosen for elastic processing of data to meet the customer system's requirements for elastic fluctuation with high concurrency and low latency of message processing. Alibaba Cloud Knative provides elastic scheduling of containers based on events and messages and is compatible with standard Kubernetes. The number of application instances scales in and out in real-time during business peaks and valleys. By doing so, the cloud computing capability of on-demand and real-time elasticity has been achieved. Moreover, the entire process is fully automated, reducing the burden on the infrastructure of business developers significantly. The following example simulates a scenario for users.


  • An ACK/ASK cluster is created, and Knative is selected for installation.
  • Knative is deployed for reference for existing ASK clusters.
  • Event-driven Eventing is deployed.


1.  Deploy an event gateway

On the Knative component management console, select "Kourier" and "EventGateway" for deployment


2.  Deploy the event source. Select a Kafka event source for deployment


3.  Deploy a service

Here, a Knative Service event-display is deployed to receive events:

apiVersion: serving.knative.dev/v1
kind: Service
  name: event-display
    networking.knative.dev/flow-controller.class: strict
    networking.knative.dev/ingress.class: kourier.ingress.networking.knative.dev
        autoscaling.knative.dev/class: xpa.autoscaling.knative.dev
        autoscaling.knative.dev/maxScale: "100"
        autoscaling.knative.dev/minScale: "1"
        autoscaling.knative.dev/pod-buffer-size: "3"
        autoscaling.knative.dev/gc-time: "30"
      - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/event-display:0128

After the deployment, check the results:

$ kubectl get po |grep event
event-display-tslr5-deployment-5c9c4c469b-bnjlz                   2/2     Running   0          3h53m
event-display-tslr5-deployment-5c9c4c469b-bwxtm                   2/2     Running   0          3h53m
event-display-tslr5-deployment-5c9c4c469b-fjjr7                   2/2     Running   0          5d13h

4.  Create event source CR:

Create a KafkaSource resource and set a topic, a consumer group, a kourier gateway address, and the service event-display that is used to consume events corresponding to Kafka:

apiVersion: sources.knative.dev/v1alpha1
kind: KafkaSource
  name: kafka-source
    k8s.aliyun.com/domain: event-display.default.example.com
    k8s.aliyun.com/retry-count: "5"
    k8s.aliyun.com/retry-interval: "2"
    k8s.aliyun.com/req-timeout: "60"
  consumerGroup: demo-consumer
  # Broker URL. Replace this with the URLs for your kafka cluster,
  # which is in the format of my-cluster-kafka-bootstrap.my-kafka-namespace:9092.
  topics: demo

Once created, the corresponding Kafka event source pod can be checked:

$ kubectl get po|grep kafka
kafkasource-kafka-source-a8b95415-703f-4d50-8339-6203f0f6154pzq   1/1     Running   0          5d13h
kafkasource-kafka-source-a8b95415-703f-4d50-8339-6203f0f61x9xc9   1/1     Running   0          5d13h


Verify the results by sending a message from the client to the Kafka message queue. The results are listed below:

$ kubectl get po
NAME                                                              READY   STATUS    RESTARTS   AGE
event-display-tslr5-deployment-5c9c4c469b-58hv6                   2/2     Running   0          23s
event-display-tslr5-deployment-5c9c4c469b-bnjlz                   2/2     Running   0          4h6m
event-display-tslr5-deployment-5c9c4c469b-bwxtm                   2/2     Running   0          4h6m
event-display-tslr5-deployment-5c9c4c469b-d9vxc                   2/2     Running   0          23s
event-display-tslr5-deployment-5c9c4c469b-fjjr7                   2/2     Running   0          5d13h
event-display-tslr5-deployment-5c9c4c469b-kl827                   2/2     Running   0          24s
event-display-tslr5-deployment-5c9c4c469b-l5642                   2/2     Running   0          24s
event-display-tslr5-deployment-5c9c4c469b-q82pq                   2/2     Running   0          23s
event-display-tslr5-deployment-5c9c4c469b-qh7z5                   2/2     Running   0          24s
event-display-tslr5-deployment-5c9c4c469b-qmcnn                   2/2     Running   0          24s
event-display-tslr5-deployment-5c9c4c469b-t7sv2                   2/2     Running   0          24s
event-display-tslr5-deployment-5c9c4c469b-tpnwk                   2/2     Running   0          24s


This article introduces Eventing, the event-driving framework of Knative. It supports access to Kafka and RocketMQ event sources. The article also introduces the ways to combine MNS and Knative in AI event-driven scenarios. You are welcome to try it out today!.

0 0 0
Share on

Alibaba Container Service

99 posts | 26 followers

You may also like