快速教程:使用 Cloud Audit Logs 接收事件 (Google Cloud CLI)

此快速入门介绍如何设置 Cloud Run for Anthos,以使用 Eventarc 接收来自 Cloud Storage 的事件。

在本快速入门中,您将执行以下操作:

  1. 创建 Cloud Storage 存储分区作为事件来源。
  2. 创建 Google Kubernetes Engine (GKE) 集群并启用 Cloud Run for Anthos。
  3. 设置一个服务帐号,以使用将事件转发到目标的事件转发器组件从 Pub/Sub 中拉取事件。
  4. 部署接收事件的 Cloud Run for Anthos 服务。
  5. 在 Eventarc 中初始化 Cloud Run for Anthos 目标。
  6. 创建一个 Eventarc GKE 触发器以将事件从 Cloud Storage 发送到 Cloud Run for Anthos 服务。
  7. 将文件上传到 Cloud Storage 存储桶以生成事件,并在 Cloud Run for Anthos 日志中查看该事件。

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. 确保您的 Google Cloud 项目已启用结算功能

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. 确保您的 Google Cloud 项目已启用结算功能

  6. 安装并初始化 Google Cloud CLI。如果系统提示您配置默认计算区域,请输入 n
  7. 更新 gcloud 组件:
    gcloud components update
  8. 启用 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
  9. 设置本快速入门中使用的配置变量:
    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
    其中 PROJECT_ID 是您的 Google Cloud 项目 ID。
  10. 可选:您可以使用 Google Cloud CLI 输入以下命令来配置设置:
    gcloud config list
    输出应类似如下所示:
    [eventarc]
    location = us-central1
    [run]
    cluster = events-cluster
    cluster_location = us-central1
    platform = gke
  11. 在 Cloud Storage 中启用 Cloud Audit Logs 管理员读取数据读取数据写入日志类型:
    1. 读取您项目的 IAM 政策,并将其存储在一个文件中:
       gcloud projects get-iam-policy PROJECT_ID > /tmp/policy.yaml
      
    2. /tmp/policy.yaml 中修改政策,仅添加或更改数据访问审核日志配置。

      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
      EMAIL_ADDRESS 替换为您的电子邮件地址。
    3. 写入新的 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 集群

使用 CloudRunHttpLoadBalancingHorizontalPodAutoscaling 插件为 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 中拉取事件并将其转发到目标。

  1. 创建一个名为 TRIGGER_GSA 的服务帐号,用于创建触发器:

    TRIGGER_GSA=eventarc-crfa-triggers
    gcloud iam service-accounts create $TRIGGER_GSA
  2. 向该服务帐号授予 pubsub.subscribermonitoring.metricWritereventarc.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 服务帐号授予权限:

  1. 为 Eventarc 启用 GKE 目标:

    gcloud eventarc gke-destinations init
  2. 在系统提示绑定所需角色时,输入 y

    以下角色绑定到服务帐号:

    • compute.viewer
    • container.developer
    • iam.serviceAccountAdmin

创建 Cloud Audit Logs 触发器

将文件上传到 Cloud Storage 时,Eventarc 触发器会将 Cloud Storage 中的事件发送到 hello Cloud Run for Anthos 服务。

  1. 创建 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 的触发器。

  2. 确认触发器已成功创建:

    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 服务日志中查看事件的消息。

  1. 将文本文件上传到 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 服务会记录事件的消息。

  2. 要查看事件消息,请转到 Cloud Run for Anthos 服务日志:

    1. 转到 Cloud Run for Anthos 页面。

      转到 Cloud Run for Anthos

    2. 点击 hello 服务。
    3. 选择日志标签页。
  3. 查找类似如下的日志条目:

    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 集群付费。

您可以删除映像删除存储桶以及删除 GKE 集群

要删除 Eventarc 触发器,请运行以下命令:

gcloud eventarc triggers delete my-gke-trigger

或者,您也可以删除 Google Cloud 项目,以避免产生费用。删除 Cloud 项目后,系统即会停止对该项目中使用的所有资源计费。

gcloud projects delete PROJECT_ID_OR_NUMBER

PROJECT_ID_OR_NUMBER 替换为项目 ID 或编号。

后续步骤