您可以遷移事件觸發條件,讓 Google Kubernetes Engine (GKE) 服務 (包括 Knative 服務) 使用 Eventarc 接收 Pub/Sub 事件。本指南假設您要遷移現有的事件觸發條件,且 GKE 服務是在 GKE 叢集中執行。您必須為每個叢集執行遷移作業。
這個遷移路徑可避免遺失事件,並盡量減少重複事件。
遷移作業包含下列項目:
- 如要允許 Eventarc 管理 GKE 叢集中的資源,請啟用 GKE 目的地。
- 如有需要,請為執行目標 GKE 服務的 GKE 叢集啟用 Workload Identity。
- 找出現有事件觸發條件。
- 建立 Eventarc 觸發條件,指向與現有觸發條件相同的 GKE 服務。
- 確認 Eventarc 觸發條件是否如預期傳送事件。
- 刪除原始事件觸發條件,並在遷移後清除所用資源。
事前準備
本指南假設您已具備下列條件:
設定指令列環境
如要設定 gcloud
和 kubectl
指令列工具,請按照下列步驟操作:
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
登入帳戶:
gcloud auth login
設定 gcloud CLI 的預設設定:
gcloud config set project PROJECT_ID gcloud config set run/cluster CLUSTER_NAME gcloud config set run/cluster_location CLUSTER_LOCATION gcloud config set run/platform gke gcloud config set eventarc/location LOCATION
更改下列內容:
為專案啟用下列 API:
gcloud services enable cloudapis.googleapis.com gcloud services enable cloudbuild.googleapis.com gcloud services enable cloudresourcemanager.googleapis.com gcloud services enable container.googleapis.com gcloud services enable containerregistry.googleapis.com gcloud services enable eventarc.googleapis.com
安裝
kubectl
指令列工具:gcloud components install kubectl
更新已安裝的 gcloud CLI 元件:
gcloud components update
為 Eventarc 啟用 GKE 目的地:
gcloud eventarc gke-destinations init
系統提示繫結必要角色時,請輸入
y
。已繫結下列角色:
roles/compute.viewer
roles/container.developer
roles/iam.serviceAccountAdmin
列出 GKE 叢集的現有事件觸發條件:
gcloud beta events triggers list --namespace EVENTS_NAMESPACE
請將
EVENTS_NAMESPACE
改成您事件代理程式的命名空間。輸出結果會與下列內容相似:
TRIGGER EVENT TYPE TARGET ✔ trigger-id google.cloud.pubsub.topic.v1.messagePublished cloud-run-service-name
您需要主題 ID 才能建立 Eventarc 觸發條件。 擷取現有事件觸發條件的主題 ID:
gcloud beta events triggers describe TRIGGER_ID \ --platform gke --namespace EVENTS_NAMESPACE \ --format="flattened(serialized_source.spec.topic,serialized_trigger.spec.filter.attributes.type,serialized_trigger.spec.subscriber.ref.name,serialized_trigger.spec.subscriber.ref.namespace)"
將
TRIGGER_ID
替換為現有事件觸發條件的 ID 或完整識別碼。輸出結果會與下列內容相似:
topic: topic-id type: google.cloud.pubsub.topic.v1.messagePublished name: cloud-run-service-name namespace: events
建立 Google Cloud 服務帳戶 (GSA):
TRIGGER_GSA=SERVICE_ACCOUNT_NAME gcloud iam service-accounts create $TRIGGER_GSA
將
SERVICE_ACCOUNT_NAME
改為長度介於 6 至 30 個字元的名稱。可使用小寫英數字元和破折號。將
pubsub.subscriber
和monitoring.metricWriter
角色指派給服務帳戶:PROJECT_ID=$(gcloud config get-value project) gcloud projects add-iam-policy-binding $PROJECT_ID \ --member "serviceAccount:$TRIGGER_GSA@$PROJECT_ID.iam.gserviceaccount.com" \ --role "roles/pubsub.subscriber" gcloud projects add-iam-policy-binding $PROJECT_ID \ --member "serviceAccount:$TRIGGER_GSA@$PROJECT_ID.iam.gserviceaccount.com" \ --role "roles/monitoring.metricWriter"
根據現有事件觸發條件的設定,建立新的 Eventarc 觸發條件。所有引數 (包括目的地服務、叢集和主題 ID) 都應與現有事件觸發條件的引數相符。
gcloud eventarc triggers create EVENTARC_TRIGGER_NAME \ --event-filters="type=google.cloud.pubsub.topic.v1.messagePublished" \ --location LOCATION \ --destination-gke-service=DESTINATION_SERVICE \ --destination-gke-cluster=CLUSTER_NAME \ --destination-gke-location=CLUSTER_LOCATION \ --destination-gke-namespace=EVENTS_NAMESPACE \ --destination-gke-path=/ \ --service-account=$TRIGGER_GSA@$PROJECT_ID.iam.gserviceaccount.com \ --transport-topic=projects/PROJECT_ID/topics/TOPIC_ID
將
EVENTARC_TRIGGER_NAME
換成新 Eventarc 觸發程序的名稱,並將 TOPIC_ID 換成您先前擷取的 Pub/Sub 主題 ID。這會為 Pub/Sub 主題 ID 建立 Eventarc 觸發條件。
確認觸發條件是否已成功建立:
gcloud eventarc triggers list
輸出結果會與下列內容相似:
NAME TYPE DESTINATION_RUN_SERVICE DESTINATION_RUN_PATH ACTIVE eventarc-trigger-name google.cloud.pubsub.topic.v1.messagePublished Yes
將訊息發布至 Pub/Sub 主題:
gcloud pubsub topics publish TOPIC_ID \ --message "Hello, World!"
確認事件和是否成功傳送。舉例來說,您可以查看接收端 GKE 服務的記錄:
kubectl logs \ --selector serving.knative.dev/service=DESTINATION_SERVICE \ -c user-container \ -n EVENTS_NAMESPACE \ --tail=100
這兩個觸發程序的輸出內容類似如下,只有
Context Attributes
和Extensions
不同。實際訊息資料 (subscription
資料除外) 應完全相同,這樣在遷移至 Eventarc 後,就不需要變更任何事件接收器。Eventarc 事件:
☁️ cloudevents.Event Validation: valid Context Attributes, specversion: 1.0 type: google.cloud.pubsub.topic.v1.messagePublished source: //pubsub.googleapis.com/projects/project-id/topics/topic-name id: 2759155988927083 time: 2021-07-22T17:47:19.964Z datacontenttype: application/json Data, { "subscription": "projects/project-id/subscriptions/eventarc-us-central1-eventarc-trigger-name-sub-567", "message": { "data": "V29ybGQ=", "messageId": "2759155988927083", "publishTime": "2021-07-22T17:47:19.964Z" } }
Knative serving 事件:
☁️ cloudevents.Event Validation: valid Context Attributes, specversion: 1.0 type: google.cloud.pubsub.topic.v1.messagePublished source: //pubsub.googleapis.com/projects/project-id/topics/topic-name id: 2759155988927083 time: 2021-07-22T17:47:19.964Z dataschema: https://raw.githubusercontent.com/googleapis/google-cloudevents/master/proto/google/events/cloud/pubsub/v1/data.proto datacontenttype: application/json Extensions, knativearrivaltime: 2021-07-22T17:47:20.723302362Z knsourcetrigger: link0.09767362059083662 traceparent: 00-f3cc6d754d361a0e49e83e5973fa3565-b4a20ef4ecffe96b-00 Data, { "subscription": "cre-src_events_source-for-trigger-name_5ffa9638-8cab-4010-900a-2edb275d9eaf", "message": { "messageId": "2759155988927083", "data": "V29ybGQ=", "publishTime": "2021-07-22T17:47:19.964Z" } }
刪除原始事件觸發條件:
gcloud beta events triggers delete TRIGGER_NAME \ --platform gke \ --namespace EVENTS_NAMESPACE \ --quiet
啟用 GKE 目的地
如要允許 Eventarc 管理 GKE 叢集中的資源,請啟用 GKE 目的地,並將 Eventarc 服務帳戶繫結至必要角色。
在叢集上啟用 Workload Identity Federation for GKE
如果叢集已啟用 Workload Identity Federation for GKE,可以略過這個步驟。
Workload Identity Federation for GKE 的安全性較高,且更易於管理,因此建議您使用這項功能,從 GKE 執行的應用程式存取 Google Cloud 服務。您也需要使用 Eventarc 轉送 GKE 事件。
如要在現有叢集上啟用 Workload Identity Federation for GKE,請參閱「使用 Workload Identity」。
找出現有的事件觸發條件
遷移任何現有事件觸發條件前,請先擷取觸發條件詳細資料。
建立 Eventarc 觸發條件,取代現有觸發條件
建立 Eventarc 觸發程序前,請先設定使用者管理的服務帳戶,並授予特定角色,讓 Eventarc 管理 GKE 目的地事件。
測試遷移作業並驗證輸出內容
如要測試遷移作業,請發布至 Pub/Sub 主題來觸發測試事件,然後確認每個現有事件觸發條件和 Eventarc 觸發條件分別至少傳送一個事件。
舉例來說,觸發測試事件後,輸出內容會重複,且類似下列內容:
Hello, World! ID: 2896291125947199 Hello, World! ID: 2896291125947199
您也可以參考下列情境,瞭解事件接收器如何顯示事件資料:
遷移後清除所用資源
測試並確認觸發條件已遷移至 Eventarc 後,即可刪除原始事件觸發條件。
您已將現有的 Knative serving 觸發條件遷移至 Eventarc。