建立 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>
是您想要用作接收器的任何支援的可定址物件,例如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 中建立的命名空間名稱。 -
執行以下命令來檢視記錄,以驗證 Kubernetes 事件是否已由 Knative Eventing 系統傳送到接收器
其中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 中建立的命名空間名稱。