环境说明
◼ PingSource负责生成event
◼ Event由Sequence中的各Step顺次处理
◆各Step都运行一个appender应用
◆分别向收到的数据尾部附加自定义的专有数据项
◼ 最终结果发往ksvc/event-display
环境示意图
创建名称空间
# kubectl create ns sequence-demo
namespace/sequence-demo created
创建 step
steps.yaml
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: first
namespace: sequence-demo
spec:
template:
spec:
containers:
- image: gcr.dockerproxy.com/knative-releases/knative.dev/eventing/cmd/appender
env:
- name: MESSAGE
value: " - Handled by 0"
---
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: second
namespace: sequence-demo
spec:
template:
spec:
containers:
- image: gcr.dockerproxy.com/knative-releases/knative.dev/eventing/cmd/appender
env:
- name: MESSAGE
value: " - Handled by 1"
---
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: third
namespace: sequence-demo
spec:
template:
spec:
containers:
- image: gcr.dockerproxy.com/knative-releases/knative.dev/eventing/cmd/appender
env:
- name: MESSAGE
value: " - Handled by 2"
创建 steps 资源
# kubectl apply -f steps.yaml
service.serving.knative.dev/first created
service.serving.knative.dev/second created
service.serving.knative.dev/third created
查看 steps 资源
# kubectl get ksvc -n sequence-demo
NAME URL LATESTCREATED LATESTREADY READY REASON
first http://first.sequence-demo.svc.wgs.local first-00001 first-00001 True
second http://second.sequence-demo.svc.wgs.local second-00001 second-00001 True
third http://third.sequence-demo.svc.wgs.local third-00001 third-00001 True
创建 event-display
event-display.yaml
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: event-display
namespace: sequence-demo
spec:
template:
metadata:
annotations:
autoscaling.knative.dev/min-scale: "1"
spec:
containers:
- image: gcr.dockerproxy.com/knative-releases/knative.dev/eventing/cmd/event_display
创建 event-display 资源
# kubectl apply -f event-display.yaml
service.serving.knative.dev/event-display created
查看 event-display 资源
# kubectl get ksvc -n sequence-demo
NAME URL LATESTCREATED LATESTREADY READY REASON
event-display http://event-display.sequence-demo.svc.wgs.local event-display-00001 event-display-00001 True
first http://first.sequence-demo.svc.wgs.local first-00001 first-00001 True
second http://second.sequence-demo.svc.wgs.local second-00001 second-00001 True
third http://third.sequence-demo.svc.wgs.local third-00001 third-00001 True
创建 sequence
sequence.yaml
apiVersion: flows.knative.dev/v1
kind: Sequence
metadata:
name: sequence
namespace: sequence-demo
spec:
channelTemplate:
apiVersion: messaging.knative.dev/v1
kind: InMemoryChannel
steps:
- ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: first
namespace: sequence-demo
- ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: second
namespace: sequence-demo
- ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: third
namespace: sequence-demo
reply:
ref:
kind: Service
apiVersion: serving.knative.dev/v1
name: event-display
namespace: sequence-demo
创建 sequence 资源
# kubectl apply -f sequence.yaml
sequence.flows.knative.dev/sequence created
查看 sequence 资源
# kubectl get sequences -n sequence-demo
NAME URL AGE READY REASON
sequence http://sequence-kn-sequence-0-kn-channel.sequence-demo.svc.wgs.local 69s True
创建 PingSource
ping-source.yaml
apiVersion: sources.knative.dev/v1
kind: PingSource
metadata:
name: ping-source
namespace: sequence-demo
spec:
schedule: "*/2 * * * *"
contentType: "application/json"
data: '{"message": "Hello world!"}'
sink:
ref:
apiVersion: flows.knative.dev/v1
kind: Sequence
name: sequence
namespace: sequence-demo
创建 pingsource 资源
# kubectl apply -f ping-source.yaml
pingsource.sources.knative.dev/ping-source created
查看 pingsource 资源
# kubectl get PingSource -n sequence-demo
NAME SINK SCHEDULE AGE READY REASON
ping-source http://sequence-kn-sequence-0-kn-channel.sequence-demo.svc.wgs.local */2 * * * * 64s True
验证结果
查看 pod
# kubectl get pod -n sequence-demo
NAME READY STATUS RESTARTS AGE
event-display-00002-deployment-5d884ddd6f-qhwpx 2/2 Running 0 32s
查看日志
# kubectl logs event-display-00002-deployment-5d884ddd6f-qhwpx -c user-container -n sequence-demo
☁️ cloudevents.Event
Context Attributes,
specversion: 1.0
type: dev.knative.sources.ping
source: /apis/v1/namespaces/sequence-demo/pingsources/ping-source
id: 8df28a0b-2391-405b-a369-28c23882e6af
time: 2023-11-19T13:44:00.245632902Z
datacontenttype: application/json
Data,
{
"id": 0,
"message": "Hello world! - Handled by 0 - Handled by 1 - Handled by 2"
}
参考文档
https://knative.dev/docs/eventing/flows/sequence/
标签:sequence,Eventing,demo,示例,Flow,dev,display,knative,event From: https://www.cnblogs.com/wangguishe/p/17836908.html