跳至内容

创建 ApiServerSource 对象

version

本主题介绍如何创建 ApiServerSource 对象。

开始之前

在您创建 ApiServerSource 对象之前

  • 您必须在您的集群上安装 Knative Eventing
  • 您必须安装 kubectl CLI 工具。
  • 可选:如果您想使用 kn 命令,请安装 kn 工具。

创建 ApiServerSource 对象

  1. 可选:通过运行以下命令为 API 服务器源实例创建命名空间

    kubectl create namespace <namespace>
    
    其中 <namespace> 是您要创建的命名空间的名称。

    注意

    为您的 ApiServerSource 和相关组件创建命名空间可以让您更轻松地查看此工作流的更改和事件,因为这些更改和事件与可能存在于您的 default 命名空间中的其他组件隔离开来。

    它还使移除源变得更容易,因为您可以删除命名空间以移除所有资源。

  2. 创建 ServiceAccount

    1. 使用以下模板创建一个 YAML 文件

      apiVersion: v1
      kind: ServiceAccount
      metadata:
        name: <service-account>
        namespace: <namespace>
      
      其中

      • <service-account> 是您要创建的 ServiceAccount 的名称。
      • <namespace> 是您之前在步骤 1 中创建的命名空间。
    2. 通过运行以下命令应用 YAML 文件

      kubectl apply -f <filename>.yaml
      
      其中 <filename> 是您在先前步骤中创建的文件的名称。

  3. 创建角色

    1. 使用以下模板创建一个 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
        

        注意

        唯一需要的动词是 getlistwatch

    2. 通过运行以下命令应用 YAML 文件

      kubectl apply -f <filename>.yaml
      
      其中 <filename> 是您在先前步骤中创建的文件的名称。

  4. 创建 RoleBinding

    1. 使用以下模板创建一个 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 的名称。
    2. 通过运行以下命令应用 YAML 文件

      kubectl apply -f <filename>.yaml
      
      其中 <filename> 是您在先前步骤中创建的文件的名称。

  5. 创建接收器。如果您没有自己的接收器,您可以使用以下示例服务,该服务将传入消息转储到日志中

    1. 将以下 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 中创建的命名空间的名称。

    2. 通过运行以下命令应用 YAML 文件

      kubectl apply -f <filename>.yaml
      
      其中 <filename> 是您在先前步骤中创建的文件的名称。

  6. 创建 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 客户端文档

    1. 使用以下模板创建一个 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 对象,例如,ServiceDeployment
      • <sink-name> 是您的接收器的名称。

      有关您可以为 ApiServerSource 对象配置的字段的更多信息,请参阅 ApiServerSource 参考

    2. 通过运行以下命令应用 YAML 文件

      kubectl apply -f <filename>.yaml
      
      其中 <filename> 是您在先前步骤中创建的文件的名称。

验证 ApiServerSource 对象

  1. 通过在您的命名空间中启动一个测试 Pod 来使 Kubernetes API 服务器创建事件,方法是运行以下命令

    kubectl run busybox --image=busybox --namespace=<namespace> --restart=Never -- ls
    
    其中 <namespace> 是您之前在步骤 1 中创建的命名空间的名称。

  2. 通过运行以下命令删除测试 Pod

    kubectl --namespace=<namespace> delete pod busybox
    
    其中 <namespace> 是您之前在步骤 1 中创建的命名空间的名称。

  3. 通过运行以下命令查看日志以验证 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 中创建的命名空间的名称。

我们使用分析和 Cookie 来了解网站流量。有关您使用我们网站的信息将为此目的与 Google 共享。 了解更多。