本教學課程將說明如何排除使用 Eventarc 將 Cloud Storage 事件轉送至未經驗證 Cloud Run 服務時,遇到的執行階段錯誤。
目標
本教學課程將說明如何完成下列工作:
- 建立 Artifact Registry 標準存放區,用於儲存容器映像檔。
- 建立 Cloud Storage bucket 做為事件來源。
- 建構容器映像檔,並將其上傳及部署至 Cloud Run。
- 建立 Eventarc 觸發條件。
- 將檔案上傳至 Cloud Storage 值區。
- 排解並修正執行階段錯誤。
費用
在本文件中,您會使用 Google Cloud的下列計費元件:
您可以使用 Pricing Calculator 根據預測用量產生預估費用。
事前準備
貴機構定義的安全性限制,可能會導致您無法完成下列步驟。如需疑難排解資訊,請參閱「在受限的 Google Cloud 環境中開發應用程式」。
- 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.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
如果您是專案建立者,系統會授予您基本擁有者角色 (
roles/owner
)。根據預設,這個身分與存取權管理 (IAM) 角色會包含完整存取大多數 Google Cloud資源所需的權限,因此您可以略過這個步驟。如果您不是專案建立者,則必須將必要權限授予專案中的適當實體。舉例來說,主體可以是 Google 帳戶 (適用於使用者),也可以是服務帳戶 (適用於應用程式和運算工作負載)。詳情請參閱活動目的地的角色和權限頁面。
請注意,根據預設,Cloud Build 權限包含上傳及下載 Artifact Registry 構件權限。
所需權限
如要取得完成本教學課程所需的權限,請要求管理員為您授予專案的下列 IAM 角色:
-
Cloud Build 編輯器 (
roles/cloudbuild.builds.editor
) -
Cloud Run 管理員 (
roles/run.admin
) -
Eventarc 管理員 (
roles/eventarc.admin
) -
記錄檔檢視存取者 (
roles/logging.viewAccessor
) -
專案 IAM 管理員 (
roles/resourcemanager.projectIamAdmin
) -
服務帳戶管理員 (
roles/iam.serviceAccountAdmin
) -
服務帳戶使用者 (
roles/iam.serviceAccountUser
) -
服務使用情形管理員 (
roles/serviceusage.serviceUsageAdmin
) -
儲存空間管理員 (
roles/storage.admin
)
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。
-
Cloud Build 編輯器 (
- 針對 Cloud Storage,請為
ADMIN_READ
、DATA_WRITE
和DATA_READ
資料存取類型啟用稽核記錄。- 讀取與 Google Cloud 專案、資料夾或機構相關的身分與存取權管理 (IAM) 政策,並將其儲存在暫存檔中:
gcloud projects get-iam-policy PROJECT_ID > /tmp/policy.yaml
- 在文字編輯器中開啟
/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
- 編寫新的 IAM 政策:
gcloud projects set-iam-policy PROJECT_ID /tmp/policy.yaml
如果上述指令回報與其他變更衝突,請重複執行這些步驟,從讀取 IAM 政策開始。詳情請參閱「使用 API 設定資料存取稽核記錄」一文。
- 讀取與 Google Cloud 專案、資料夾或機構相關的身分與存取權管理 (IAM) 政策,並將其儲存在暫存檔中:
- 將
eventarc.eventReceiver
角色授予 Compute Engine 服務帳戶:export PROJECT_NUMBER="$(gcloud projects describe $(gcloud config get-value project) --format='value(projectNumber)')" gcloud projects add-iam-policy-binding $(gcloud config get-value project) \ --member=serviceAccount:${PROJECT_NUMBER}-compute@developer.gserviceaccount.com \ --role='roles/eventarc.eventReceiver'
- 如果您是在 2021 年 4 月 8 日當天或之前啟用 Pub/Sub 服務帳戶,請將
iam.serviceAccountTokenCreator
角色授予 Pub/Sub 服務帳戶:gcloud projects add-iam-policy-binding $(gcloud config get-value project) \ --member="serviceAccount:service-${PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com"\ --role='roles/iam.serviceAccountTokenCreator'
- 設定本教學課程中使用的預設值:
export REGION=us-central1 gcloud config set run/region ${REGION} gcloud config set run/platform managed gcloud config set eventarc/location ${REGION}
建立 Artifact Registry 標準存放區
建立 Artifact Registry 標準存放區來儲存容器映像檔:
gcloud artifacts repositories create REPOSITORY \ --repository-format=docker \ --location=$REGION
將 REPOSITORY
替換為儲存庫的專屬名稱。
建立 Cloud Storage 值區
在兩個地區中建立 Cloud Storage bucket,做為 Cloud Run 服務的事件來源:
在
us-east1
中建立 bucket:export BUCKET1="troubleshoot-bucket1-PROJECT_ID" gsutil mb -l us-east1 gs://${BUCKET1}
在
us-west1
中建立 bucket:export BUCKET2="troubleshoot-bucket2-PROJECT_ID" gsutil mb -l us-west1 gs://${BUCKET2}
建立事件來源後,請在 Cloud Run 上部署事件接收器服務。
部署事件接收器
部署可接收及記錄事件的 Cloud Run 服務。
複製 GitHub 存放區來擷取程式碼範例:
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples.git cd golang-samples/eventarc/audit_storage
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git cd java-docs-samples/eventarc/audit-storage
.NET
git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git cd dotnet-docs-samples/eventarc/audit-storage
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git cd nodejs-docs-samples/eventarc/audit-storage
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git cd python-docs-samples/eventarc/audit-storage
請查看本教學課程的程式碼,其中包含下列項目:
事件處理常式會在 HTTP
POST
要求中,將傳入的事件接收為 CloudEvent:Go
Java
.NET
Node.js
Python
使用事件處理常式的伺服器:
Go
Java
.NET
Node.js
Python
定義服務作業環境的 Dockerfile。Dockerfile 的內容依程式語言而有所不同:
Go
Java
.NET
Node.js
Python
使用 Cloud Build 建構容器映像檔,並將映像檔上傳至 Artifact Registry:
export PROJECT_ID=$(gcloud config get-value project) export SERVICE_NAME=troubleshoot-service gcloud builds submit --tag $REGION-docker.pkg.dev/${PROJECT_ID}/REPOSITORY/${SERVICE_NAME}:v1
將容器映像檔部署至 Cloud Run:
gcloud run deploy ${SERVICE_NAME} \ --image $REGION-docker.pkg.dev/${PROJECT_ID}/REPOSITORY/${SERVICE_NAME}:v1 \ --allow-unauthenticated
部署成功後,指令列會顯示服務網址。
建立觸發條件
部署 Cloud Run 服務後,請設定觸發條件,透過稽核記錄監聽 Cloud Storage 的事件。
建立 Eventarc 觸發條件,以便監聽使用 Cloud 稽核記錄轉送的 Cloud Storage 事件:
gcloud eventarc triggers create troubleshoot-trigger \ --destination-run-service=troubleshoot-service \ --event-filters="type=google.cloud.audit.log.v1.written" \ --event-filters="serviceName=storage.googleapis.com" \ --event-filters="methodName=storage.objects.create" \ --service-account=${PROJECT_NUMBER}-compute@developer.gserviceaccount.com
這項操作會建立名為
troubleshoot-trigger
的觸發條件。如要確認
troubleshoot-trigger
是否已建立,請執行:gcloud eventarc triggers list
畫面會顯示如下的輸出內容:
NAME: troubleshoot-trigger TYPE: google.cloud.audit.log.v1.written DESTINATION: Cloud Run service: troubleshoot-service ACTIVE: By 20:03:37 LOCATION: us-central1
產生及查看事件
確認您已成功部署服務,且可以接收 Cloud Storage 傳出的事件。
建立檔案並上傳至
BUCKET1
儲存空間值區:echo "Hello World" > random.txt gsutil cp random.txt gs://${BUCKET1}/random.txt
監控記錄,確認服務是否收到事件。如要查看記錄項目,請完成下列步驟:
篩選記錄項目,並以 JSON 格式傳回輸出內容:
gcloud logging read "resource.labels.service_name=troubleshoot-service \ AND textPayload:random.txt" \ --format=json
尋找類似下列內容的記錄項目:
"textPayload": "Detected change in Cloud Storage bucket: ..."
請注意,系統一開始不會傳回任何記錄項目。這表示設定有問題,您必須進行調查。
調查問題
逐步調查服務未收到事件的原因。
初始化時間
雖然觸發條件會立即建立,但觸發條件傳播及篩選事件可能需要最多兩分鐘的時間。執行下列指令,確認觸發條件是否處於啟用狀態:
gcloud eventarc triggers list
輸出結果會顯示觸發事件的狀態。在以下範例中,troubleshoot-trigger
會在 14:16:56 前啟用:
NAME TYPE DESTINATION_RUN_SERVICE ACTIVE
troubleshoot-trigger google.cloud.audit.log.v1.written troubleshoot-service By 14:16:56
觸發條件啟用後,請再次將檔案上傳至儲存體儲存空間。事件會寫入 Cloud Run 服務記錄。如果服務未收到事件,可能與事件大小有關。
稽核記錄
在本教學課程中,我們會使用 Cloud 稽核記錄將 Cloud Storage 事件路由並傳送至 Cloud Run。確認已為 Cloud Storage 啟用稽核記錄。
前往 Google Cloud 控制台的「Audit Logs」頁面。
- 勾選「Google Cloud Storage」核取方塊。
- 確認已選取「管理員讀取」、「資料讀取」和「資料寫入」記錄類型。
啟用 Cloud 稽核記錄後,請再次將檔案上傳至儲存體值區,然後查看記錄。如果服務仍未收到事件,可能與觸發位置有關。
觸發地點
不同位置可能會有多個資源,因此您必須篩選與 Cloud Run 目標位於相同區域的來源事件。詳情請參閱「Eventarc 支援的位置」和「瞭解 Eventarc 位置」。
在本教學課程中,您已將 Cloud Run 服務部署至 us-central1
。由於您將 eventarc/location
設為 us-central1
,因此也會在相同位置建立觸發條件。
但您在 us-east1
和 us-west1
位置建立了兩個 Cloud Storage 值區。如要接收這些位置的事件,您必須在這些位置建立 Eventarc 觸發條件。
建立位於 us-east1
中的 Eventarc 觸發條件:
確認現有觸發條件的所在位置:
gcloud eventarc triggers describe troubleshoot-trigger
將位置和區域設為
us-east1
:gcloud config set eventarc/location us-east1 gcloud config set run/region us-east1
再次部署事件接收器,方法是建構容器映像檔並部署至 Cloud Run。
建立位於
us-east1
中的新觸發條件:gcloud eventarc triggers create troubleshoot-trigger-new \ --destination-run-service=troubleshoot-service \ --event-filters="type=google.cloud.audit.log.v1.written" \ --event-filters="serviceName=storage.googleapis.com" \ --event-filters="methodName=storage.objects.create" \ --service-account=${PROJECT_NUMBER}-compute@developer.gserviceaccount.com
檢查是否已建立觸發條件:
gcloud eventarc triggers list
觸發條件初始化最多可能需要兩分鐘,才能開始將事件路由。
如要確認觸發條件已正確部署,請產生並查看事件。
其他可能遇到的問題
使用 Eventarc 時,您可能會遇到其他問題。
事件大小
您傳送的事件不得超過事件大小限制。
先前可觸發事件的觸發條件已停止運作
確認來源是否會產生事件。檢查 Cloud 稽核記錄,確認受監控的服務是否會發出記錄。如果記錄了記錄檔,但未傳送事件,請與支援團隊聯絡。
確認是否有同名觸發條件的 Pub/Sub 主題。Eventarc 會使用 Pub/Sub 做為傳輸層,並使用現有的 Pub/Sub 主題,或自動建立主題並為您管理。
- 如要列出觸發條件,請參閱
gcloud eventarc triggers list
。 如要列出 Pub/Sub 主題,請執行:
gcloud pubsub topics list
確認 Pub/Sub 主題名稱包含已建立觸發條件的名稱。例如:
name: projects/PROJECT_ID/topics/eventarc-us-east1-troubleshoot-trigger-new-123
如果找不到 Pub/Sub 主題,請針對特定供應商、事件類型和 Cloud Run 目的地重新建立觸發事件。
- 如要列出觸發條件,請參閱
確認已為服務設定觸發條件。
前往 Google Cloud 控制台的「Services」頁面。
按一下服務名稱,開啟「Service details」(服務詳細資料)頁面。
按一下「觸發條件」分頁標籤。
系統應列出與服務相關聯的 Eventarc 觸發條件。
使用 Pub/Sub 指標類型驗證 Pub/Sub 主題和訂閱的健康狀況。
您可以使用
subscription/dead_letter_message_count
指標監控轉寄的無法送達郵件。這個指標會顯示 Pub/Sub 從訂閱項目轉寄的無法送達訊息數量。如果訊息未發布至主題,請檢查 Cloud 稽核記錄,確認受監控的服務是否會產生記錄。如果記錄了記錄檔,但未傳送事件,請與支援團隊聯絡。
您可以使用
subscription/push_request_count
指標,並依response_code
和subcription_id
將指標分組,監控推播訂閱項目。如果系統回報推送錯誤,請查看 Cloud Run 服務記錄。如果接收端點傳回的狀態碼不是 OK,表示 Cloud Run 程式碼無法正常運作,您必須與支援團隊聯絡。
詳情請參閱「建立以指標門檻為基礎的警告政策」。
清除所用資源
如果您是為了這個教學課程建立新專案,請刪除專案。如果您使用現有的專案,且想保留該專案而不採用本教學課程中新增的變更,請刪除為教學課程建立的資源。
刪除專案
如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。
如要刪除專案:
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
刪除教學課程資源
刪除您在本教學課程中部署的 Cloud Run 服務:
gcloud run services delete SERVICE_NAME
其中
SERVICE_NAME
是您選擇的服務名稱。您也可以從 Google Cloud 控制台刪除 Cloud Run 服務。
移除您在教學課程設定期間新增的所有 gcloud CLI 預設設定。
例如:
gcloud config unset run/region
或
gcloud config unset project
刪除本教學課程中建立的其他 Google Cloud 資源:
- 刪除 Eventarc 觸發條件:
請將gcloud eventarc triggers delete TRIGGER_NAME
TRIGGER_NAME
替換為觸發條件名稱。
- 刪除 Eventarc 觸發條件: