跨 Google Cloud 项目路由事件

本教程介绍如何使用 Eventarc 从一个 Google Cloud 项目中的来源读取事件,并将其路由到另一个 Google Cloud 项目中的目标目标位置。可以通过将 Pub/Sub 用作跨项目传输层来实现。

跨项目路由 Pub/Sub 事件

由于 Pub/Sub 是全球分布式服务,因此您可以在一个项目中创建主题,从另一个项目发布到该主题,然后触发 Eventarc 以将消息路由到 Cloud Run 服务:

跨项目路由事件:Cloud Pub/Sub 和 Eventarc

  1. 将 Google Cloud 项目 ID 设置为第二个项目:

    gcloud config set project PROJECT_TWO_ID

    PROJECT_TWO_ID 替换为您的第二个Google Cloud 项目的 ID。

  2. 在第二个项目中,执行以下操作:

    1. 启用 Cloud Run 和 Eventarc API:

      gcloud services enable run.googleapis.com eventarc.googleapis.com
    2. 设置默认位置:

      REGION=REGION

      REGION 替换为您选择的受支持的 Eventarc 位置。例如 us-central1

    3. 创建 Pub/Sub 主题:

      TOPIC=my-topic
      gcloud pubsub topics create $TOPIC
    4. 使用预构建的映像 us-docker.pkg.dev/cloudrun/container/hello 部署未经身份验证的 Cloud Run 服务:

      gcloud run deploy hello \
          --image=us-docker.pkg.dev/cloudrun/container/hello \
          --allow-unauthenticated \
          --region=$REGION

      当您看到服务网址时,表示部署完成。

    5. 使用 Eventarc 触发器将主题连接到该服务:

      gcloud eventarc triggers create cross-project-trigger \
          --destination-run-service=hello \
          --destination-run-region=${REGION} \
          --location=${REGION} \
          --event-filters="type=google.cloud.pubsub.topic.v1.messagePublished" \
          --transport-topic=projects/PROJECT_TWO_ID/topics/$TOPIC

      这将创建一个名为 cross-project-trigger 的触发器。

  3. 将 Google Cloud 项目 ID 设置为您的第一个项目:

    gcloud config set project PROJECT_ONE_ID

    PROJECT_ONE_ID 替换为您的第一个Google Cloud 项目的 ID。

  4. 在第一个项目中,向第二个项目中的主题发布消息:

    gcloud pubsub topics publish projects/PROJECT_TWO_ID/topics/$TOPIC --message="hello"
  5. 将 Google Cloud 项目 ID 设置为第二个项目:

    gcloud config set project PROJECT_TWO_ID
  6. 在第二个项目中,确认生成的事件已记录:

    gcloud logging read "resource.labels.service_name=hello AND jsonPayload.message:hello" --format=json

    系统会返回如下所示的日志条目:

    "message": "Received event of type google.cloud.pubsub.topic.v1.messagePublished. Event data: hello"

跨项目路由 Cloud Storage 事件

使用适用于 Cloud Storage 的 Pub/Sub 通知将事件从一个项目发布到另一个项目,然后通过 Eventarc 触发器将事件路由到 Cloud Run 服务:

跨项目路由事件:Cloud Storage 和 Eventarc

  1. 将 Google Cloud 项目 ID 设置为您的第一个项目:

    gcloud config set project PROJECT_ONE_ID
  2. 创建 Cloud Storage 存储分区,请运行以下命令:

    PROJECT1=$(gcloud config get-value project)
    BUCKET=$PROJECT1-cross-project
    gcloud storage buckets create gs://$BUCKET --location=${REGION}
  3. 针对第二个项目中的主题为该存储桶创建 Pub/Sub 通知:

    gcloud storage buckets notifications create gs://$BUCKET --topic=projects/PROJECT_TWO_ID/topics/$TOPIC --payload-format=json
  4. 将文件上传到该存储桶:

    echo "Hello World" > random.txt
    gcloud storage cp random.txt gs://$BUCKET/random.txt
  5. 将 Google Cloud 项目 ID 设置为第二个项目:

    gcloud config set project PROJECT_TWO_ID
  6. 在第二个项目中,确认生成的事件已记录:

    gcloud logging read "resource.labels.service_name=hello AND jsonPayload.message:random.txt" --format=json

    系统会返回如下所示的日志条目:

    Received event of type google.cloud.pubsub.topic.v1.messagePublished. Event data: {
      "kind": "storage#object",
      "id": "project1-cross-project/random.txt/1635327604259719",
      "selfLink": "https://www.googleapis.com/storage/v1/b/project1-cross-project/o/random.txt",
      "name": "random.txt",
      "bucket": "project1-cross-project",
      "generation": "1635327604259719",
    [...]
    }

跨项目路由 Cloud Audit Logs 事件

当创建与触发器的过滤条件匹配的审核日志条目时,可能会触发对您的服务的请求。如需了解详情,请参阅确定 Cloud Audit Logs 的事件过滤条件。在这种情况下,在第一个项目中创建 Compute Engine 虚拟机实例时,与触发器的过滤条件匹配的审核日志条目可让您捕获事件并将其路由到第二个项目中的 Cloud Run 服务:

跨项目路由事件:Cloud Audit Logs 和 Eventarc

  1. 将 Google Cloud 项目 ID 设置为您的第一个项目:

    gcloud config set project PROJECT_ONE_ID
  2. 在第一个项目中,为 Compute Engine 启用管理员读取数据读取数据写入日志类型:

    请注意,在项目级层,您需要 roles/owner Identity and Access Management (IAM) 角色才能为 Google Cloud 资源配置数据访问审核日志。

    1. 读取您项目的 IAM 政策,并将其存储在一个文件中:

      gcloud projects get-iam-policy PROJECT_ONE_ID > /tmp/policy.yaml
      
    2. 修改 /tmp/policy.yaml,仅添加或更改数据访问审核日志配置。

      auditConfigs:
      - auditLogConfigs:
        - logType: ADMIN_READ
        - logType: DATA_READ
        - logType: DATA_WRITE
        service: compute.googleapis.com
      
    3. 写入新的 IAM 政策:

      gcloud projects set-iam-policy PROJECT_ONE_ID /tmp/policy.yaml
      

      如果上述命令报告与其他更改发生冲突,请重复以上步骤(从读取项目的 IAM 政策开始)。

  3. 在第一个项目中,创建一个 Cloud Logging 接收器以将 Cloud Audit Logs 路由到第二个项目中的主题:

    gcloud logging sinks create cross-project-sink \
        pubsub.googleapis.com/projects/PROJECT_TWO_ID/topics/my-topic \
        --log-filter='protoPayload.methodName="beta.compute.instances.insert"'

    系统应会返回如下所示的提醒:

    Please remember to grant `serviceAccount:p1011272509317-375795@gcp-sa-logging.iam.gserviceaccount.com` the Pub/Sub Publisher role on the topic.
  4. 将 Google Cloud 项目 ID 设置为第二个项目:

    gcloud config set project PROJECT_TWO_ID
  5. 在第二个项目中,将角色授予服务账号:

    gcloud pubsub topics add-iam-policy-binding my-topic \
        --member=SERVICE_ACCOUNT \
        --role=roles/pubsub.publisher

    SERVICE_ACCOUNT 替换为上一步中返回的服务账号电子邮件地址。

  6. 将 Google Cloud 项目 ID 设置为您的第一个项目:

    gcloud config set project PROJECT_ONE_ID
  7. 在第一个项目中,创建 Compute Engine 虚拟机实例

    如果您使用 Google Cloud 控制台创建虚拟机实例,则可以接受默认值,供本教程使用。

  8. 将 Google Cloud 项目 ID 设置为第二个项目:

    gcloud config set project PROJECT_TWO_ID
  9. 在第二个项目中,确认生成的事件已记录:

    gcloud logging read "resource.labels.service_name=hello AND jsonPayload.message:beta.compute.instances.insert" --format=json

    系统会返回如下所示的日志条目:

    Received event of type google.cloud.pubsub.topic.v1.messagePublished. Eventdata: {
      "logName": "projects/workflows-atamel/logs/cloudaudit.googleapis.com%2Factivity",
      "operation": {
        "id": "operation-1635330842489-5cf5321f4f454-ecc363cd-3883c08d",
        "last": true,
        "producer": "compute.googleapis.com"
      },
      "protoPayload": {
        "@type": "type.googleapis.com/google.cloud.audit.AuditLog",
        "methodName": "beta.compute.instances.insert",
      }
    [...]
    }