快速教程:使用 Cloud Audit Logs 接收事件 (Google Cloud CLI)
此快速入门介绍如何设置 Cloud Run for Anthos,以使用 Eventarc 接收来自 Cloud Storage 的事件。
在本快速入门中,您将执行以下操作:
- 创建 Cloud Storage 存储分区作为事件来源。
- 创建 Google Kubernetes Engine (GKE) 集群并启用 Cloud Run for Anthos。
- 设置一个服务帐号,以使用将事件转发到目标的事件转发器组件从 Pub/Sub 中拉取事件。
- 部署接收事件的 Cloud Run for Anthos 服务。
- 在 Eventarc 中初始化 Cloud Run for Anthos 目标。
- 创建一个 Eventarc GKE 触发器以将事件从 Cloud Storage 发送到 Cloud Run for Anthos 服务。
- 将文件上传到 Cloud Storage 存储桶以生成事件,并在 Cloud Run for Anthos 日志中查看该事件。
准备工作
- 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
- 安装并初始化 Google Cloud CLI。如果系统提示您配置默认计算区域,请输入
n
。 - 更新 gcloud 组件:
gcloud components update
- 启用 Google Cloud、Cloud Build、Resource Manager、Google Kubernetes Engine API、Container Registry 和 Eventarc API:
gcloud services enable cloudapis.googleapis.com gcloud services enable cloudbuild.googleapis.com gcloud services enable cloudresourcemanager.googleapis.com gcloud services enable container.googleapis.com gcloud services enable containerregistry.googleapis.com gcloud services enable eventarc.googleapis.com
- 设置本快速入门中使用的配置变量:
其中 PROJECT_ID 是您的 Google Cloud 项目 ID。gcloud config set project PROJECT_ID gcloud config set run/cluster events-cluster gcloud config set run/cluster_location us-central1 gcloud config set run/platform gke gcloud config set eventarc/location us-central1
- 可选:您可以使用 Google Cloud CLI 输入以下命令来配置设置:
输出应类似如下所示:gcloud config list
[eventarc] location = us-central1 [run] cluster = events-cluster cluster_location = us-central1 platform = gke
- 在 Cloud Storage 中启用 Cloud Audit Logs 管理员读取、数据读取和数据写入日志类型:
- 读取您项目的 IAM 政策,并将其存储在一个文件中:
gcloud projects get-iam-policy PROJECT_ID > /tmp/policy.yaml
- 在
/tmp/policy.yaml
中修改政策,仅添加或更改数据访问审核日志配置。 将 EMAIL_ADDRESS 替换为您的电子邮件地址。auditConfigs: - auditLogConfigs: - logType: ADMIN_READ - logType: DATA_WRITE - logType: DATA_READ service: storage.googleapis.com bindings: - members: - user:EMAIL_ADDRESS role: roles/owner etag: BwW_bHKTV5U= version: 1
- 写入新的 IAM 政策:
如果上述命令报告与其他更改发生冲突,请重复以上步骤(从读取项目的 IAM 政策开始)。gcloud projects set-iam-policy PROJECT_ID /tmp/policy.yaml
- 读取您项目的 IAM 政策,并将其存储在一个文件中:
创建 Cloud Storage 存储分区
本快速入门使用 Cloud Storage 作为事件来源。要创建存储桶,请执行以下操作:
gsutil mb -l us-central1 gs://events-quickstart-$(gcloud config get-value project)/
创建事件来源后,您可以在 Cloud Run 上部署事件接收器服务。
创建已启用 Cloud Run for Anthos 的 GKE 集群
使用 CloudRun
、HttpLoadBalancing
和 HorizontalPodAutoscaling
插件为 Cloud Run for Anthos 创建 GKE 集群。启用 Workload Identity 以从 GKE 中运行的应用访问 Google Cloud 服务。
PROJECT_ID=$(gcloud config get-value project) gcloud beta container clusters create events-cluster \ --addons=HttpLoadBalancing,HorizontalPodAutoscaling,CloudRun \ --machine-type=n1-standard-4 \ --enable-autoscaling --min-nodes=2 --max-nodes=10 \ --no-issue-client-certificate --num-nodes=2 \ --enable-stackdriver-kubernetes \ --scopes=cloud-platform,logging-write,monitoring-write,pubsub \ --zone us-central1 \ --release-channel=rapid \ --workload-pool=$PROJECT_ID.svc.id.goog
等待集群创建过程完成。您可以在创建过程中忽略警告。创建集群后,输出应类似于以下内容:
Creating cluster events-cluster...done.
Created [https://container.googleapis.com/v1beta1/projects/my-project
/zones/us-central1/clusters/events-cluster].
其中 my-project
是您的 Google Cloud 项目 ID。
这将在 my-project
中创建一个名为 events-cluster
的 GKE 集群。
设置 Google 服务帐号
设置用户提供的服务帐号并向其授予特定角色,以便事件转发器组件可以从 Pub/Sub 中拉取事件并将其转发到目标。
创建一个名为
TRIGGER_GSA
的服务帐号,用于创建触发器:TRIGGER_GSA=eventarc-crfa-triggers gcloud iam service-accounts create $TRIGGER_GSA
向该服务帐号授予
pubsub.subscriber
、monitoring.metricWriter
和eventarc.eventReceiver
角色: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 Run for Anthos 服务
使用预构建的映像 gcr.io/cloudrun/hello
,部署将接收和记录事件的 Cloud Run for Anthos 服务:
gcloud run deploy hello \ --platform=gke \ --cluster=events-cluster \ --cluster-location=us-central1 \ --namespace=default \ --image=gcr.io/cloudrun/hello
启用 GKE 目标
对于针对 Cloud Run for Anthos 服务的每个触发器,Eventarc 会创建一个事件转发器组件,用于从 Pub/Sub 拉取事件并将其转发到目标。如需在 GKE 集群中创建组件和管理资源,请向 Eventarc 服务帐号授予权限:
为 Eventarc 启用 GKE 目标:
gcloud eventarc gke-destinations init
在系统提示绑定所需角色时,输入
y
。以下角色绑定到服务帐号:
compute.viewer
container.developer
iam.serviceAccountAdmin
创建 Cloud Audit Logs 触发器
将文件上传到 Cloud Storage 时,Eventarc 触发器会将 Cloud Storage 中的事件发送到 hello
Cloud Run for Anthos 服务。
创建 Cloud Audit Logs 触发器:
gcloud eventarc triggers create my-gke-trigger \ --location=$TRIGGER_LOCATION \ --destination-gke-cluster="events-cluster" \ --destination-gke-location="us-central1" \ --destination-gke-namespace="default" \ --destination-gke-service="hello" \ --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
这将创建一个名为
my-gke-trigger
的触发器。确认触发器已成功创建:
gcloud eventarc triggers list
输出应类似如下所示:
NAME TYPE DESTINATION_RUN_SERVICE DESTINATION_RUN_PATH ACTIVE my-gke-trigger google.cloud.audit.log.v1.written Yes
生成并查看事件
将文本文件上传到 Cloud Storage 以生成事件并触发 Cloud Run for Anthos 服务。然后,您可以在 Cloud Run for Anthos 服务日志中查看事件的消息。
将文本文件上传到 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 Run for Anthos 服务日志:
- 转到 Cloud Run for Anthos 页面。
- 点击
hello
服务。 - 选择日志标签页。
查找类似如下的日志条目:
Hello from Cloud Run! The container started successfully and is listening for HTTP requests on $PORT Received event of type google.cloud.audit.log.v1.written.
清理
虽然当服务未在使用时 Cloud Run 不会产生费用,但您可能仍然需要为在 Container Registry 中存储容器映像、Eventarc 资源、Pub/Sub 消息和 GKE 集群付费。
要删除 Eventarc 触发器,请运行以下命令:
gcloud eventarc triggers delete my-gke-trigger
或者,您也可以删除 Google Cloud 项目,以避免产生费用。删除 Cloud 项目后,系统即会停止对该项目中使用的所有资源计费。
gcloud projects delete PROJECT_ID_OR_NUMBER
将 PROJECT_ID_OR_NUMBER 替换为项目 ID 或编号。