OpenShift 4.3 之Knative-Tutorial(6) Eventing之Channel和Subscription

文章目录

  • 概念
  • 操作
  • 参考

概念

Knative Eventing中的Channel和Subscription类似JMS消息体系中的Topic/Subscribe机制。Event Source是消息的发布者(类似JMS的Publisher),Channle是传送消息的通道(类似JMS的Topic),而Subscription通过订阅Channel获取到相关的Event,并且使用Knative Serving的服务处理消息。
OpenShift 4.3 之Knative-Tutorial(6) Eventing之Channel和Subscription_第1张图片

操作

  1. 创建如下内容的channel.yaml文件,其中定义了名为eventinghello-ch的Channel类型的对象。
apiVersion: messaging.knative.dev/v1alpha1
kind: Channel
metadata:
  name: eventinghello-ch
  1. 执行命令生成Channel对象,然后查看eventinghello-ch状态为READY=true。
$ oc -n knativetutorial apply -f channel.yaml
 
$ oc -n knativetutorial get channel
NAME               READY   REASON   URL                                                                    AGE
eventinghello-ch   True             http://eventinghello-ch-kn-channel.knativetutorial.svc.cluster.local   34s
 
$ oc -n knativetutorial get svc
NAME                          TYPE           CLUSTER-IP       EXTERNAL-IP                                           PORT(S)      AGE
eventinghello-ch-kn-channel   ExternalName   <none>           imc-dispatcher.knative-eventing.svc.cluster.local     <none>       40s
  1. 创建如下内容的event-source.yaml文件,其中定义了名为my-cjs的CronJobSource类型的对象,它每2秒产生一次事件。事件发送目标Sink为eventinghello-ch的Channel。
apiVersion: sources.eventing.knative.dev/v1alpha1
kind: CronJobSource
metadata:
  name: my-cjs
spec:
  schedule: "*/2 * * * *"
  data: '{"message": "From CronJob Source"}'
  sink:
   ref:
    apiVersion: messaging.knative.dev/v1alpha1 
    kind: Channel 
    name: eventinghello-ch
  1. 执行命令生成CronJobSource对象,然后查看my-cjs状态为READY=true。说明:如果接收事件的目标Sink没有Ready,那么my-cjs的状态也不会Ready。
$ oc -n knativetutorial apply -f event-source.yaml
cronjobsource.sources.eventing.knative.dev/event-greeter-cronjob-source created
 
$ oc -n knativetutorial get cronjobsources
NAME     READY   AGE
my-cjs   True    76s
 
$ oc -n knativetutorial get deployment
NAME                                                        READY   UP-TO-DATE   AVAILABLE   AGE
cronjobsource-my-cjs-bf3f1b8f-fc05-46d0-ae5c-a5bd9c7377b0   1/1     1            1           44m
  1. 分别创建eventing-helloa-sink.yaml和eventing-hellob-sink.yaml文件,内容如下。它们分别定义了名为eventinghelloa和eventinghellob的Service。
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: eventinghelloa
spec:
  template:
    metadata:
      name: eventinghelloa-v1
      annotations:
        autoscaling.knative.dev/target: "1"
    spec:
      containers:
      - image: quay.io/burrsutter/eventinghello:0.0.1
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: eventinghellob
spec:
  template:
    metadata:
      name: eventinghellob-v1
      annotations:
        autoscaling.knative.dev/target: "1"
    spec:
      containers:
      - image: quay.io/burrsutter/eventinghello:0.0.1
  1. 执行命令,创建eventinghelloa和eventinghellob服务。然后查看和eventinghelloa和eventinghellob相关的Deployment和Serivce。
$ oc apply -n knativetutorial -f eventing-helloa-sink.yaml
$ oc apply -n knativetutorial -f eventing-hellob-sink.yaml
 
$ oc -n knativetutorial get deployment 
NAME                                                        READY   UP-TO-DATE   AVAILABLE   AGE
cronjobsource-my-cjs-bf3f1b8f-fc05-46d0-ae5c-a5bd9c7377b0   1/1     1            1           44m
eventinghelloa-v1-deployment                                0/0     0            0           24m
eventinghellob-v1-deployment                                0/0     0            0           24m
 
$ oc -n knativetutorial get svc
NAME                          TYPE           CLUSTER-IP       EXTERNAL-IP                                                  PORT(S)                             AGE
eventinghello-ch-kn-channel   ExternalName   <none>           imc-dispatcher.knative-eventing.svc.cluster.local            <none>                              53m
eventinghelloa                ExternalName   <none>           kourier-internal.knative-serving-ingress.svc.cluster.local   <none>                              12s
eventinghelloa-v1             ClusterIP      172.30.177.109   <none>                                                       80/TCP                              22s
eventinghelloa-v1-private     ClusterIP      172.30.210.190   <none>                                                       80/TCP,9090/TCP,9091/TCP,8022/TCP   22s
eventinghellob                ExternalName   <none>           kourier-internal.knative-serving-ingress.svc.cluster.local   <none>                              11s
eventinghellob-v1             ClusterIP      172.30.116.241   <none>                                                       80/TCP                              21s
eventinghellob-v1-private     ClusterIP      172.30.45.121    <none>                                                       80/TCP,9090/TCP,9091/TCP,8022/TCP   21s
  1. 创建eventing-helloa-sub.yaml和eventing-hellob-sub.yaml文件,内容分别如下。他们分别定义了名为eventinghelloa-sub和eventinghellob-sub的Subscription。这两个Subscription都是从名为eventinghello-ch的Channel获取消息,只不过一个用名为eventinghelloa的Service处理,另一个用名为eventinghellob的Service处理。
apiVersion: messaging.knative.dev/v1alpha1
kind: Subscription
metadata:
  name: eventinghelloa-sub
spec:
  channel:
    apiVersion: messaging.knative.dev/v1alpha1
    kind: Channel
    name: eventinghello-ch
  subscriber:
    ref:
      apiVersion: serving.knative.dev/v1
      kind: Service
      name: eventinghelloa
apiVersion: messaging.knative.dev/v1alpha1
kind: Subscription
metadata:
  name: eventinghellob-sub
spec:
  channel:
    apiVersion: messaging.knative.dev/v1alpha1
    kind: Channel
    name: eventinghello-ch
  subscriber:
    ref:
      apiVersion: serving.knative.dev/v1
      kind: Service
      name: eventinghellob
  1. 执行命令,分别创建名为eventinghelloa-sub和eventinghellob-sub的Subscription,然后查看它们的状态变为READY=True。
$ oc -n knativetutorial apply -f eventing-helloa-sub.yaml
$ oc -n knativetutorial apply -f eventing-hellob-sub.yaml
 
$ oc -n knativetutorial get subscriptions.messaging.knative.dev
NAME                 READY   REASON   AGE
eventinghelloa-sub   True             7m11s
eventinghellob-sub   True             7m11s
  1. 在控制台查看运行情况。可以看到eventinghelloa和eventinghellob的实例数会在0和1之间变化。
    OpenShift 4.3 之Knative-Tutorial(6) Eventing之Channel和Subscription_第2张图片
    OpenShift 4.3 之Knative-Tutorial(6) Eventing之Channel和Subscription_第3张图片
  2. 执行命令,清理环境。
oc -n knativetutorial delete -f channel.yaml
oc -n knativetutorial delete -f event-source.yaml
oc -n knativetutorial delete -f eventing-helloa-sink.yaml
oc -n knativetutorial delete -f eventing-hellob-sink.yaml
oc -n knativetutorial delete -f eventing-helloa-sub.yaml
oc -n knativetutorial delete -f eventing-hellob-sub.yaml

参考

  • https://github.com/RedHatWorkshops/knative-on-ocp4/blob/master/6.AddingChannelsAndSubscriptions.md

你可能感兴趣的:(OpenShift,4,Knative,Dev)