本教程介绍如何使用经过身份验证的 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 存储桶以用作事件来源:
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 事件 (y/N)?)提示时,请回复
n
(表示“否”)。
当您看到 Cloud Run 服务网址时,表示部署完成。
创建 Eventarc 触发器
Eventarc 触发器会将 Cloud Storage 存储桶中的事件发送到 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@
这将创建一个名为
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"
您可能需要等待一些时间才能看到日志。如果您没有立即看到日志,请稍等片刻,然后再检查一次。