创建 ApiServerSource 对象¶
本主题介绍如何创建 ApiServerSource 对象。
开始之前¶
在您创建 ApiServerSource 对象之前
- 您必须在您的集群上安装 Knative Eventing。
- 您必须安装
kubectl
CLI 工具。 - 可选:如果您想使用
kn
命令,请安装kn
工具。
创建 ApiServerSource 对象¶
-
可选:通过运行以下命令为 API 服务器源实例创建命名空间
其中kubectl create namespace <namespace>
<namespace>
是您要创建的命名空间的名称。注意
为您的 ApiServerSource 和相关组件创建命名空间可以让您更轻松地查看此工作流的更改和事件,因为这些更改和事件与可能存在于您的
default
命名空间中的其他组件隔离开来。
它还使移除源变得更容易,因为您可以删除命名空间以移除所有资源。 -
创建 ServiceAccount
-
使用以下模板创建一个 YAML 文件
其中apiVersion: v1 kind: ServiceAccount metadata: name: <service-account> namespace: <namespace>
<service-account>
是您要创建的 ServiceAccount 的名称。<namespace>
是您之前在步骤 1 中创建的命名空间。
-
通过运行以下命令应用 YAML 文件
其中kubectl apply -f <filename>.yaml
<filename>
是您在先前步骤中创建的文件的名称。
-
-
创建角色
-
使用以下模板创建一个 YAML 文件
其中apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: <role> namespace: <namespace> rules: <rules>
<role>
是您要创建的角色的名称。<namespace>
是您之前在步骤 1 中创建的命名空间的名称。-
<rules>
是您要授予 APIServerSource 对象的一组权限。这组权限必须与您要接收事件的资源匹配。例如,要接收与events
资源相关的事件,请使用以下权限集- apiGroups: - "" resources: - events verbs: - get - list - watch
注意
唯一需要的动词是
get
、list
和watch
。
-
通过运行以下命令应用 YAML 文件
其中kubectl apply -f <filename>.yaml
<filename>
是您在先前步骤中创建的文件的名称。
-
-
创建 RoleBinding
-
使用以下模板创建一个 YAML 文件
其中apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: <role-binding> namespace: <namespace> roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: <role> subjects: - kind: ServiceAccount name: <service-account> namespace: <namespace>
<role-binding>
是您要创建的 RoleBinding 的名称。<namespace>
是您之前在步骤 1 中创建的命名空间的名称。<role>
是您之前在步骤 3 中创建的角色的名称。<service-account>
是您之前在步骤 2 中创建的 ServiceAccount 的名称。
-
通过运行以下命令应用 YAML 文件
其中kubectl apply -f <filename>.yaml
<filename>
是您在先前步骤中创建的文件的名称。
-
-
创建接收器。如果您没有自己的接收器,您可以使用以下示例服务,该服务将传入消息转储到日志中
-
将以下 YAML 复制到一个文件中
apiVersion: apps/v1 kind: Deployment metadata: name: event-display namespace: <namespace> 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 namespace: <namespace> spec: selector: app: event-display ports: - protocol: TCP port: 80 targetPort: 8080
其中
<namespace>
是您在上面步骤 1 中创建的命名空间的名称。 -
通过运行以下命令应用 YAML 文件
其中kubectl apply -f <filename>.yaml
<filename>
是您在先前步骤中创建的文件的名称。
-
-
创建 ApiServerSource 对象
-
要创建 ApiServerSource,请运行以下命令
其中kn source apiserver create <apiserversource> \ --namespace <namespace> \ --mode "Resource" \ --resource "Event:v1" \ --service-account <service-account> \ --sink <sink-name>
<apiserversource>
是您要创建的源的名称。<namespace>
是您之前在步骤 1 中创建的命名空间的名称。<service-account>
是您之前在步骤 2 中创建的 ServiceAccount 的名称。<sink-name>
是您的接收器的名称,例如,http://event-display.pingsource-example.svc.cluster.local
。
有关可用选项的列表,请参阅 Knative 客户端文档。
-
使用以下模板创建一个 YAML 文件
其中apiVersion: sources.knative.dev/v1 kind: ApiServerSource metadata: name: <apiserversource-name> namespace: <namespace> spec: serviceAccountName: <service-account> mode: <event-mode> resources: - apiVersion: v1 kind: Event sink: ref: apiVersion: v1 kind: <sink-kind> name: <sink-name>
<apiserversource-name>
是您要创建的源的名称。<namespace>
是您之前在步骤 1 中创建的命名空间的名称。<service-account>
是您之前在步骤 2 中创建的 ServiceAccount 的名称。<event-mode>
既可以是Resource
,也可以是Reference
。如果设置为Resource
,则事件有效负载包含事件所针对的整个资源。如果设置为Reference
,则事件有效负载仅包含对事件所针对的资源的引用。默认值为Reference
。<sink-kind>
是您要用作接收器的任何支持的 Addressable 对象,例如,Service
或Deployment
。<sink-name>
是您的接收器的名称。
有关您可以为 ApiServerSource 对象配置的字段的更多信息,请参阅 ApiServerSource 参考。
-
通过运行以下命令应用 YAML 文件
其中kubectl apply -f <filename>.yaml
<filename>
是您在先前步骤中创建的文件的名称。
-
验证 ApiServerSource 对象¶
-
通过在您的命名空间中启动一个测试 Pod 来使 Kubernetes API 服务器创建事件,方法是运行以下命令
其中kubectl run busybox --image=busybox --namespace=<namespace> --restart=Never -- ls
<namespace>
是您之前在步骤 1 中创建的命名空间的名称。 -
通过运行以下命令删除测试 Pod
其中kubectl --namespace=<namespace> delete pod busybox
<namespace>
是您之前在步骤 1 中创建的命名空间的名称。 -
通过运行以下命令查看日志以验证 Knative Eventing 系统是否将 Kubernetes 事件发送到了接收器
其中kubectl logs --namespace=<namespace> -l app=<sink> --tail=100
<namespace>
是您之前在步骤 1 中创建的命名空间的名称。<sink>
是您之前在步骤 5 中用作接收器的 PodSpecable 对象的名称。
示例日志输出
☁️ cloudevents.Event Validation: valid Context Attributes, specversion: 1.0 type: dev.knative.apiserver.resource.update source: https://10.96.0.1:443 subject: /apis/v1/namespaces/apiserversource-example/events/testevents.15dd3050eb1e6f50 id: e0447eb7-36b5-443b-9d37-faf4fe5c62f0 time: 2020-07-28T19:14:54.719501054Z datacontenttype: application/json Extensions, kind: Event name: busybox.1626008649e617e3 namespace: apiserversource-example Data, { "apiVersion": "v1", "count": 1, "eventTime": null, "firstTimestamp": "2020-07-28T19:14:54Z", "involvedObject": { "apiVersion": "v1", "fieldPath": "spec.containers{busybox}", "kind": "Pod", "name": "busybox", "namespace": "apiserversource-example", "resourceVersion": "28987493", "uid": "1efb342a-737b-11e9-a6c5-42010a8a00ed" }, "kind": "Event", "lastTimestamp": "2020-07-28T19:14:54Z", "message": "Started container", "metadata": { "creationTimestamp": "2020-07-28T19:14:54Z", "name": "busybox.1626008649e617e3", "namespace": "default", "resourceVersion": "506088", "selfLink": "/api/v1/namespaces/apiserversource-example/events/busybox.1626008649e617e3", "uid": "2005af47-737b-11e9-a6c5-42010a8a00ed" }, "reason": "Started", "reportingComponent": "", "reportingInstance": "", "source": { "component": "kubelet", "host": "gke-knative-auto-cluster-default-pool-23c23c4f-xdj0" }, "type": "Normal" }
删除 ApiServerSource 对象¶
要移除 ApiServerSource 对象和所有相关资源
-
通过运行以下命令删除命名空间
其中kubectl delete namespace <namespace>
<namespace>
是您之前在步骤 1 中创建的命名空间的名称。