本页面介绍如何设置 Events for Cloud Run for Anthos 以及如何从 Cloud Storage 接收事件。
在本快速入门中,您将执行以下操作:
- 创建 GKE 集群并启用 Cloud Run for Anthos on Google Cloud。
- 初始化 Events for Cloud Run for Anthos。
- 创建事件代理以将事件从来源路由到接收器。
- 部署接收事件的 Cloud Run for Anthos 服务。
- 设置 Cloud Storage 以生成事件。
- 生成并查看事件。
准备工作
-
登录您的 Google 帐号。
如果您还没有 Google 帐号,请注册新帐号。
-
在 Google Cloud Console 的项目选择器页面上,选择或创建一个 Google Cloud 项目。
-
确保您的 Cloud 项目已启用结算功能。 了解如何确认您的项目是否已启用结算功能。
- 安装并初始化 Cloud SDK。
- 确保您使用的是最新版本的
gcloud
命令行工具。 - 将
gcloud
命令行工具设置为使用您的 GCP 项目:gcloud config set project PROJECT-ID
- 安装
gcloud
命令行工具 Beta 版组件:gcloud components install beta
- 启用本快速入门中使用的服务:
gcloud services enable cloudapis.googleapis.com gcloud services enable container.googleapis.com gcloud services enable containerregistry.googleapis.com gcloud services enable cloudbuild.googleapis.com
- 设置
gcloud
命令行工具配置变量:gcloud config set run/cluster events-cluster gcloud config set run/cluster_location us-central1 gcloud config set run/platform gke
- 可选:您可以使用
gcloud
命令行工具,通过输入以下命令来检查配置设置:gcloud config list
输出内容类似如下:
[run] cluster = events-cluster cluster_location = us-central1 platform = gke
创建已启用 Cloud Run for Anthos 的 GKE 集群
为启用了
CloudRun
、HttpLoadBalancing
、HorizontalPodAutoscaling
插件的 Cloud Run for Anthos 创建 Google Kubernetes Engine 集群:gcloud beta container clusters create events-cluster \ --addons=HttpLoadBalancing,HorizontalPodAutoscaling,CloudRun \ --machine-type=n1-standard-4 \ --enable-autoscaling --min-nodes=3 --max-nodes=10 \ --no-issue-client-certificate --num-nodes=3 --image-type=cos \ --enable-stackdriver-kubernetes \ --scopes=cloud-platform,logging-write,monitoring-write,pubsub \ --zone us-central1 \ --release-channel=rapid
等待集群创建过程完成,这可能需要几分钟。在创建过程中,您可能会看到一些可以放心忽略的警告。创建完集群后,输出将类似于以下内容:
Creating cluster events-cluster...done. Created [https://container.googleapis.com/v1beta1/ \ projects/my-project/zones/us-central1/clusters/events-cluster].
其中,
my-project
是您的项目 ID。您刚刚在项目my-project
中创建了名为events-cluster
的新 Google Kubernetes Engine 集群。
初始化 Events for Cloud Run for Anthos
要初始化 Events for Cloud Run for Anthos,请执行以下操作:
使用
gcloud
命令行工具为 Cloud Run for Anthos 初始化 Events for Cloud Run for Anthos:gcloud beta events init --platform gke
出现提示时,选择
events-cluster
作为 GKE 集群。在要求创建服务帐号、将服务帐号绑定到特定角色、生成新密钥和启用必要服务的所有提示中,输入
Y
。
此操作会在两个名为
cloud-run-events
和knative-eventing
的 Kubernetes 命名空间中创建 Pod。可选:检查是否成功创建 Pod:
kubectl get pods -n cloud-run-events
输出内容类似如下:
NAME READY STATUS RESTARTS AGE controller-9cc679b67-2952n 1/1 Running 0 22s webhook-8576c4cfcb-dhz82 1/1 Running 0 16m
如需检查 Pod
knative-eventing
命名空间,请输入以下命令:kubectl get pods -n knative-eventing
输出内容类似如下:
NAME READY STATUS RESTARTS AGE eventing-controller-77f46f6cf8-kj9ck 1/1 Running 0 17m eventing-webhook-5bc787965f-hcmwg 1/1 Running 0 17m
创建事件代理
在 Events for Cloud Run for Anthos 中,代理会将事件从来源路由到目标服务。在本快速入门中,您将创建一个代理,它将事件从 Cloud Storage 路由到 Cloud Run for Anthos 服务。
如需创建代理,请执行以下操作:
创建
events
命名空间:kubectl create namespace events
使用默认密钥初始化命名空间:
gcloud beta events namespaces init events \ --copy-default-secret
创建事件代理:
gcloud beta events brokers create default \ --namespace events
可选:几分钟后,您可以通过运行以下命令来检查代理的状态:
kubectl get brokers \ -n events
部署 Cloud Run for Anthos 服务以接收事件
部署 Cloud Run for Anthos 服务,用于接收并记录代理的事件。
如需部署 Cloud Run for Anthos 服务,请执行以下操作:
克隆代码库:
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git cd python-docs-samples/eventarc/audit-storage
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git cd java-docs-samples/eventarc/audit-storage
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples.git cd golang-samples/eventarc/audit_storage
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git cd nodejs-docs-samples/eventarc/audit-storage
C#
git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git cd dotnet-docs-samples/eventarc/audit-storage
构建容器并将其上传到 Cloud Build:
gcloud builds submit \ --tag gcr.io/$(gcloud config get-value project)/events-quickstart-container
将容器映像部署到 Cloud Run for Anthos:
gcloud run deploy events-quickstart-receiver \ --namespace=events \ --image gcr.io/$(gcloud config get-value project)/events-quickstart-container
当您看到服务网址时,表示已成功部署容器映像。
设置 Cloud Storage
当存储分区发生更改时,Cloud Storage 会生成事件并将其发送到事件代理。如需设置 Cloud Storage,请执行以下操作:
使用
gsutil
命令行工具创建 Cloud Storage 存储分区:gsutil mb \ -p $(gcloud config get-value project) \ -l us-central1 \ gs://"events-quickstart-$(gcloud config get-value project)"
在此示例中,Cloud Storage 存储分区创建在 us-central1 区域。
检索 Cloud Storage 服务帐号:
curl -X GET -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://storage.googleapis.com/storage/v1/projects/$(gcloud config get-value project)/serviceAccount"
向 Cloud Storage 服务帐号授予 Pub/Sub 的发布权限:
gcloud projects add-iam-policy-binding $(gcloud config get-value project) \ --member=serviceAccount:CLOUD-STORAGE-SERVICE-ACCOUNT \ --role roles/pubsub.publisher
创建 Cloud Storage 触发器
创建触发器时,您必须指定从 Cloud Storage 接收事件的 Cloud Run for Anthos 服务。
创建 Cloud Storage 触发器:
gcloud beta events triggers create trigger-storage \ --namespace events \ --target-service events-quickstart-receiver \ --type=google.cloud.storage.object.v1.finalized \ --parameters bucket=events-quickstart-$(gcloud config get-value project)
这会为您的
events-quickstart-bucket
Cloud Storage 存储分区创建一个触发器。可选:您可以通过运行以下命令来检查触发器的状态:
gcloud beta events triggers list \ --namespace=events
请注意,触发器创建完毕并准备就绪时,可能需要长达 10 分钟的时间才能传播和过滤 Cloud Storage 事件。
生成和查看事件
要生成事件,请创建一个文件名为
random.txt
且包含文本“Hello World”的文本文件,并使用gsutil
命令行工具将其上传至 Cloud Storage。echo "Hello World" > random.txt gsutil cp random.txt gs://events-quickstart-$(gcloud config get-value project)/random.txt
上传操作会生成事件,而 Cloud Run for Anthos 服务会记录事件的消息。
要在 Cloud Logging 中查看消息,请执行以下操作:
恭喜!您已成功部署了 Events for Cloud Run for Anthos、触发了事件并记录了其内容。
清理
要停止对测试 Cloud 项目中使用的所有资源计费,请删除该项目:
- 在 Cloud Console 中,转到管理资源页面。
- 在项目列表中,选择要删除的项目,然后点击删除。
- 在对话框中输入项目 ID,然后点击关闭以删除项目。