设置 Binary Authorization 政策强制执行

Binary Authorization for GKE on VMware 是一项 Google Cloud 功能,可将 Binary Authorization 的托管部署时强制执行扩展到本地用户集群。在 GKE on VMware 上,Binary Authorization 的主要使用场景是保护用户集群上的工作负载。请按照本指南中的步骤,将您在 Google Cloud 项目中配置的 Binary Authorization 政策的强制执行规则应用于您的用户集群。如需详细了解 Binary Authorization 政策和规则,请参阅 Binary Authorization 概览

前提条件

您必须先满足以下前提条件,然后才能为用户集群启用 Binary Authorization 政策强制执行功能:

  • 向舰队注册集群:对于使用 gkectl 创建的集群,该集群会注册到您在集群配置文件的 gkeConnect.projectID 字段中指定的 Google Cloud 项目。此项目称为舰队宿主项目。如需详细了解舰队(包括用例、最佳实践和示例),请参阅舰队管理文档。

  • 在 Google Cloud 项目中启用 Binary Authorization API:在舰队宿主项目中启用 Binary Authorization 服务

  • 将 Binary Authorization Policy Evaluator 角色添加到舰队宿主项目:如需在舰队宿主项目上将 Binary Authorization Policy Evaluator (roles/binaryauthorization.policyEvaluator) 角色授予 Kubernetes 服务账号,请运行以下命令:

    gcloud projects add-iam-policy-binding PROJECT_ID \
        --member="serviceAccount:PROJECT_ID.svc.id.goog[binauthz-system/binauthz-agent]" \
        --role="roles/binaryauthorization.policyEvaluator"
    

    如果您的集群在代理服务器后面运行,请确保代理服务器允许与 Binary Authorization API (binaryauthorization.googleapis.com) 建立连接。此 API 为部署到集群的映像提供基于政策的部署验证和控制。如需了解详情,请参阅代理和防火墙规则代理

满足前提条件后,您可以在创建新集群或更新现有集群时启用(或停用)Binary Authorization 政策。

在集群创建期间启用 Binary Authorization 政策

您可以使用 gkectl 或 gcloud CLI 启用 Binary Authorization 政策强制执行。

gkectl

如需在使用 gkectl 创建集群时启用 Binary Authorization,请执行以下操作:

  1. 在创建集群之前,请将 binaryAuthorization.evaluationMode 添加到用户集群配置文件中,如以下示例所示:

    ...
    binaryAuthorization:
      evaluationMode: "project_singleton_policy_enforce"
    ...
    

    允许的 evaluationMode 值包括:

    • project_singleton_policy_enforce:对 Google Cloud 项目强制执行 Binary Authorization 政策(也称为项目单例政策)中指定的规则,以管理集群上的容器映像部署。

    • disabled:为集群停用 Binary Authorization。此设置为默认值。 如果您省略 binaryAuthorization,则该功能会停用。

  2. 请在集群配置文件中进行所需的任何其他更改,然后运行 gkectl create cluster 命令。

如需详细了解如何创建集群,请参阅 GKE on VMware 安装概览

为现有集群启用或停用 Binary Authorization 政策

如果您已有 1.28 版或更高版本的集群,可以随时使用 gkectl 或 gcloud CLI 启用或停用 Binary Authorization。

gkectl

  • 启用:

    1. 修改集群配置文件,以添加 binaryAuthorization 字段:

      ...
      binaryAuthorization:
        evaluationMode: "project_singleton_policy_enforce"
      
    2. 更新集群:

      gkectl update cluster \
        --kubeconfig ADMIN_CLUSTER_KUBECONFIG \
        --config USER_CLUSTER_CONFIG_FILE \
        --force

      替换以下内容:

      • ADMIN_CLUSTER_KUBECONFIG:管理员集群 kubeconfig 文件的路径
      • USER_CLUSTER_CONFIG_FILE:用户集群配置文件的路径。
    3. 等待 binauthz-system 命名空间中名为 binauthz-module-deployment 的 Deployment 准备就绪。

      该 Deployment 准备就绪后,Binary Authorization 会强制执行 Binary Authorization 政策(也称为项目单例政策)中指定的规则。此政策与您的 Google Cloud 项目相关联,用于指定用于管理容器映像部署的规则。如需详细了解如何使用 gkectl 更新集群,请参阅更新集群。如需详细了解 Binary Authorization 政策和规则,请参阅 Binary Authorization 概览

  • 停用:

    1. 修改集群配置文件,移除 binaryAuthorization 部分或将 evaluationMode 设置为 disabled

      ...
      binaryAuthorization:
        evaluationMode: "disabled"
      
    2. 更新集群:

      gkectl update cluster \
        --kubeconfig ADMIN_CLUSTER_KUBECONFIG \
        --config USER_CLUSTER_CONFIG_FILE \
        --force

    进行此项更改后,请等待几分钟,直到 binauthz-system 命名空间中名为 binauthz-module-deployment 的 Deployment 被移除。

问题排查

如果您未满足所有前提条件,则可能会看到如下所示的消息,表明 Binary Authorization 配置存在问题:

failed to validate Binary Authorization policy

(1) Ensure the Binary Authorization API is enabled for your Google Cloud project:
    gcloud services enable binaryauthorization.googleapis.com --project=PROJECT_ID
(2) Ensure an IAM policy binding is in place granting binaryauthorization.policyEvaluator role to the binauthz-system/binauthz-agent Kubernetes service account:
    gcloud projects add-iam-policy-binding PROJECT_ID \
        --member="serviceAccount:PROJECT_ID.svc.id.goog[binauthz-system/binauthz-agent]" \
        --role=roles/binaryauthorization.policyEvaluator