OpenShift 4.3 之Knative-Tutorial(5) Eventing之Source和Sink

文章目录

  • 环境确认
  • 配置Source和Sink
  • 参考

环境确认

在OpenShift 4.3中确认Knative的运行环境,包括Knative Serving和Knative Eventing,以及基础环境Service Mesh环境。

  1. Service Mesh Control Plane为缺省配置。
    OpenShift 4.3 之Knative-Tutorial(5) Eventing之Source和Sink_第1张图片
  2. Service Mesh Member Roll配置。
piVersion: maistra.io/v1
kind: ServiceMeshMemberRoll
metadata:
  name: default
  namespace: istio-system
spec:
  members:
    - knative-serving
    - knative-eventing

OpenShift 4.3 之Knative-Tutorial(5) Eventing之Source和Sink_第2张图片
3. Knative Serving为缺省配置。
OpenShift 4.3 之Knative-Tutorial(5) Eventing之Source和Sink_第3张图片
4. Knative Eventing为缺省配置。
OpenShift 4.3 之Knative-Tutorial(5) Eventing之Source和Sink_第4张图片

配置Source和Sink

  1. 当安装好Knative Eventing Operator后,会在OpenShift中增加sources.eventing.knative.dev相关的Kubernetes API。执行以下命令可以查看这些API,我们可以看到平台支持的Source类型。本文将使用CronJobSource实现定时发送消息。
$ oc api-resources --api-group='sources.eventing.knative.dev'
NAME              APIGROUP                      NAMESPACED   KIND
apiserversources  sources.eventing.knative.dev  true         ApiServerSource
containersources  sources.eventing.knative.dev  true         ContainerSource
cronjobsources    sources.eventing.knative.dev  true         CronJobSource
sinkbindings      sources.eventing.knative.dev  true         SinkBinding
  1. 创建eventinghello-source.yaml文件,内容如下。文件定义了名为event-greeter-cronjob-source的CronJobSource,它每2分钟发送一次内容为"Thanks for doing Knative Tutorial"的消息。消息发送到Sink(即目标)是名为eventinghello的Service。
apiVersion: sources.eventing.knative.dev/v1
kind: CronJobSource
metadata:
  name: event-greeter-cronjob-source
spec:
  schedule: "*/2 * * * *"
  data: '{"message": "Thanks for doing Knative Tutorial"}'
  sink:  
    apiVersion: serving.knative.dev/v1 
    kind: Service
    name: eventinghello
  1. 创建eventing-hello-sink.yaml文件,内容如下。它定义作为消息接受目标(Sink)—— 名为eventinghello的Service是如何实现的。
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: eventinghello
spec:
  template:
    metadata:
      name: eventinghello-v1
      annotations:
        autoscaling.knative.dev/target: "1"
    spec:
      containers:
      - image: quay.io/burrsutter/eventinghello:0.0.1
  1. 执行命令创建创建以上Source和Service。
$ oc -n knativetutorial apply -f eventinghello-source.yaml
cronjobsource.sources.eventing.knative.dev/eventinghello-cronjob-source created
$ oc -n knativetutorial apply -f eventing-hello-sink.yaml
service.serving.knative.dev/eventinghello created
  1. 查看eventinghello-cronjob-source和Service的状态。
$ oc -n knativetutorial get cronjobsources -w
NAME                           READY   AGE
eventinghello-cronjob-source   False   2m23s
eventinghello-cronjob-source   True    5m33s
  1. 查看Service状态,并测试Serverless该服务。
$ oc -n knativetutorial get svc eventinghello
NAME            TYPE           CLUSTER-IP   EXTERNAL-IP                                                  PORT(S)   AGE
eventinghello   ExternalName   <none>       kourier-internal.knative-serving-ingress.svc.cluster.local   <none>    11m
 
$ curl $(kn route list | grep eventinghello | awk 'NR==1 {print $2}')
  1. 在OpenShift Console的Developer视图中,通过Topology查看knativetutorial项目资源。确认当前运行名为eventinghello的Knative Serving实例数为1。
    OpenShift 4.3 之Knative-Tutorial(5) Eventing之Source和Sink_第5张图片
    在1分钟左右如果没有进一步请求,运行eventinghello的实例数降到0。OpenShift 4.3 之Knative-Tutorial(5) Eventing之Source和Sink_第6张图片
  2. 执行命令,清理运行环境。
$ oc -n knativetutorial delete -f eventinghello-source.yaml
$ oc -n knativetutorial delete -f eventing-hello-sink.yaml

参考

  • https://github.com/RedHatWorkshops/knative-on-ocp4/blob/master/5.AddingDirectEventSource.md

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