第 14 章 在 Service Mesh 中使用接收器绑定
您可以将接收器绑定与 Service Mesh 搭配使用。
14.1. 使用 Service Mesh 配置接收器绑定
此流程描述了如何使用 Service Mesh 配置接收器绑定。
先决条件
- 您已设置了 Service Mesh 和 Serverless 的集成。
流程
在作为
Service
MeshMemberRollevent-display-service.yaml
配置文件示例apiVersion: serving.knative.dev/v1 kind: Service metadata: name: event-display namespace: <namespace> 1 spec: template: metadata: annotations: sidecar.istio.io/inject: "true" 2 sidecar.istio.io/rewriteAppHTTPProbers: "true" spec: containers: - image: quay.io/openshift-knative/knative-eventing-sources-event-display:latest
应用
Service
对象:$ oc apply -f event-display-service.yaml
创建
SinkBinding
对象:heartbeat-sinkbinding.yaml
配置文件示例apiVersion: sources.knative.dev/v1alpha1 kind: SinkBinding metadata: name: bind-heartbeat namespace: <namespace> 1 spec: subject: apiVersion: batch/v1 kind: Job 2 selector: matchLabels: app: heartbeat-cron sink: ref: apiVersion: serving.knative.dev/v1 kind: Service name: event-display
应用
SinkBinding
对象:$ oc apply -f heartbeat-sinkbinding.yaml
创建
CronJob
对象:heartbeat-cronjob.yaml
配置文件示例apiVersion: batch/v1 kind: CronJob metadata: name: heartbeat-cron namespace: <namespace> 1 spec: # Run every minute schedule: "* * * * *" jobTemplate: metadata: labels: app: heartbeat-cron bindings.knative.dev/include: "true" spec: template: metadata: annotations: sidecar.istio.io/inject: "true" 2 sidecar.istio.io/rewriteAppHTTPProbers: "true" spec: restartPolicy: Never containers: - name: single-heartbeat image: quay.io/openshift-knative/heartbeats:latest args: - --period=1 env: - name: ONE_SHOT value: "true" - name: POD_NAME valueFrom: fieldRef: fieldPath: metadata.name - name: POD_NAMESPACE valueFrom: fieldRef: fieldPath: metadata.namespace
应用
CronJob
对象:$ oc apply -f heartbeat-cronjob.yaml
可选:查看消息转储程序功能日志来验证事件是否已发送到 Knative 事件接收器:
示例命令
$ oc logs $(oc get pod -o name | grep event-display) -c user-container
输出示例
☁️ cloudevents.Event Validation: valid Context Attributes, specversion: 1.0 type: dev.knative.eventing.samples.heartbeat source: https://knative.dev/eventing-contrib/cmd/heartbeats/#event-test/mypod id: 2b72d7bf-c38f-4a98-a433-608fbcdd2596 time: 2019-10-18T15:23:20.809775386Z contenttype: application/json Extensions, beats: true heart: yes the: 42 Data, { "id": 1, "label": "" }