Q 《OpenShift 4.x HOL教程汇总》
说明:本文已经在OpenShift 4.13 + OpenShift Serverless 1.29.1环境中验证
概念
Knative Eventing中的Channel和Subscription类似JMS消息体系中的Topic/Subscribe机制。Event Source是消息的发布者(类似JMS的Publisher),Channle是传送消息的通道(类似JMS的Topic),而Subscription通过订阅Channel获取到相关的Event,并且使用Knative Serving的服务处理消息。
操作
- 执行命令,创建名为eventinghello-ch的Channel类型的对象,然后查看Knative的channel 对象,其中eventinghello-ch状态为READY=true。
$ kn channel create eventinghello-ch -n knative-tutorial
$ kn channel list -n knative-tutorial
NAME TYPE URL AGE READY REASON
eventinghello-ch InMemoryChannel http://eventinghello-ch-kn-channel.knative-tutorial.svc.cluster.local 24s True
- 查看生成的所有OpenShift对象。
$ oc get all -n knative-tutorial
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/eventinghello-ch-kn-channel ExternalName <none> imc-dispatcher.knative-eventing.svc.cluster.local <none> 6m37s
NAME URL AGE READY REASON
channel.messaging.knative.dev/eventinghello-ch http://eventinghello-ch-kn-channel.knative-tutorial.svc.cluster.local 6m37s True
NAME URL AGE READY REASON
inmemorychannel.messaging.knative.dev/eventinghello-ch http://eventinghello-ch-kn-channel.knative-tutorial.svc.cluster.local 6m37s True
- 执行命令生成PingSource对象,然后查看PingSource对象的状态为READY=true。
$ kn source ping create event-greeter-ping-source --schedule "*/2 * * * *" --sink channel:eventinghello-ch -n knative-tutorial
$ kn source ping list -n knative-tutorial
NAME SCHEDULE SINK AGE CONDITIONS READY REASON
event-greeter-ping-source */2 * * * * Channel:eventinghello-ch 9m56s 3 OK / 3 True
- 执行命令创建名为eventinghelloa和eventinghellob的Service。
$ kn service create eventinghelloa --concurrency-target=1 --revision-name=eventinghelloa-v1 --image=quay.io/rhdevelopers/eventinghello:0.0.2 -n knative-tutorial
$ kn service create eventinghellob --concurrency-target=1 --revision-name=eventinghellob-v1 --image=quay.io/rhdevelopers/eventinghello:0.0.2 -n knative-tutorial
- 查看和eventinghelloa和eventinghellob相关的Knative Serivce对象,它们的状态为READY=true。
$ kn service list -n knative-tutorial
NAME URL LATEST AGE CONDITIONS READY REASON
eventinghelloa http://eventinghelloa-knative-tutorial.apps.cluster-pek-e4bf.pek-e4bf.sandbox1216.opentlc.com eventinghelloa-v1 99s 3 OK / 3 True
eventinghellob http://eventinghellob-knative-tutorial.apps.cluster-pek-e4bf.pek-e4bf.sandbox1216.opentlc.com eventinghellob-v1 110s 3 OK / 3 True
- 执行命令,分别创建名为eventinghelloa-sub和eventinghellob-sub的Subscription。这两个Subscription都是从名为eventinghello-ch的Channel获取消息,只不过一个用名为eventinghelloa的Service处理,另一个用名为eventinghellob的Service处理。
$ kn subscription create eventinghelloa-sub --channel eventinghello-ch --sink eventinghelloa -n knative-tutorial
$ kn subscription create eventinghellob-sub --channel eventinghello-ch --sink eventinghellob -n knative-tutorial
$ kn subscription list -n knative-tutorial
NAME CHANNEL SUBSCRIBER REPLY DEAD LETTER SINK READY REASON
eventinghelloa-sub Channel:eventinghello-ch ksvc:eventinghelloa True
eventinghellob-sub Channel:eventinghello-ch ksvc:eventinghellob True
- 在控制台查看运行情况。可以看到eventinghelloa和eventinghellob的实例数会在0和2之间变化。
- 执行命令,清理环境。
$ kn service delete eventinghelloa -n knative-tutorial
$ kn service delete eventinghellob -n knative-tutorial
$ kn subscription delete eventinghelloa-sub -n knative-tutorial
$ kn subscription delete eventinghellob-sub -n knative-tutorial
$ kn source ping delete event-greeter-ping-source -n knative-tutorial
$ kn channel delete eventinghello-ch -n knative-tutorial
参考
- https://redhat-developer-demos.github.io/knative-tutorial/knative-tutorial/eventing/channel-and-subscribers.html