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:
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.
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.
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:
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:
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.
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:
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:
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.
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
metadata:
name: event-display
annotations:
networking.knative.dev/flow-controller.class: strict
networking.knative.dev/ingress.class: kourier.ingress.networking.knative.dev
spec:
template:
metadata:
annotations:
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"
spec:
containers:
- 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
metadata:
name: kafka-source
annotations:
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"
spec:
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.
bootstrapServers: 192.168.7.113:9092,192.168.7.110:9092,192.168.7.108:9092
topics: demo
sink:
uri: http://101.200.177.84
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!.
164 posts | 29 followers
FollowAlibaba Developer - September 7, 2020
Alibaba Container Service - January 27, 2022
Alibaba Container Service - July 22, 2021
Alibaba Container Service - August 1, 2023
Alibaba Container Service - April 18, 2024
Alibaba Developer - March 3, 2020
164 posts | 29 followers
FollowAlibaba Cloud Function Compute is a fully-managed event-driven compute service. It allows you to focus on writing and uploading code without the need to manage infrastructure such as servers.
Learn MoreA fully-managed Apache Kafka service to help you quickly build data pipelines for your big data analytics.
Learn MoreVisualization, O&M-free orchestration, and Coordination of Stateful Application Scenarios
Learn MoreAccelerate AI-driven business and AI model training and inference with Alibaba Cloud GPU technology
Learn MoreMore Posts by Alibaba Container Service