创建 ContainerSource¶
ContainerSource 对象启动一个生成事件并将消息发送到 sink URI 的容器镜像。您还可以使用 ContainerSource 在 Knative 中支持您自己的事件源。
要使用 ContainerSource 创建自定义事件源,您必须创建一个容器镜像,以及一个使用您的镜像 URI 的 ContainerSource。
准备工作¶
在创建 ContainerSource 对象之前,您必须在集群上安装 Knative Eventing。
开发、构建和发布容器镜像¶
您可以使用任何语言开发容器镜像,并可以使用任何您喜欢的工具构建和发布您的镜像。以下是一些基本准则:
- ContainerSource 控制器注入两个环境变量;
K_SINK和K_CE_OVERRIDES,它们分别从spec.sink和spec.ceOverrides解析。 - 事件消息以 CloudEvents HTTP 格式以 POST 方式发送到
K_SINK中指定的 sink URI。
创建 ContainerSource 对象¶
-
构建您的事件源镜像并将其发布到您的镜像仓库。您的镜像必须读取环境变量
K_SINK并将消息发布到K_SINK中指定的 URL。您可以使用以下 YAML 部署一个演示
heartbeats事件源apiVersion: sources.knative.dev/v1 kind: ContainerSource metadata: name: heartbeat-source spec: template: spec: containers: - image: gcr.io/knative-nightly/knative.dev/eventing/cmd/heartbeats:latest name: heartbeats sink: ref: apiVersion: serving.knative.dev/v1 kind: Service name: event-display -
通过运行命令为您的 ContainerSource 创建一个命名空间
kubectl create namespace <namespace>其中
<namespace>是您希望您的 ContainerSource 使用的命名空间。例如,heartbeat-source。 -
创建一个 sink。如果您还没有 sink,您可以使用以下 Knative Service,它将传入消息转储到其日志中
注意
要创建 Knative 服务,您必须在集群上安装 Knative Serving。
-
要创建 sink,请运行命令
kn service create event-display --port 8080 --image gcr.io/knative-releases/knative.dev/eventing/cmd/event_display
-
使用以下示例创建 YAML 文件
apiVersion: apps/v1 kind: Deployment metadata: name: event-display spec: replicas: 1 selector: matchLabels: &labels app: event-display template: metadata: labels: *labels spec: containers: - name: event-display image: gcr.io/knative-releases/knative.dev/eventing/cmd/event_display --- kind: Service apiVersion: v1 metadata: name: event-display spec: selector: app: event-display ports: - protocol: TCP port: 80 targetPort: 8080 -
通过运行命令应用 YAML 文件
其中kubectl apply -f <filename>.yaml<filename>是您在上一步中创建的文件的名称。
-
-
创建具有特定参数和环境设置的具体 ContainerSource
-
要创建 ContainerSource,请运行命令
其中kn source container create <name> --image <image-uri> --sink <sink> -e POD_NAME=<pod-name> -e POD_NAMESPACE=<pod-namespace><name>是您希望您的 ContainerSource 对象的名称,例如test-heartbeats。<image-uri>对应于您在步骤 1 中构建和发布的镜像 URI,例如gcr.io/knative-nightly/knative.dev/eventing/cmd/heartbeats。<pod-name>是容器运行所在的 Pod 的名称,例如mypod。<pod-namespace>是 Pod 运行所在的命名空间,例如event-test。<sink>是您的 sink 的名称,例如event-display。有关可用选项的列表,请参阅 Knative 客户端文档。
-
使用以下模板创建 YAML 文件
其中apiVersion: sources.knative.dev/v1 kind: ContainerSource metadata: name: <containersource-name> spec: template: spec: containers: - image: <event-source-image-uri> name: <container-name> env: - name: POD_NAME value: "<pod-name>" - name: POD_NAMESPACE value: "<pod-namespace>" sink: ref: apiVersion: v1 kind: Service name: <sink><namespace>是您为您的 ContainerSource 创建的命名空间,例如containersource-example。<containersource-name>是您希望您的 ContainerSource 的名称,例如test-heartbeats。<event-source-image-uri>对应于您在步骤 1 中构建和发布的镜像 URI,例如gcr.io/knative-nightly/knative.dev/eventing/cmd/heartbeats。<container-name>是您的事件源的名称,例如heartbeats。<pod-name>是容器运行所在的 Pod 的名称,例如mypod。<pod-namespace>是 Pod 运行所在的命名空间,例如event-test。<sink>是您的 sink 的名称,例如event-display。
有关您可以为 ContainerSource 对象配置的字段的更多信息,请参阅ContainerSource 参考。
-
通过运行命令应用 YAML 文件
其中kubectl apply -f <filename>.yaml<filename>是您在上一步中创建的文件的名称。
注意
参数和环境变量已设置并传递给容器。
-
验证 ContainerSource 对象¶
-
通过运行命令查看您的事件消费者的日志
其中kubectl -n <namespace> logs -l <pod-name> --tail=200<namespace>是包含 ContainerSource 对象的命名空间。<pod-name>是容器运行所在的 Pod 的名称。
例如
$ kubectl -n containersource-example logs -l app=event-display --tail=200 -
验证输出是否返回您的 ContainerSource 发送到您的 sink 的事件的属性。在以下示例中,该命令已返回 ContainerSource 发送到
event-displayService 的事件的Attributes和Data属性☁️ cloudevents.Event Validation: valid Context Attributes, specversion: 1.0 type: dev.knative.eventing.samples.heartbeat source: https://knative.com.cn/eventing/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": 2, "label": "" }
删除 ContainerSource 对象¶
要删除 ContainerSource 对象以及命名空间中的所有相关资源
-
通过运行以下命令删除命名空间
kubectl delete namespace <namespace>其中
<namespace>是包含 ContainerSource 对象的命名空间。