在 AKS 和 EKS 上使用边车身份验证进行工作负载身份联合

工作负载身份联合可让 Apigee 工作负载获取私下来源的联合凭据,并将其交换为可访问 Apigee API 的 Google 令牌。本页面介绍了如何为 Apigee Hybrid 配置工作负载身份联合,以使用边车容器获取这些私有联合凭据。此方法是使用 Kubernetes Projected 服务账号令牌的替代方案,如在 AKS 和 EKS 上启用工作负载身份联合中所述。如果您需要使用外部或自定义身份提供方 (IdP) 来获取身份验证令牌,请使用 Sidecar 方法。

借助此方法,您可以在非 GKE 平台上使用边车容器和工作负载身份联合,以装载来自首选 IdP 的安全令牌,用于服务账号身份验证。

准备工作

在完成此过程之前,请确保您已完成以下操作:

  • 为您的 Google Cloud 项目配置了工作负载身份联合,包括创建身份池和提供方,以及向 Kubernetes 服务账号授予 IAM 权限。如需了解详情,请参阅在 AKS 和 EKS 上启用工作负载身份联合。
  • 构建并推送了一个自定义边车容器映像,该映像可从您的身份提供方 (IdP) 获取凭据,并将安全令牌写入共享卷(例如 /shared/token)。

配置 Apigee Hybrid

如需使用边车身份验证启用工作负载身份联合,请在 overrides.yaml 文件中设置边车配置。您必须指定边车容器以及在边车与 Apigee Hybrid 组件之间共享数据所需的任何卷。Apigee 会将边车容器添加为 Kubernetes 原生边车(一个带有 restartPolicy: Always 的 init 容器),因此您还必须指定 startupProbe。Kubernetes 会等待此探测成功,然后再启动 Apigee 组件容器。例如,您可以配置探测器以在共享令牌文件上运行 cat,从而验证令牌是否已写入。如果您使用的是 Vault,则必须添加所需的注释,以允许边车从您的 IdP 中提取令牌。

您只需在 overrides.yaml 文件中为所有组件配置 gcp.federatedWorkloadIdentity.sidecarSpec 部分。应用配置后,Helm 图表会将每个 APIGEE_COMPONENT 实例替换为相应的组件名称。组件名称为 apigee-cassandra、apigee-guardrails、apigee-logger、apigee-mart、apigee-mcp-server、apigee-metrics、apigee-mint-task-scheduler、apigee-runtime、apigee-synchronizer 和 apigee-watcher。

将以下配置添加到 overrides.yaml 文件中。选择与您的使用情形(Kubernetes Secret 或文件,或 Vault 代理注入)相符的示例:

Secret 或文件

gcp:
  federatedWorkloadIdentity:
    enabled: true
    type: "sidecar"
    sidecarSpec:
      volumes:
      - name: source-creds
        secret:
          secretName: apigee-hybrid-APIGEE_COMPONENT
      container:
        env: 
        - name: "apigee-component"
          value: APIGEE_COMPONENT
        image: CUSTOM_SIDECAR_IMAGE
        startupProbe:
          exec:
            command:
            - cat
            - /shared/token
        volumeMounts:
        - name: shared-mount
          mountPath: /shared
        - name: source-creds
          mountPath: /source-creds
          readOnly: true

保险库代理注入

gcp:
  federatedWorkloadIdentity:
    enabled: true
    type: "sidecar"
    sidecarSpec:
      annotations:
        vault.hashicorp.com/agent-inject: "true"
        vault.hashicorp.com/agent-pre-populate-only: "false"
        vault.hashicorp.com/log-level: "trace"
        vault.hashicorp.com/ca-cert: "VAULT_CA_CERT_PATH"
        vault.hashicorp.com/namespace: "VAULT_NAMESPACE"
        vault.hashicorp.com/agent-inject-secret-source-token: "VAULT_TOKEN" # For example: '/path/to/secret' or '/path/to/APIGEE_COMPONENT/secret' if the secret is per-component.
        vault.hashicorp.com/secret-volume-path: "VAULT_SECRET_VOLUME_PATH" # For example: '/source-creds'
        vault.hashicorp.com/service: "VAULT_SERVICE_URL:8200" # For example: '127.0.0.1:8200' or 'https://vault.example.com:8200'
      volumes:
      - name: source-creds
        secret:
          secretName: apigee-hybrid-APIGEE_COMPONENT
      container:
        env: 
        - name: "apigee-component"
          value: APIGEE_COMPONENT
        image: CUSTOM_SIDECAR_IMAGE
        startupProbe:
          exec:
            command:
            - cat
            - /shared/token
        volumeMounts:
        - name: shared-mount
          mountPath: /shared
        - name: source-creds
          mountPath: /source-creds
          readOnly: true

如果您的边车需要使用 Vault 注释注入每个组件的 Secret,并且您无法使用 APIGEE_COMPONENT 替换机制,则可以将注释添加到需要边车的每个组件。例如:

mart:
  annotations:
    vault.hashicorp.com/agent-inject: "true"
    vault.hashicorp.com/role: "VAULT_ROLE"
    vault.hashicorp.com/agent-inject-secret-token: "VAULT_SECRET_PATH"
    vault.hashicorp.com/agent-inject-template-token: |
      {{- with secret "VAULT_SECRET_PATH" -}}
      {{ .Data.data.token }}
      {{- end -}}

runtime:
  annotations:
    vault.hashicorp.com/agent-inject: "true"
    vault.hashicorp.com/role: "VAULT_ROLE"
    vault.hashicorp.com/agent-inject-secret-token: "VAULT_SECRET_PATH"
    vault.hashicorp.com/agent-inject-template-token: |
      {{- with secret "VAULT_SECRET_PATH" -}}
      {{ .Data.data.token }}
      {{- end -}}

... other components ...

其中:

  • gcp.federatedWorkloadIdentity.enabled:必须为 true。
  • gcp.federatedWorkloadIdentity.type:必须为 "sidecar"。这会告知 Apigee Hybrid 使用边车方法进行工作负载身份联合。
  • gcp.federatedWorkloadIdentity.sidecarSpec.container:自定义边车的 Kubernetes 容器规范。您必须指定可从 IdP 获取令牌的自定义 image。
  • gcp.federatedWorkloadIdentity.sidecarSpec.container.env:(可选)要在边车容器中设置的环境变量的列表。例如,您可以将 apigee-component 设置为 APIGEE_COMPONENT,以便边车可以识别它正在与哪个 Apigee 组件一起运行。
  • gcp.federatedWorkloadIdentity.sidecarSpec.container.startupProbe:边车容器的 Kubernetes 启动探测。该探测器会检查令牌文件是否存在于共享卷中。
  • gcp.federatedWorkloadIdentity.sidecarSpec.container.volumeMounts:边车容器的卷装载列表。您必须为包含 Apigee 组件令牌的共享卷添加名为 shared-mount 的卷装载。
  • gcp.federatedWorkloadIdentity.sidecarSpec.volumes:(可选)要添加到混合 Pod 以供边车容器使用的其他卷的列表。
  • gcp.federatedWorkloadIdentity.sidecarSpec.annotations:(仅限 Vault)要应用于混合 Pod 的注释的映射。
  • [component].annotations:(仅限 Vault,可选)要应用于各个混合组件的注释映射(如果配置需要)。

更新 overrides.yaml 文件后,将更改应用于集群。 运行以下命令以更新每个混合组件:

  1. 确保您位于 apigee-charts/ 目录中。
  2. 升级 Apigee Operator/Controller:
    helm upgrade operator apigee-operator/ \
      --install \
      --namespace APIGEE_NAMESPACE \
      -f OVERRIDES_FILE
    
  3. 升级 Apigee 数据存储区:
    helm upgrade datastore apigee-datastore/ \
      --install \
      --namespace APIGEE_NAMESPACE \
      -f OVERRIDES_FILE
    
  4. 升级 Apigee 遥测:
    helm upgrade telemetry apigee-telemetry/ \
      --install \
      --namespace APIGEE_NAMESPACE \
      -f OVERRIDES_FILE
    
  5. 升级 Apigee Redis:
    helm upgrade redis apigee-redis/ \
      --install \
      --namespace APIGEE_NAMESPACE \
      -f OVERRIDES_FILE
    
  6. 升级 Apigee 入站流量管理器:
    helm upgrade ingress-manager apigee-ingress-manager/ \
      --install \
      --namespace APIGEE_NAMESPACE \
      -f OVERRIDES_FILE
    
  7. 升级 Apigee 组织:
    helm upgrade ORG_NAME apigee-org/ \
      --install \
      --namespace APIGEE_NAMESPACE \
      -f OVERRIDES_FILE
    
  8. 升级环境。

    一次只能安装一个环境。使用 --set env=ENV_NAME 指定环境。

    helm upgrade ENV_RELEASE_NAME apigee-env/ \
      --install \
      --namespace APIGEE_NAMESPACE \
      --set env=ENV_NAME \
      -f OVERRIDES_FILE
    
  9. 升级环境组 (virtualhosts)。

    一次只能升级一个环境组 (virtualhost)。使用 --set envgroup=ENV_GROUP_NAME 指定环境组。对 override.yaml 文件中提到的每个环境组重复运行以下命令:

    helm upgrade ENV_GROUP_RELEASE_NAME apigee-virtualhost/ \
      --install \
      --namespace APIGEE_NAMESPACE \
      --set envgroup=ENV_GROUP_NAME \
      -f OVERRIDES_FILE
    

有关详情,请参阅: