OpenShift 4 之Knative(3) - 通过事件触发Serverless服务

在Serverless的使用场景中很多是基于事件触发的,即当Kative通过Eventing机制接收到某类事件后调用后台的Kative Service,本文介绍几种不同的触发方式调用Serverless。
在完成《OpenShift 4 之Knative(2):第一个Serverless应用》后,可根据本文实现“通过事件调用Serverless服务”。

文章目录

  • 通过事件直接触发Serverless Service
    • 创建Event Source
    • 验证Serverless服务被触发
  • 通过订阅事件触发Serverless Service
    • 创建Channel对象
    • 验证Serverless服务被触发

通过事件直接触发Serverless Service

创建Event Source

首先我们创建一个基于“CronJobSource“对象的定时任务,该对象将定时任务的sink设为上一步实现的“dumpy-serverless”服务。

# event-source.yaml
apiVersion: sources.eventing.knative.dev/v1alpha1
kind: CronJobSource 
metadata:
  name: event-dumpy-cronjob-source
spec:
  schedule: "* * * * *"
  sink:
    apiVersion: serving.knative.dev/v1alpha1
    kind: Service
    name: dumpy-serverless 
$ oc create -f https://raw.githubusercontent.com/liuxiaoyu-git/knative-on-ocp4/master/eventing/event-source.yaml
$ oc get sources
NAME                                                                    AGE
cronjobsource.sources.eventing.knative.dev/event-dumpy-cronjob-source   24m

验证Serverless服务被触发

  1. 持续观察运行的Pod情况,此时应该有2个Running的Pod,一个Pod是运行定时任务event-dumpy-cronjob-source,另一个Pod是运行被调用的dumpy-serverless。
$ oc get pod | grep Running -w
cronjobsource-event-dumpy--390658a6-245e-11ea-9fb4-52fdfc072wr5   1/1     Running     0          29m
dumpy-serverless-xrjzq-2-deployment-99cfd764-g8wxz                2/2     Running     0          28m
  1. 然后通过查看dumpy-serverless的日志,确认该服务被每1分钟调用一次。
$ oc logs -f dumpy-serverless-xrjzq-2-deployment-99cfd764-g8wxz -c user-container
{"body":""}
2019/12/22 01:58:00 Openshift Http Request Dumper received a message: POST / HTTP/1.1
Host: dumpy-serverless.kn-demo.svc.cluster.local
Accept-Encoding: gzip
Ce-Id: 22c8e7d2-ba8f-4e1a-8028-dd292d417b3d
Ce-Source: /apis/v1/namespaces/kn-demo/cronjobsources/event-dumpy-cronjob-source
Ce-Specversion: 0.3
Ce-Time: 2019-12-22T01:58:00.003275636Z
Ce-Type: dev.knative.cronjob.event
Content-Length: 11
Forwarded: for=10.128.1.41;proto=http, for=10.128.1.6
K-Proxy-Request: activator
User-Agent: Go-http-client/1.1
X-B3-Parentspanid: 56637b9df6d4dd2b
X-B3-Sampled: 0
X-B3-Spanid: c46f0865a4e03070
X-B3-Traceid: a362fd95fac310f94b7be06279e24e4f
X-Envoy-Decorator-Operation: dumpy-serverless-xrjzq-2.kn-demo.svc.cluster.local:80/*
X-Envoy-Expected-Rq-Timeout-Ms: 600000
X-Envoy-Internal: true
X-Forwarded-For: 10.128.1.41, 10.128.1.6, 10.128.1.9
X-Forwarded-Proto: http
X-Request-Id: 9feec76b-57b8-414e-a327-588533e34df9
。。。。
  1. 然后我们删除掉Event Source:
$ oc delete cronjobsource.sources.eventing.knative.dev/event-dumpy-cronjob-source
  1. 然后监控Pod的运行情况,开始还有运行Serverless的Pod,稍等一会就没有了,说明Knative把运行该服务的Pod降为“0”了。
$ oc get pod | grep Running

通过订阅事件触发Serverless Service

创建Channel对象

  1. 创建一个InMemoryChannel类型的Channel对象(可以将该对象理解为消息对系列)。
$ oc create -f https://raw.githubusercontent.com/liuxiaoyu-git/knative-on-ocp4/master/eventing/in-memory-channel.yaml 
$ oc get inmemorychannel
NAME             READY   REASON   HOSTNAME                                              AGE
ch-event-dumpy   True             ch-event-dumpy-kn-channel.kn-demo.svc.cluster.local   38s

该InMemoryChannel对象定义如下:

apiVersion: messaging.knative.dev/v1alpha1
kind: InMemoryChannel
metadata:
  name: ch-event-dumpy
  1. 定义一个CronJobSource对象,作为事件源,将它产生的事件放到上一步的InMemoryChannel中。
apiVersion: sources.eventing.knative.dev/v1alpha1
kind: CronJobSource 
metadata:
  name: event-dumpy-cronjob-source
spec:
  schedule: "* * * * *"
  sink:
    apiVersion: messaging.knative.dev/v1alpha1
    kind: InMemoryChannel
    name: ch-event-dumpy

执行命令创建CronJobSource对象:

$ oc create -f https://raw.githubusercontent.com/liuxiaoyu-git/knative-on-ocp4/master/eventing/event-source-to-channel.yaml 
$ oc get cronjobsource
NAME                         AGE
event-dumpy-cronjob-source   13s
  1. 定义一个Subscription对象,从InMemoryChannel中获取事件,并将其传给名为dumpy-serverless的Serverless Service。
apiVersion: messaging.knative.dev/v1alpha1
kind: Subscription
metadata:
  name: event-dumpy-subscriber 
spec:
  channel:
    apiVersion: messaging.knative.dev/v1alpha1
    kind: InMemoryChannel
    name: ch-event-dumpy
  subscriber:
    ref:
      apiVersion: serving.knative.dev/v1alpha1
      kind: Service
      name: dumpy-serverless 

执行命令创建Subscription对象:

$ oc create -f https://raw.githubusercontent.com/liuxiaoyu-git/knative-on-ocp4/master/eventing/event-subscription.yaml
$ oc get subscription.messaging.knative.dev
NAME                     READY   REASON   AGE
event-dumpy-subscriber   True             3m26s

验证Serverless服务被触发

  1. 查看运行的Pod的。此时应该有2个Pod,一个用来定时产生事件(下面第一个),另一个是用来运行消费事件的dumpy-serverless服务(下面第二个)。
$ oc get pod | grep Running
cronjob-event-dumpy-cronjob-source-dzccx-76c95574c9-q6m2f   1/1     Running     0          8m54s
dumpy-serverless-956fn-deployment-65cb7f5d97-z6ffq          2/2     Running     0          60s
  1. 删掉定时产生任务的事件源event-dumpy-cronjob-source。
$ oc delete cronjobsource.sources.eventing.knative.dev/event-dumpy-cronjob-source
  1. 稍后确认运行dumpy-serverless的Pod数量已经降到“0”。
$ oc get po | grep Running
  1. 最后可以删除事件订阅。
$ oc delete subscription.messaging.knative.dev/event-dumpy-subscriber

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