本教學課程說明如何使用經過驗證的 Cloud Run 服務,透過 Eventarc 接收事件,並部署容器化應用程式。
為 Eventarc 觸發條件指定篩選器,即可設定事件的轉送方式,包括事件來源和事件目標。在本例中,Cloud Storage 值區的更新會觸發事件,並以 HTTP 要求的形式將要求傳送至 Cloud Run 服務。
建立 Artifact Registry 標準存放區
建立 Artifact Registry 標準存放區,用於儲存容器映像檔:
gcloud artifacts repositories create REPOSITORY \ --repository-format=docker \ --location=$REGION
將 REPOSITORY
替換成存放區的專屬名稱。
建立 Cloud Storage 值區
建立要用做事件來源的 Cloud Storage bucket:
gcloud storage buckets create gs://PROJECT_ID-bucket/ --location=us-central1
建立事件來源後,即可在 Cloud Run 上部署事件接收器服務。
將事件接收器部署至 Cloud Run
部署可接收及記錄事件的 Cloud Run 服務。
複製 GitHub 存放區:
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
您也可以 下載 zip 格式的範例,然後解壓縮該檔案。
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
您也可以 下載 zip 格式的範例,然後解壓縮該檔案。
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
您也可以 下載 zip 格式的範例,然後解壓縮該檔案。
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
您也可以 下載 zip 格式的範例,然後解壓縮該檔案。
C#
git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git
您也可以 下載 zip 格式的範例,然後解壓縮該檔案。
變更為包含 Cloud Run 範例程式碼的目錄:
Node.js
cd nodejs-docs-samples/eventarc/audit-storage/
Python
cd python-docs-samples/eventarc/audit-storage/
Go
cd golang-samples/eventarc/audit_storage/
Java
cd java-docs-samples/eventarc/audit-storage/
C#
cd dotnet-docs-samples/eventarc/audit-storage/
為 Cloud Run 服務建構容器:
export PROJECT_ID=$(gcloud config get-value project) export SERVICE_NAME=helloworld-events 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 public access to helloworld-events (y/N)?」(允許公開存取 helloworld-events (y/N)?) 提示中,輸入
n
表示「否」。
看到 Cloud Run 服務網址時,表示部署完成。
建立 Eventarc 觸發條件
Eventarc 觸發條件會將 Cloud Storage bucket 中的事件傳送至 helloworld-events
Cloud Run 服務。這項服務需要驗證,且事件應由具備服務帳戶的呼叫端觸發,該帳戶必須擁有使用資源所需的 IAM 角色和權限。
建立用來篩選 Cloud Storage 事件的觸發條件:
gcloud eventarc triggers create ${SERVICE_NAME} \ --destination-run-service=${SERVICE_NAME} \ --destination-run-region=${REGION} \ --location=${REGION} \ --event-filters="type=google.cloud.storage.object.v1.finalized" \ --event-filters="bucket=PROJECT_ID-bucket" \ --service-account=PROJECT_NUMBER-compute@developer.gserviceaccount.com
這項操作會建立名為
helloworld-events
的觸發條件。請注意,在 Google Cloud 專案中首次建立 Eventarc 觸發條件時,Eventarc 服務代理程式的佈建作業可能會延遲。這個問題通常可以透過重新建立觸發條件來解決。詳情請參閱「權限遭拒錯誤」。
確認觸發條件是否已成功建立。請注意,雖然觸發條件會立即建立,但最多可能需要兩分鐘才能正常運作。
gcloud eventarc triggers list --location=${REGION}
畫面會顯示如下的輸出內容:
NAME: helloworld-events TYPE: google.cloud.storage.object.v1.finalized DESTINATION: Cloud Run service: helloworld-events ACTIVE: Yes
產生及查看活動
將文字檔案上傳至 Cloud Storage 值區,產生會傳送至 Cloud Run 服務的事件。Cloud Run 服務會在服務記錄中記錄事件。
如要產生事件,請按照下列步驟操作:
將文字檔案上傳到 Cloud Storage:
echo "Hello World" > random.txt gcloud storage cp random.txt gs://PROJECT_ID-bucket/random.txt
上傳作業會產生事件,而 Cloud Run 服務會記錄事件的訊息。
如要查看記錄項目:
篩選記錄檔項目,並以 JSON 格式傳回輸出內容:
gcloud logging read "resource.labels.service_name=helloworld-events AND textPayload:random.txt" --format=json
尋找類似下列內容的記錄項目:
"textPayload": "Detected change in Cloud Storage bucket: objects/random.txt"
記錄需要一些時間才會出現。如果沒有立即看到記錄,請稍候片刻再查看一次。