Knative 基本功能深入剖析:Knative Eventing 之 Sequence 介绍

作者 | 元毅,阿里云容器平台高级开发工程师,负责阿里云容器平台 Knative 相关工作。

导读:在实际的开发中我们经常会遇到将一条数据需要经过多次处理的场景,称为 Pipeline。那么在 Knative 中是否也提供这样的能力呢?其实从 Knative Eventing 0.7 版本开始,就提供了 Sequence CRD 资源,用于事件处理 Pipeline。本篇文章中,阿里云容器平台高级开发工程师元毅将为大家详细介绍 Sequence 的定义及在 Knative Eventing 中提供的 4 种使用场景。

Sequence 定义

首先来看一下 Sequence Spec 定义:

apiVersion: messaging.knative.dev/v1alpha1
kind: Sequence
metadata:name: test
spec:channelTemplate:apiVersion: messaging.knative.dev/v1alpha1kind: InMemoryChannelsteps:- ref:apiVersion: serving.knative.dev/v1alpha1kind: Servicename: testreply:kind: BrokerapiVersion: eventing.knative.dev/v1alpha1name: test

Sequence Spec 包括 3 个部分:

  1. steps: 在 step 中定义了按照顺序执行的服务,每个服务会对应创建 Subscription;

  2. channelTemplate:指定了使用具体的那个 Channel;

  3. reply:(可选)定义了将最后一个 step 服务结果转发到的目标服务。

    Sequence 都是适合哪些具体应用场景呢?我们上面也提到了事件处理的 Pipeline。那么在实际场景应用中究竟以什么样的形式体现呢? 现在我们揭晓一下 Sequence 在 Knative Eventing 中提供的如下 4 种使用场景:

  • 直接访问 Service;
  • 面向事件处理;
  • 级联 Sequence;
  • 面向 Broker/Trigger。

直接访问 Service 场景

事件源产生的事件直接发送给 Sequence 服务, Sequence 接收到事件之后顺序调用 Service 服务对事件进行处理:
在这里插入图片描述

创建 Knative Service

这里我们创建 3 个 Knative Service 用于事件处理。每个 Service 接收到事件之后会打印当前的事件处理信息。

apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:name: first
spec:template:spec:containers:- image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700env:- name: STEPvalue: "0"---
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:name: second
spec:template:spec:containers:- image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700env:- name: STEPvalue: "1"
---
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:name: third
spec:template:spec:containers:- image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700env:- name: STEPvalue: "2"
---

创建 Sequence

创建顺序调用 first->second->third Service 的 Sequence。

apiVersion: messaging.knative.dev/v1alpha1
kind: Sequence
metadata:name: sequence
spec:channelTemplate:apiVersion: messaging.knative.dev/v1alpha1kind: InMemoryChannelsteps:- ref:apiVersion: serving.knative.dev/v1alpha1kind: Servicename: first- ref:apiVersion: serving.knative.dev/v1alpha1kind: Servicename: second- ref:apiVersion: serving.knative.dev/v1alpha1kind: Servicename: third

创建数据源

创建 CronJobSource 数据源,每隔 1 分钟发送一条事件消息 {"message": "Hello world!"} 到 Sequence 服务。

apiVersion: sources.eventing.knative.dev/v1alpha1
kind: CronJobSource
metadata:name: cronjob-source
spec:schedule: "*/1 * * * *"data: '{"message": "Hello world!"}'sink:apiVersion: messaging.knative.dev/v1alpha1kind: Sequencename: sequence

示例结果

在这里插入图片描述
在这里插入图片描述

面向事件处理场景

事件源产生的事件直接发送给 Sequence 服务, Sequence 接收到事件之后顺序调用 Service 服务对事件进行处理,处理之后的最终结果会调用 event-display Service 显示:
在这里插入图片描述

创建 Knative Service

同上创建 3 个 Knative Service 用于事件处理:

apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:name: first
spec:template:spec:containers:- image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700env:- name: STEPvalue: "0"---
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:name: second
spec:template:spec:containers:- image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700env:- name: STEPvalue: "1"
---
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:name: third
spec:template:spec:containers:- image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700env:- name: STEPvalue: "2"
---

创建 Sequence

创建顺序调用 first->second->third Service 的 Sequence,将处理结果通过 reply 发送给 event-display

apiVersion: messaging.knative.dev/v1alpha1
kind: Sequence
metadata:name: sequence
spec:channelTemplate:apiVersion: messaging.knative.dev/v1alpha1kind: InMemoryChannelsteps:- ref:apiVersion: serving.knative.dev/v1alpha1kind: Servicename: first- ref:apiVersion: serving.knative.dev/v1alpha1kind: Servicename: second- ref:apiVersion: serving.knative.dev/v1alpha1kind: Servicename: thirdreply:kind: ServiceapiVersion: serving.knative.dev/v1alpha1name: event-display

创建结果显示 Service

创建 event-display Service, 用于接收最终的结果信息。

apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:name: event-display
spec:template:spec:containers:- image: registry.cn-hangzhou.aliyuncs.com/knative-release/event_display:bf45b3eb1e7fc4cb63d6a5a6416cf696295484a7662e0cf9ccdf5c080542c21d

创建数据源

创建 CronJobSource 数据源,每隔 1 分钟发送一条事件消息 {"message": "Hello world!"} 到 Sequence 服务。

apiVersion: sources.eventing.knative.dev/v1alpha1
kind: CronJobSource
metadata:name: cronjob-source
spec:schedule: "*/1 * * * *"data: '{"message": "Hello world!"}'sink:apiVersion: messaging.knative.dev/v1alpha1kind: Sequencename: sequence

示例结果

在这里插入图片描述
在这里插入图片描述

级联 Sequence 场景

Sequence 更高级的地方还在于支持级联处理: Sequence By Sequence,这样可以进行多次 Sequence 处理,满足复杂事件处理场景需求。
在这里插入图片描述

创建 Knative Service

创建 6 个 Knative Service 用于事件处理, 前 3 个用于第 1 个 Sequence,后 3 个用于第 2 个 Sequence。

apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:name: first
spec:template:spec:containers:- image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700env:- name: STEPvalue: "0"---
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:name: second
spec:template:spec:containers:- image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700env:- name: STEPvalue: "1"
---
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:name: third
spec:template:spec:containers:- image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700env:- name: STEPvalue: "2"
---
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:name: fourth
spec:template:spec:containers:- image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700env:- name: STEPvalue: "3"---
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:name: fifth
spec:template:spec:containers:- image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700env:- name: STEPvalue: "4"
---
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:name: sixth
spec:template:spec:containers:- image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700env:- name: STEPvalue: "5"
---

创建第 1 个 Sequence

使用 first->second->third Service 用于第 1 个 Sequence 调用处理,将执行结果发送给第 2 个 Sequence。

apiVersion: messaging.knative.dev/v1alpha1
kind: Sequence
metadata:name: first-sequence
spec:channelTemplate:apiVersion: messaging.knative.dev/v1alpha1kind: InMemoryChannelsteps:- ref:apiVersion: serving.knative.dev/v1alpha1kind: Servicename: first- ref:apiVersion: serving.knative.dev/v1alpha1kind: Servicename: second- ref:apiVersion: serving.knative.dev/v1alpha1kind: Servicename: thirdreply:kind: SequenceapiVersion: messaging.knative.dev/v1alpha1name: second-sequence

创建第 2 个 Sequence

使用 fourth->fifth->sixth Service 用于第 2 个 Sequence 调用处理,将执行结果发送给 event-display

apiVersion: messaging.knative.dev/v1alpha1
kind: Sequence
metadata:name: second-sequence
spec:channelTemplate:apiVersion: messaging.knative.dev/v1alpha1kind: InMemoryChannelsteps:- ref:apiVersion: serving.knative.dev/v1alpha1kind: Servicename: fourth- ref:apiVersion: serving.knative.dev/v1alpha1kind: Servicename: fifth- ref:apiVersion: serving.knative.dev/v1alpha1kind: Servicename: sixthreply:kind: ServiceapiVersion: serving.knative.dev/v1alpha1name: event-display

创建结果显示 Service

apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:name: event-display
spec:template:spec:containerers:- image: registry.cn-hangzhou.aliyuncs.com/knative-release/event_display:bf45b3eb1e7fc4cb63d6a5a6416cf696295484a7662e0cf9ccdf5c080542c21d

创建数据源指向第 1 个 Sequence

apiVersion: sources.eventing.knative.dev/v1alpha1
kind: CronJobSource
metadata:name: cronjob-source
spec:schedule: "*/1 * * * *"data: '{"message": "Hello world!"}'sink:apiVersion: messaging.knative.dev/v1alpha1kind: Sequencename: first-sequence

示例结果

在这里插入图片描述
在这里插入图片描述

Broker/Trigger 场景

事件源 cronjobsource 向 Broker 发送事件,通过 Trigger 将这些事件发送到由 3 个 Service 调用的 Sequence 中。Sequence 处理完之后将结果事件发送给 Broker,并最终由另一个 Trigger 发送给 event-display Service 显示事件结果:
在这里插入图片描述

创建 Knative Service

同上创建 3 个 Knative Service,用于 Sequence 中服务处理。

apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:name: first
spec:template:spec:containers:- image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700env:- name: STEPvalue: "0"---
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:name: second
spec:template:spec:containers:- image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700env:- name: STEPvalue: "1"
---
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:name: third
spec:template:spec:containers:- image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700env:- name: STEPvalue: "2"---

创建 Sequence

创建 Sequence,这里依次顺序执行 first->second->third 这 3 个服务。将最终处理的结果发送到 broker-test 中。

apiVersion: messaging.knative.dev/v1alpha1
kind: Sequence
metadata:name: sequence
spec:channelTemplate:apiVersion: messaging.knative.dev/v1alpha1kind: InMemoryChannelsteps:- ref:apiVersion: serving.knative.dev/v1alpha1kind: Servicename: first- ref:apiVersion: serving.knative.dev/v1alpha1kind: Servicename: second- ref:apiVersion: serving.knative.dev/v1alpha1kind: Servicename: thirdreply:kind: BrokerapiVersion: eventing.knative.dev/v1alpha1name: default

创建事件源指向 Broker

创建 CronjobSource,它将每隔 1 分钟发送一条 {"message": "Hello world!"} 消息到 broker-test 中。

apiVersion: sources.eventing.knative.dev/v1alpha1
kind: CronJobSource
metadata:name: cronjob-source
spec:schedule: "*/1 * * * *"data: '{"message": "Hello world!"}'sink:apiVersion: eventing.knative.dev/v1alpha1kind: Brokername: default

创建 Broker

创建默认 Broker:

kubectl label namespace default knative-eventing-injection=enabled

创建 Trigger 指向 Sequence

创建订阅事件类型为 dev.knative.cronjob.event 的 Trigger, 用于 Sequence 进行消费处理。

apiVersion: eventing.knative.dev/v1alpha1
kind: Trigger
metadata:name: sequence-trigger
spec:filter:sourceAndType:type: dev.knative.cronjob.eventsubscriber:ref:apiVersion: messaging.knative.dev/v1alpha1kind: Sequencename: sequence

创建结果订阅 Trigger

创建订阅 samples.http.mod3 的事件类型 Trigger,将 Sequence 执行的结果发送给 event-display Service 进行显示。

apiVersion: eventing.knative.dev/v1alpha1
kind: Trigger
metadata:name: display-trigger
spec:filter:sourceAndType:type: samples.http.mod3subscriber:ref:apiVersion: serving.knative.dev/v1alpha1kind: Servicename: event-display
---
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:name: event-display
spec:template:spec:containers:- image: registry.cn-hangzhou.aliyuncs.com/knative-release/event_display:bf45b3eb1e7fc4cb63d6a5a6416cf696295484a7662e0cf9ccdf5c080542c21d
---

示例结果

在这里插入图片描述
在这里插入图片描述

小结

以上介绍了什么是 Sequence, 以及基于 Sequence 的 4 种使用场景,我们可以根据实际需求选择不同的使用场景,从而实现事件处理 Pipeline。这对于需要多步骤处理事件的场景尤为适合。

欢迎加入 Knative 交流群

在这里插入图片描述

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注