将 Pub/Sub 触发器从 Events for Cloud Run for Anthos 迁移到 Eventarc

您可以迁移事件触发器,使得 Google Kubernetes Engine (GKE) 服务(包括 Knative serving 服务)可以使用 Eventarc 接收 Pub/Sub 事件。本指南假定您要迁移现有事件触发器,并且 GKE 服务正在 GKE 集群中运行。需要为每个集群运行迁移。

此迁移路径可避免事件丢失,同时最大限度地减少事件重复。

迁移涉及以下内容:

  1. 如需允许 Eventarc 管理 GKE 集群中的资源,请启用 GKE 目标。
  2. 如有必要,请为运行目标 GKE 服务的 GKE 集群启用 Workload Identity
  3. 确定现有事件触发器。
  4. 创建一个 Eventarc 触发器以指向与现有触发器相同的 GKE 服务。
  5. 确认 Eventarc 触发器按预期传送事件。
  6. 在迁移后删除原始事件触发器并进行清理。

准备工作

本指南假定您已具备:

设置命令行环境

如需设置 gcloudkubectl 命令行工具,请执行以下操作:

  1. 在 Google Cloud 控制台中,激活 Cloud Shell。

    激活 Cloud Shell

    Cloud Shell 会话随即会在 Google Cloud 控制台的底部启动,并显示命令行提示符。Cloud Shell 是一个已安装 Google Cloud CLI 且已为当前项目设置值的 Shell 环境。该会话可能需要几秒钟时间来完成初始化。

  2. 使用您的账号登录:

    gcloud auth login
  3. 设定 gcloud CLI 的默认设置:

    gcloud config set project PROJECT_ID
    gcloud config set run/cluster CLUSTER_NAME
    gcloud config set run/cluster_location CLUSTER_LOCATION
    gcloud config set run/platform gke
    gcloud config set eventarc/location LOCATION
    

    替换以下内容:

    • PROJECT_ID:您的项目 ID
    • CLUSTER_NAME:您的集群的名称
    • CLUSTER_LOCATION:支持 GKE 的任何可用区,例如:us-central1-a
    • LOCATION:Eventarc 触发器的位置,例如:us-central1
  4. 为项目启用以下 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
    
  5. 安装 kubectl 命令行工具:

    gcloud components install kubectl
  6. 更新已安装的 gcloud CLI 组件:

    gcloud components update

启用 GKE 目标

如需允许 Eventarc 管理 GKE 集群中的资源,请启用 GKE 目标,并将 Eventarc 服务账号绑定到所需的角色。

  1. 为 Eventarc 启用 GKE 目标:

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

    绑定以下角色:

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

在集群上启用 Workload Identity

如果您的集群上已启用 Workload Identity,则可以跳过此步骤。

由于 Workload Identity 具有增强的安全属性和可管理性,因此它是从 GKE 内运行的应用访问 Google Cloud 服务的推荐方法。它也是使用 Eventarc 转发 GKE 事件所需的。

如需在现有集群上启用 Workload Identity,请参阅使用 Workload Identity

确定现有事件触发器

在迁移任何现有事件触发器之前,您必须先检索触发器详细信息。

  1. 列出 GKE 集群的现有事件触发器:

    gcloud beta events triggers list --namespace EVENTS_NAMESPACE
    

    EVENTS_NAMESPACE 替换为事件代理的命名空间。

    输出内容类似如下:

       TRIGGER             EVENT TYPE                                     TARGET
    ✔  trigger-id          google.cloud.pubsub.topic.v1.messagePublished  cloud-run-service-name
    
  2. 您需要主题 ID 才能创建 Eventarc 触发器。检索现有事件触发器的主题 ID:

    gcloud beta events triggers describe TRIGGER_ID \
      --platform gke --namespace EVENTS_NAMESPACE \
      --format="flattened(serialized_source.spec.topic,serialized_trigger.spec.filter.attributes.type,serialized_trigger.spec.subscriber.ref.name,serialized_trigger.spec.subscriber.ref.namespace)"
    

    TRIGGER_ID 替换为现有事件触发器的 ID 或完全限定标识符。

    输出类似于以下内容:

    topic:     topic-id
    type:      google.cloud.pubsub.topic.v1.messagePublished
    name:      cloud-run-service-name
    namespace: events
    

创建 Eventarc 触发器以替换现有触发器

在创建 Eventarc 触发器之前,请先设置用户管理的服务账号,并为其授予特定的角色,以便 Eventarc 可以管理 GKE 目标的事件。

  1. 创建 Google Cloud 服务账号 (GSA):

    TRIGGER_GSA=SERVICE_ACCOUNT_NAME
    gcloud iam service-accounts create $TRIGGER_GSA

    SERVICE_ACCOUNT_NAME 替换为一个介于 6 到 30 个字符的名称。它可以包含小写字母数字字符和短划线。

  2. 向该服务账号授予 pubsub.subscribermonitoring.metricWriter 角色:

    PROJECT_ID=$(gcloud config get-value project)
    
    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"
  3. 根据现有事件触发器的配置创建新的 Eventarc 触发器。所有参数(包括目标服务、集群和主题 ID)都应与现有事件触发器匹配。

    gcloud eventarc triggers create EVENTARC_TRIGGER_NAME \
      --event-filters="type=google.cloud.pubsub.topic.v1.messagePublished" \
      --location LOCATION \
      --destination-gke-service=DESTINATION_SERVICE \
      --destination-gke-cluster=CLUSTER_NAME  \
      --destination-gke-location=CLUSTER_LOCATION  \
      --destination-gke-namespace=EVENTS_NAMESPACE  \
      --destination-gke-path=/ \
      --service-account=$TRIGGER_GSA@$PROJECT_ID.iam.gserviceaccount.com \
      --transport-topic=projects/PROJECT_ID/topics/TOPIC_ID
    

    EVENTARC_TRIGGER_NAME 替换为新 Eventarc 触发器的名称,并将 TOPIC_ID 替换为您之前检索的 Pub/Sub 主题 ID。

    此操作会为 Pub/Sub 主题 ID 创建一个 Eventarc 触发器。

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

    gcloud eventarc triggers list
    

    输出内容类似如下:

    NAME                  TYPE                                           DESTINATION_RUN_SERVICE  DESTINATION_RUN_PATH  ACTIVE
    eventarc-trigger-name google.cloud.pubsub.topic.v1.messagePublished                                                 Yes
    

测试迁移并验证输出

如需测试迁移,请通过发布到 Pub/Sub 主题来触发测试事件,然后验证每个现有事件触发器和 Eventarc 触发器是否分别至少有一个已传送的事件。

例如,触发测试事件后的输出会重复,并且类似于以下内容:

  Hello, World! ID: 2896291125947199
  Hello, World! ID: 2896291125947199

您还可以使用以下场景来作为指导,在该场景中,事件接收器可以显示事件数据:

  1. 将消息发布到 Pub/Sub 主题:

    gcloud pubsub topics publish TOPIC_ID \
      --message "Hello, World!"
    
  2. 验证事件并且它已成功传送。例如,查看接收 GKE 服务的日志:

    kubectl logs \
      --selector serving.knative.dev/service=DESTINATION_SERVICE \
      -c user-container \
      -n EVENTS_NAMESPACE \
      --tail=100
    

    两个触发器的输出类似于以下内容,仅 Context AttributesExtensions 不同。实际消息数据(subscription 数据除外)应相同,这样迁移到 Eventarc 后无需更改事件接收器。

    Eventarc 事件:

    ☁️  cloudevents.Event
    Validation: valid
    Context Attributes,
      specversion: 1.0
      type: google.cloud.pubsub.topic.v1.messagePublished
      source: //pubsub.googleapis.com/projects/project-id/topics/topic-name
      id: 2759155988927083
      time: 2021-07-22T17:47:19.964Z
      datacontenttype: application/json
    Data,
      {
        "subscription": "projects/project-id/subscriptions/eventarc-us-central1-eventarc-trigger-name-sub-567",
        "message": {
          "data": "V29ybGQ=",
          "messageId": "2759155988927083",
          "publishTime": "2021-07-22T17:47:19.964Z"
        }
      }
    

    Knative serving 事件:

    ☁️  cloudevents.Event
    Validation: valid
    Context Attributes,
      specversion: 1.0
      type: google.cloud.pubsub.topic.v1.messagePublished
      source: //pubsub.googleapis.com/projects/project-id/topics/topic-name
      id: 2759155988927083
      time: 2021-07-22T17:47:19.964Z
      dataschema: https://raw.githubusercontent.com/googleapis/google-cloudevents/master/proto/google/events/cloud/pubsub/v1/data.proto
      datacontenttype: application/json
    Extensions,
      knativearrivaltime: 2021-07-22T17:47:20.723302362Z
      knsourcetrigger: link0.09767362059083662
      traceparent: 00-f3cc6d754d361a0e49e83e5973fa3565-b4a20ef4ecffe96b-00
    Data,
      {
        "subscription": "cre-src_events_source-for-trigger-name_5ffa9638-8cab-4010-900a-2edb275d9eaf",
        "message": {
          "messageId": "2759155988927083",
          "data": "V29ybGQ=",
          "publishTime": "2021-07-22T17:47:19.964Z"
        }
      }
    

迁移后进行清理

测试并验证触发器到 Eventarc 的迁移后,您可以删除原始事件触发器。

  1. 删除原始事件触发器:

    gcloud beta events triggers delete TRIGGER_NAME \
      --platform gke \
      --namespace EVENTS_NAMESPACE \
      --quiet
    

您已将现有的 Knative serving 触发器迁移到 Eventarc。

后续步骤