環境確認
在OpenShift 4.3中確認Knative的運行環境,包括Knative Serving和Knative Eventing,以及基礎環境Service Mesh環境。
- Service Mesh Control Plane爲缺省配置。
- Service Mesh Member Roll配置。
piVersion: maistra.io/v1
kind: ServiceMeshMemberRoll
metadata:
name: default
namespace: istio-system
spec:
members:
- knative-serving
- knative-eventing
3. Knative Serving爲缺省配置。
4. Knative Eventing爲缺省配置。
配置Source和Sink
- 當安裝好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
- 創建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
- 創建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
- 執行命令創建創建以上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
- 查看eventinghello-cronjob-source和Service的狀態。
$ oc -n knativetutorial get cronjobsources -w
NAME READY AGE
eventinghello-cronjob-source False 2m23s
eventinghello-cronjob-source True 5m33s
- 查看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}')
- 在OpenShift Console的Developer視圖中,通過Topology查看knativetutorial項目資源。確認當前運行名爲eventinghello的Knative Serving實例數爲1。
在1分鐘左右如果沒有進一步請求,運行eventinghello的實例數降到0。 - 執行命令,清理運行環境。
$ 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