在Serverless的使用场景中很多是基于事件触发的,即当Kative通过Eventing机制接收到某类事件后调用后台的Kative Service,本文介绍几种不同的触发方式调用Serverless。
在完成《OpenShift 4 之Knative(2):第一个Serverless应用》后,可根据本文实现“通过事件调用Serverless服务”。
首先我们创建一个基于“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
$ 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
$ 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
。。。。
$ oc delete cronjobsource.sources.eventing.knative.dev/event-dumpy-cronjob-source
$ oc get pod | grep Running
$ 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
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
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
$ 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
$ oc delete cronjobsource.sources.eventing.knative.dev/event-dumpy-cronjob-source
$ oc get po | grep Running
$ oc delete subscription.messaging.knative.dev/event-dumpy-subscriber