在 GKE 中設定跨專案存取權

本頁面說明如何授予 Binary Authorization 存取權,讓您能存取與目前專案位於不同 Google Cloud 專案中的政策和容器映像檔。舉例來說,如果您從其他專案擁有的 Artifact Registry 或 Container Registry (已淘汰) 存放區,在 Google Kubernetes Engine (GKE) 叢集中部署映像檔,則必須授予專案中的二進位授權服務存取權,才能取得來源存放區中的映像檔中繼資料。

術語

本文使用下列詞彙:

  • 服務代理: Google Cloud代管的服務帳戶。 二進位授權會使用服務代理程式與 Google Cloud資源 (例如 GKE 叢集) 互動。
  • 政策專案: 包含二進位授權政策的 Google Cloud 專案。
  • 叢集專案: 含有 GKE 叢集的 Google Cloud 專案。
  • 構件專案: 包含 Artifact Registry 或 Container Registry (已淘汰) 存放區的專案。 Google Cloud

需要跨專案存取權的情境

在下列情況中,您必須授予跨專案權限:

  • 政策專案與叢集專案不同。
  • 叢集專案與構件專案不同。

事前準備

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Install the Google Cloud CLI.

  3. 如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI

  4. 如要初始化 gcloud CLI,請執行下列指令:

    gcloud init
  5. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Binary Authorization API:

    gcloud services enable binaryauthorization.googleapis.com
  8. Install the Google Cloud CLI.

  9. 如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI

  10. 如要初始化 gcloud CLI,請執行下列指令:

    gcloud init
  11. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  12. Verify that billing is enabled for your Google Cloud project.

  13. Enable the Binary Authorization API:

    gcloud services enable binaryauthorization.googleapis.com
  14. 叢集專案與政策專案不同

    在政策專案中,將「二進位授權政策評估者」(roles/binaryauthorization.policyEvaluator) 角色授予叢集專案中的二進位授權服務代理程式。

    gcloud projects add-iam-policy-binding POLICY_PROJECT_ID \
      --member="serviceAccount:service-$(gcloud projects describe CLUSTER_PROJECT_ID --format='value(projectNumber)')@gcp-sa-binaryauthorization.iam.gserviceaccount.com" \
      --role=roles/binaryauthorization.policyEvaluator

    取代下列項目:

    • POLICY_PROJECT_ID:包含政策的專案 ID。
    • CLUSTER_PROJECT_ID:叢集的專案 ID。

    叢集專案與構件專案不同

    在叢集專案中,將構件專案的 Artifact Registry Reader (roles/artifactregistry.reader) 角色授予 Binary Authorization 服務代理程式。

    gcloud projects add-iam-policy-binding ARTIFACT_PROJECT_ID \
        --member="serviceAccount:service-$(gcloud projects describe CLUSTER_PROJECT_ID --format='value(projectNumber)')@gcp-sa-binaryauthorization.iam.gserviceaccount.com" \
        --role=roles/artifactregistry.reader

    取代下列項目:

    • ARTIFACT_PROJECT_ID:包含 Artifact Registry 存放區的專案 ID。
    • CLUSTER_PROJECT_ID:執行 GKE 叢集的專案 ID。