使用 Eventarc 為 Knative 服務建立 BigQuery 處理管道


本教學課程說明如何使用 Eventarc 建構處理管道,排定查詢公開 BigQuery 資料集的作業、根據資料產生圖表,以及透過電子郵件分享圖表連結。

目標

在本教學課程中,您將建構及部署三個 Knative 服務,這些服務會在 Google Kubernetes Engine (GKE) 叢集中執行,並使用 Eventarc 接收事件:

  1. 查詢執行器:在 Cloud Scheduler 作業將訊息發布至 Pub/Sub 主題時觸發;這項服務會使用 BigQuery API 從公開的 COVID-19 資料集擷取資料,並將結果儲存至新的 BigQuery 資料表。
  2. 圖表建立者:查詢執行器服務發布訊息至 Pub/Sub 主題時觸發;這項服務會使用 Python 繪圖程式庫 Matplotlib 產生圖表,並將圖表儲存至 Cloud Storage 值區。
  3. Notifier:當圖表建立者服務將圖表儲存在 Cloud Storage 值區時,這項服務會由稽核記錄觸發,並使用電子郵件服務 SendGrid 將圖表連結傳送至電子郵件地址。

下圖顯示高階架構:

BigQuery 處理管道

費用

在本文件中,您會使用 Google Cloud的下列計費元件:

如要根據預測用量估算費用,請使用 Pricing Calculator

初次使用 Google Cloud 的使用者可能符合免費試用資格。

事前準備

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Install the Google Cloud CLI.

  3. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  4. To initialize the gcloud CLI, run the following command:

    gcloud init
  5. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Artifact Registry, Cloud Build, Cloud Logging, Cloud Scheduler, Eventarc, GKE, Pub/Sub, and Resource Manager APIs:

    gcloud services enable artifactregistry.googleapis.com cloudbuild.googleapis.com cloudresourcemanager.googleapis.com cloudscheduler.googleapis.com container.googleapis.com eventarc.googleapis.com pubsub.googleapis.com run.googleapis.com logging.googleapis.com
  8. Install the Google Cloud CLI.

  9. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  10. To initialize the gcloud CLI, run the following command:

    gcloud init
  11. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  12. Make sure that billing is enabled for your Google Cloud project.

  13. Enable the Artifact Registry, Cloud Build, Cloud Logging, Cloud Scheduler, Eventarc, GKE, Pub/Sub, and Resource Manager APIs:

    gcloud services enable artifactregistry.googleapis.com cloudbuild.googleapis.com cloudresourcemanager.googleapis.com cloudscheduler.googleapis.com container.googleapis.com eventarc.googleapis.com pubsub.googleapis.com run.googleapis.com logging.googleapis.com
  14. 如果是 Cloud Storage,請為 ADMIN_READDATA_WRITEDATA_READ 資料存取類型啟用稽核記錄。

    1. 讀取與 Google Cloud 專案、資料夾或機構相關聯的 Identity and Access Management (IAM) 政策,並儲存在暫時檔案中:
      gcloud projects get-iam-policy PROJECT_ID > /tmp/policy.yaml
    2. 在文字編輯器中開啟 /tmp/policy.yaml,然後在 auditConfigs 區段中新增或變更稽核記錄設定:

      
        auditConfigs:
        - auditLogConfigs:
          - logType: ADMIN_READ
          - logType: DATA_WRITE
          - logType: DATA_READ
          service: storage.googleapis.com
        bindings:
        - members:
        [...]
        etag: BwW_bHKTV5U=
        version: 1
    3. 撰寫新的 IAM 政策:

      gcloud projects set-iam-policy PROJECT_ID /tmp/policy.yaml

      如果上述指令回報與其他變更發生衝突,請重複這些步驟,從讀取 IAM 政策開始。詳情請參閱「使用 API 設定資料存取稽核記錄」一文。

  15. 設定本教學課程中使用的預設值:
    CLUSTER_NAME=events-cluster
    CLUSTER_LOCATION=us-central1
    PROJECT_ID=PROJECT_ID
    gcloud config set project $PROJECT_ID
    gcloud config set run/region $CLUSTER_LOCATION
    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 $CLUSTER_LOCATION

    PROJECT_ID 替換為您的專案 ID。

建立 SendGrid API 金鑰

SendGrid 是一項雲端電子郵件服務,可讓您傳送電子郵件,不必維護電子郵件伺服器。

  1. 登入 SendGrid,然後前往「Settings」>「API Keys」
  2. 按一下「建立 API 金鑰」
  3. 選取金鑰的權限。至少必須具備「郵件傳送」權限,才能傳送電子郵件。
  4. 按一下 [Save] 建立金鑰。
  5. SendGrid 會產生一個新金鑰。此為唯一一份金鑰,因此請務必複製並儲存金鑰,以供日後使用。

建立 GKE 叢集

建立啟用 Workload Identity Federation for GKE 的叢集,以便從 GKE 執行的應用程式存取 Google Cloud 服務。您也需要 Workload Identity Federation for GKE,才能使用 Eventarc 轉送事件。

  1. 建立啟用 CloudRunHttpLoadBalancingHorizontalPodAutoscaling 外掛程式的 Knative 服務 GKE 叢集:

    gcloud beta container clusters create $CLUSTER_NAME \
        --addons=HttpLoadBalancing,HorizontalPodAutoscaling,CloudRun \
        --machine-type=n1-standard-4 \
        --enable-autoscaling --min-nodes=2 --max-nodes=10 \
        --no-issue-client-certificate --num-nodes=2  \
        --logging=SYSTEM,WORKLOAD \
        --monitoring=SYSTEM \
        --scopes=cloud-platform,logging-write,monitoring-write,pubsub \
        --zone us-central1 \
        --release-channel=rapid \
        --workload-pool=$PROJECT_ID.svc.id.goog
    
  2. 請稍候幾分鐘,等待叢集建立完成。在過程中,您可能會看到一些警告,但可以放心忽略。叢集建立完成後,輸出內容會類似如下:

    Creating cluster ...done.
    Created [https://container.googleapis.com/v1beta1/projects/my-project/zones/us-central1/clusters/events-cluster].
    
  3. 建立 Artifact Registry 標準存放區,用於儲存 Docker 容器映像檔:

    gcloud artifacts repositories create REPOSITORY \
        --repository-format=docker \
        --location=$CLUSTER_LOCATION

    REPOSITORY 替換成存放區的專屬名稱。

設定 GKE 服務帳戶

設定 GKE 服務帳戶,做為預設的 Compute 服務帳戶。

  1. 在服務帳戶之間建立身分與存取權管理 (IAM) 繫結:

    PROJECT_NUMBER="$(gcloud projects describe $(gcloud config get-value project) --format='value(projectNumber)')"
    
    gcloud iam service-accounts add-iam-policy-binding \
        --role roles/iam.workloadIdentityUser \
        --member "serviceAccount:$PROJECT_ID.svc.id.goog[default/default]" \
        $PROJECT_NUMBER-compute@developer.gserviceaccount.com
  2. 使用運算服務帳戶的電子郵件地址,將 iam.gke.io/gcp-service-account 註解新增至 GKE 服務帳戶:

    kubectl annotate serviceaccount \
        --namespace default \
        default \
        iam.gke.io/gcp-service-account=$PROJECT_NUMBER-compute@developer.gserviceaccount.com

啟用 GKE 目的地

如要允許 Eventarc 管理 GKE 叢集中的資源,請啟用 GKE 目的地,並將 Eventarc 服務帳戶繫結至必要角色。

  1. 為 Eventarc 啟用 GKE 目的地:

    gcloud eventarc gke-destinations init
  2. 在系統提示繫結必要角色時,輸入 y

    已繫結下列角色:

    • roles/compute.viewer
    • roles/container.developer
    • roles/iam.serviceAccountAdmin

建立服務帳戶並繫結存取角色

建立 Eventarc 觸發條件前,請先設定使用者管理的服務帳戶,並授予特定角色,讓 Eventarc 可以轉送 Pub/Sub 事件。

  1. 建立名為 TRIGGER_GSA 的服務帳戶:

    TRIGGER_GSA=eventarc-bigquery-triggers
    gcloud iam service-accounts create $TRIGGER_GSA
  2. pubsub.subscribermonitoring.metricWritereventarc.eventReceiver 角色指派給服務帳戶:

    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"
    
    gcloud projects add-iam-policy-binding $PROJECT_ID \
        --member "serviceAccount:$TRIGGER_GSA@$PROJECT_ID.iam.gserviceaccount.com" \
        --role "roles/eventarc.eventReceiver"

建立 Cloud Storage 值區

建立 Cloud Storage bucket 來儲存圖表。請確認值區和圖表可公開存取,且與 GKE 服務位於相同區域:

export BUCKET="$(gcloud config get-value core/project)-charts"
gcloud storage buckets create gs://${BUCKET} --location=$(gcloud config get-value run/region)
gcloud storage buckets update gs://${BUCKET} --uniform-bucket-level-access
gcloud storage buckets add-iam-policy-binding gs://${BUCKET} --member=allUsers --role=roles/storage.objectViewer

複製存放區

複製 GitHub 存放區。

git clone https://github.com/GoogleCloudPlatform/eventarc-samples
cd eventarc-samples/processing-pipelines

部署通知器服務

bigquery/notifier/python 目錄部署 Knative serving 服務,接收圖表建立者事件,並使用 SendGrid 透過電子郵件傳送所產生圖表的連結。

  1. 建構及推送容器映像檔:

    pushd bigquery/notifier/python
    export SERVICE_NAME=notifier
    docker build -t $CLUSTER_LOCATION-docker.pkg.dev/$(gcloud config get-value project)/REPOSITORY/${SERVICE_NAME}:v1 .
    docker push $CLUSTER_LOCATION-docker.pkg.dev/$(gcloud config get-value project)/REPOSITORY/${SERVICE_NAME}:v1
    popd
  2. 將容器映像檔部署到 Knative serving,並傳遞要傳送電子郵件的地址和 SendGrid API 金鑰:

    export TO_EMAILS=EMAIL_ADDRESS
    export SENDGRID_API_KEY=YOUR_SENDGRID_API_KEY
    gcloud run deploy ${SERVICE_NAME} \
        --image $CLUSTER_LOCATION-docker.pkg.dev/$(gcloud config get-value project)/REPOSITORY/${SERVICE_NAME}:v1 \
        --update-env-vars TO_EMAILS=${TO_EMAILS},SENDGRID_API_KEY=${SENDGRID_API_KEY},BUCKET=${BUCKET}

    更改下列內容:

    • EMAIL_ADDRESS:要傳送所產生圖表連結的電子郵件地址
    • YOUR_SENDGRID_API_KEY:先前記下的 SendGrid API 金鑰

看到服務網址時,表示部署作業已完成。

為通知程式服務建立觸發條件

部署在 Knative 服務上的通知程式服務的 Eventarc 觸發條件,會篩選 methodName 為 storage.objects.create 的 Cloud Storage 稽核記錄。

  1. 建立觸發條件:

    gcloud eventarc triggers create trigger-${SERVICE_NAME}-gke \
        --destination-gke-cluster=$CLUSTER_NAME \
        --destination-gke-location=$CLUSTER_LOCATION \
        --destination-gke-namespace=default \
        --destination-gke-service=$SERVICE_NAME \
        --destination-gke-path=/ \
        --event-filters="type=google.cloud.audit.log.v1.written" \
        --event-filters="serviceName=storage.googleapis.com" \
        --event-filters="methodName=storage.objects.create" \
        --service-account=$TRIGGER_GSA@$PROJECT_ID.iam.gserviceaccount.com

    這項操作會建立名為 trigger-notifier-gke 的觸發條件。

部署圖表建立服務

bigquery/chart-creator/python 目錄部署 Knative 服務,接收查詢執行器事件、從特定國家/地區的 BigQuery 資料表擷取資料,然後使用 Matplotlib 從資料產生圖表。圖表會上傳至 Cloud Storage bucket。

  1. 建構及推送容器映像檔:

    pushd bigquery/chart-creator/python
    export SERVICE_NAME=chart-creator
    docker build -t $CLUSTER_LOCATION-docker.pkg.dev/$(gcloud config get-value project)/REPOSITORY/${SERVICE_NAME}:v1 .
    docker push $CLUSTER_LOCATION-docker.pkg.dev/$(gcloud config get-value project)/REPOSITORY/${SERVICE_NAME}:v1
    popd
  2. 將容器映像檔部署至 Knative serving,並傳遞 BUCKET

    gcloud run deploy ${SERVICE_NAME} \
        --image $CLUSTER_LOCATION-docker.pkg.dev/$(gcloud config get-value project)/REPOSITORY/${SERVICE_NAME}:v1 \
        --update-env-vars BUCKET=${BUCKET}

看到服務網址時,表示部署作業已完成。

為圖表建立服務建立觸發條件

部署在 Knative 服務上的圖表建立服務的 Eventarc 觸發程序,會篩選發布至 Pub/Sub 主題的訊息。

  1. 建立觸發條件:

    gcloud eventarc triggers create trigger-${SERVICE_NAME}-gke \
        --destination-gke-cluster=$CLUSTER_NAME \
        --destination-gke-location=$CLUSTER_LOCATION \
        --destination-gke-namespace=default \
        --destination-gke-service=$SERVICE_NAME \
        --destination-gke-path=/ \
        --event-filters="type=google.cloud.pubsub.topic.v1.messagePublished" \
        --service-account=$TRIGGER_GSA@$PROJECT_ID.iam.gserviceaccount.com

    這項操作會建立名為 trigger-chart-creator-gke 的觸發條件。

  2. 設定 Pub/Sub 主題環境變數。

    export TOPIC_QUERY_COMPLETED=$(basename $(gcloud eventarc triggers describe trigger-${SERVICE_NAME}-gke --format='value(transport.pubsub.topic)'))

部署查詢執行器服務

processing-pipelines 目錄部署 Knative 服務,接收 Cloud Scheduler 事件、從公開的 COVID-19 資料集擷取資料,並將結果儲存至新的 BigQuery 資料表。

  1. 建構及推送容器映像檔:

    export SERVICE_NAME=query-runner
    docker build -t $CLUSTER_LOCATION-docker.pkg.dev/$(gcloud config get-value project)/REPOSITORY/${SERVICE_NAME}:v1 -f Dockerfile .
    docker push $CLUSTER_LOCATION-docker.pkg.dev/$(gcloud config get-value project)/REPOSITORY/${SERVICE_NAME}:v1
  2. 將容器映像檔部署至 Knative serving,並傳遞 PROJECT_IDTOPIC_QUERY_COMPLETED

    gcloud run deploy ${SERVICE_NAME} \
        --image $CLUSTER_LOCATION-docker.pkg.dev/$(gcloud config get-value project)/REPOSITORY/${SERVICE_NAME}:v1 \
        --update-env-vars PROJECT_ID=$(gcloud config get-value project),TOPIC_ID=${TOPIC_QUERY_COMPLETED}

看到服務網址時,表示部署作業已完成。

為查詢執行器服務建立觸發條件

在 Knative 服務上部署的查詢執行器服務,其 Eventarc 觸發程序會篩選發布至 Pub/Sub 主題的訊息。

  1. 建立觸發條件:

    gcloud eventarc triggers create trigger-${SERVICE_NAME}-gke \
        --destination-gke-cluster=$CLUSTER_NAME \
        --destination-gke-location=$CLUSTER_LOCATION \
        --destination-gke-namespace=default \
        --destination-gke-service=$SERVICE_NAME \
        --destination-gke-path=/ \
        --event-filters="type=google.cloud.pubsub.topic.v1.messagePublished" \
        --service-account=$TRIGGER_GSA@$PROJECT_ID.iam.gserviceaccount.com

    這項操作會建立名為 trigger-query-runner-gke 的觸發條件。

  2. 設定 Pub/Sub 主題的環境變數。

    export TOPIC_QUERY_SCHEDULED=$(gcloud eventarc triggers describe trigger-${SERVICE_NAME}-gke --format='value(transport.pubsub.topic)')

排定工作

處理管道是由兩項 Cloud Scheduler 工作觸發。

  1. 建立 Cloud Scheduler 必須使用的 App Engine 應用程式,並指定適當的 位置 (例如 europe-west):

    export APP_ENGINE_LOCATION=LOCATION
    gcloud app create --region=${APP_ENGINE_LOCATION}
  2. 建立兩項 Cloud Scheduler 工作,每天發布至 Pub/Sub 主題一次:

    gcloud scheduler jobs create pubsub cre-scheduler-uk \
        --schedule="0 16 * * *" \
        --topic=${TOPIC_QUERY_SCHEDULED} \
        --message-body="United Kingdom"
    gcloud scheduler jobs create pubsub cre-scheduler-cy \
        --schedule="0 17 * * *" \
        --topic=${TOPIC_QUERY_SCHEDULED} \
        --message-body="Cyprus"

    排程以 Unix-Cron 格式指定。 舉例來說,0 16 * * * 表示工作每天會在世界標準時間下午 4:00 執行。

執行管道

  1. 確認所有觸發條件都已建立成功:

    gcloud eventarc triggers list

    畫面會顯示如下的輸出內容:

    NAME                       TYPE                                            DESTINATION         ACTIVE  LOCATION
    trigger-chart-creator-gke  google.cloud.pubsub.topic.v1.messagePublished   GKE:chart-creator   Yes     us-central1
    trigger-notifier-gke       google.cloud.audit.log.v1.written               GKE:notifier        Yes     us-central1
    trigger-query-runner-gke   google.cloud.pubsub.topic.v1.messagePublished   GKE:query-runner    Yes     us-central1
    
  2. 擷取 Cloud Scheduler 工作 ID:

    gcloud scheduler jobs list

    畫面會顯示如下的輸出內容:

    ID                LOCATION      SCHEDULE (TZ)         TARGET_TYPE  STATE
    cre-scheduler-cy  us-central1   0 17 * * * (Etc/UTC)  Pub/Sub      ENABLED
    cre-scheduler-uk  us-central1   0 16 * * * (Etc/UTC)  Pub/Sub      ENABLED
    
  3. 雖然系統已排定工作在每天下午 4 點和 5 點執行,您也可以手動執行 Cloud Scheduler 工作:

    gcloud scheduler jobs run cre-scheduler-cy
    gcloud scheduler jobs run cre-scheduler-uk
  4. 幾分鐘後,確認 Cloud Storage bucket 中有兩個圖表:

    gcloud storage ls gs://${BUCKET}

    畫面會顯示如下的輸出內容:

    gs://PROJECT_ID-charts/chart-cyprus.png
    gs://PROJECT_ID-charts/chart-unitedkingdom.png
    

恭喜!您也會收到兩封電子郵件,內含圖表連結。

清除所用資源

如果您是為了這個教學課程建立新專案,請刪除該專案。如果您使用現有專案,並想保留專案,但不要本教學課程新增的變更,請刪除為本教學課程建立的資源

    Delete a Google Cloud project:

    gcloud projects delete PROJECT_ID

刪除教學課程資源

  1. 刪除您在本教學課程中部署的任何 Knative serving 服務:

    gcloud run services delete SERVICE_NAME

    其中 SERVICE_NAME 是您選擇的服務名稱。

    您也可以從 Google Cloud 主控台刪除 Knative 服務。

  2. 刪除您在本教學課程中建立的任何 Eventarc 觸發條件:

    gcloud eventarc triggers delete TRIGGER_NAME
    

    TRIGGER_NAME 替換為觸發條件的名稱。

  3. 移除您在教學課程設定期間新增的任何 Google Cloud CLI 預設設定。

    gcloud config unset project
    gcloud config unset run/cluster
    gcloud config unset run/cluster_location
    gcloud config unset run/platform
    gcloud config unset eventarc/location
    gcloud config unset compute/zone
  4. 從 Artifact Registry 刪除映像檔。

    gcloud artifacts docker images delete $CLUSTER_LOCATION-docker.pkg.dev/$(gcloud config get-value project)/REPOSITORY/notifier:v1
    gcloud artifacts docker images delete $CLUSTER_LOCATION-docker.pkg.dev/$(gcloud config get-value project)/REPOSITORY/chart-creator:v1
    gcloud artifacts docker images delete $CLUSTER_LOCATION-docker.pkg.dev/$(gcloud config get-value project)/REPOSITORY/query-runner:v1
  5. 刪除值區和值區內的所有物件:

    gcloud storage rm --recursive gs://${BUCKET}/
  6. 刪除 Cloud Scheduler 工作:

    gcloud scheduler jobs delete cre-scheduler-cy
    gcloud scheduler jobs delete cre-scheduler-uk

後續步驟