使用 IAM 控管存取權

本頁說明如何使用 Identity and Access Management (IAM) 控管 Artifact Registry 的存取權。

導入 CI/CD 管道時,Artifact Registry 的預設權限可減少設定工作。您也可以將 Artifact Registry 與第三方 CI/CD 工具整合,並設定存取存放區所需的權限和驗證機制。

如果您使用 Artifact Analysis 處理容器中繼資料 (例如映像檔中發現的安全漏洞),請參閱 Artifact Analysis 說明文件,瞭解如何授予檢視或管理中繼資料的存取權。

事前準備

  1. 啟用 Artifact Registry,包括啟用 API 和安裝 Google Cloud CLI。
  2. 如要套用存放區專屬權限,請為套件建立 Artifact Registry 存放區

總覽

IAM 權限角色會決定您在 Artifact Registry 存放區中建立、查看、編輯或刪除資料的能力。

角色是一組權限。您無法直接授予主體權限,而是授予角色。授予角色之後,主體就會取得該角色具備的所有權限。您可以將多個角色授予相同的主體。

Google Cloud 預設權限

根據預設,下列權限適用於與 Artifact Registry 位於同一專案的 Google Cloud CI/CD 服務:

  • Cloud Build 權限包括上傳及下載構件的權限。

如果所有服務都在同一個 Google Cloud 專案中,且預設權限符合需求,則不需要設定權限。

如果符合下列情況,您必須為這些服務設定 Artifact Registry 權限:

  • 您使用的 GKE 版本未內建從 Artifact Registry 提取映像檔的支援功能。如需設定操作說明,請參閱「GKE」一節。
  • 您希望預設服務帳戶具備存放區的讀取和寫入權限。詳情請參閱下列資訊:
  • 您為執行階段環境使用使用者提供的服務帳戶,而非預設服務帳戶。在含有 Artifact Registry 的專案中,授予服務帳戶必要角色。

第三方整合

如果是第三方用戶端,您必須同時設定權限和驗證。

傳統上,在外部執行的應用程式會使用服務帳戶金鑰存取 Google Cloud 資源。 Google Cloud 不過,服務帳戶金鑰是功能強大的憑證,如果管理不當就會帶來安全風險。

有了 Workload Identity Federation,您可以使用身分與存取權管理,將 IAM 角色授予外部身分,包括模擬服務帳戶的能力。這種做法可免除與服務帳戶金鑰相關的維護和安全負擔。

使用 Workload Identity 聯盟

  1. 建立 Workload Identity Federation 集區
  2. 建立 Workload Identity Federation 提供者
  3. 授予工作負載身分集區適當的 Artifact Registry 角色,允許存取存放區。詳情請參閱「允許外部工作負載存取 Google Cloud 資源」。
  4. 如需長時間存取 Artifact Registry,請在憑證設定中,將 OIDC 權杖到期時間設為較長的時間。
  5. 設定第三方用戶端,以便透過 Artifact Registry 進行驗證。

使用服務帳戶

  1. 建立服務帳戶來代表應用程式執行動作,或是選擇用於 CI/CD 自動化的現有服務帳戶。
  2. 授予服務帳戶適當的 Artifact Registry 角色,提供存放區存取權。
  3. 設定第三方用戶端,以便透過 Artifact Registry 進行驗證。

GitLab 上的 Google Cloud

GitLab on Google Cloud 整合功能會使用工作負載身分聯盟,授權及驗證 Google Cloud 上的 GitLab 工作負載,不需要服務帳戶或服務帳戶金鑰。如要進一步瞭解如何透過這項合作夥伴關係使用 Workload Identity Federation,請參閱「Google Cloud Workload Identity Federation 和 IAM 政策」。

如要設定 Workload Identity Federation 和 GitLab on Google Cloud的必要 IAM 角色,請參閱 GitLab 教學課程「Google Cloud Workload Identity Federation and IAM policies」。

如要連結 Artifact Registry 存放區,請參閱 GitLab 教學課程「Google Artifact Registry」。

角色和權限

凡是需要主體發送要求的 Artifact Registry API 方法,都具備使用該資源的必要權限。只要設定政策,授予主體資源的預先定義角色,即可將權限授予主體。

您可以在 Google Cloud 專案或 Artifact Registry 存放區中授予角色。

預先定義的 Artifact Registry 角色

IAM 提供預先定義的角色,這些角色會授予特定 Google Cloud 資源的存取權。

pkg.dev 網域中,為存放區使用下列預先定義的角色:
角色 說明
Artifact Registry 讀取者
(roles/artifactregistry.reader)
查看及取得構件、查看存放區中繼資料。
Artifact Registry Writer
(roles/artifactregistry.writer)
讀取及寫入構件。
Artifact Registry 存放區管理員
(roles/artifactregistry.repoAdmin)
讀取、寫入及刪除構件。
Artifact Registry 管理員
(roles/artifactregistry.admin)
建立及管理存放區和構件。
下列預先定義的角色也包含從 Container Registry 遷移至 Artifact Registry 所需的權限。

角色 說明
Container Registry -> Artifact Registry 遷移管理員 (roles/artifactregistry.containerRegistryMigrationAdmin) 包含執行遷移工具所需的所有權限
Artifact Registry 推送建立寫入者 (roles/artifactregistry.createOnPushWriter) 讀取及寫入構件。將映像檔推送至 gcr.io URL 時,建立 gcr.io 存放區。
Artifact Registry 推送建立存放區管理員 (roles/artifactregistry.createOnPushRepoAdmin) 讀取、寫入及刪除構件。建立 gcr.io 存放區。
如要查看每個角色包含的個別權限完整清單,請參閱「Artifact Registry 角色」。 您也可以使用 gcloud iam roles describe 指令,查看每個角色的權限清單。

基本 IAM 角色

基本角色是權限極高的角色,在 IAM 推出前就已存在。您不應在正式環境中授予基本角色,但可以在開發或測試環境中授予。

請盡可能使用預先定義的角色存取存放區,確保使用者和服務帳戶只具備必要權限。

如要進一步瞭解基本角色,請參閱 IAM 基本和預先定義角色參考資料

正在授予角色

如果專案中的所有存放區都適用相同角色,請在專案層級授予角色。如果部分帳戶需要不同存取層級,請在存放區層級授予角色。

如果您要授予虛擬存放區的角色,這些角色會套用至透過虛擬存放區提供的所有上游存放區,無論個別存放區的權限為何。

如果您使用 gcloud 指令授予角色,可以為主體指定單一角色繫結,也可以取得資源的允許政策、修改政策,然後設定修改後的允許政策,大規模變更政策。詳情請參閱「以程式輔助方式授予或撤銷多個角色」。

授予整個專案的角色

如果專案中的所有存放區都適用相同權限,請在專案層級授予角色。

如要將使用者或服務帳戶新增至專案,並授予 Artifact Registry 角色,請按照下列步驟操作:

主控台

  1. 在 Google Cloud 控制台中開啟「IAM」頁面。

    開啟「IAM」頁面

  2. 按一下「選取專案」,選擇執行 Artifact Registry 的專案,然後按一下「開啟」

  3. 按一下「新增」

  4. 輸入電子郵件地址。您可以將個人、服務帳戶或 Google 群組新增為主體。

  5. 為主體選取角色。請根據最低權限安全原則,授予存取必要 Artifact Registry 資源所需的最低權限。如要瞭解 Artifact Registry 預先定義的角色和權限,請參閱預先定義的 Artifact Registry 角色

  6. 按一下 [儲存]

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. 如要將角色授予單一主體,請執行下列指令:

    gcloud projects add-iam-policy-binding PROJECT \
       --member=PRINCIPAL \
       --role=ROLE

    其中

    • PROJECT 是執行 Artifact Registry 的專案 ID。
    • PRINCIPAL 是您要新增繫結的主體。請使用 user|group|serviceAccount:emaildomain:domain 格式。

      例如:user:test-user@gmail.comgroup:admins@example.comserviceAccount:test123@example.domain.comdomain:example.domain.com

    • ROLE 是您要授予的角色

    詳情請參閱 add-iam-policy-binding 說明文件。

    如要使用政策檔案授予角色,請參閱「以程式輔助方式授予或撤銷多個角色」一文。

授予存放區專屬角色

如要讓使用者或服務帳戶在專案中對每個存放區擁有不同層級的存取權,請授予存放區層級的角色。

主控台

如要授予特定存放區的存取權,請按照下列步驟操作:

  1. 在 Google Cloud 控制台中開啟「Repositories」(存放區) 頁面。

    開啟「存放區」頁面

  2. 選取適當的存放區。

  3. 如果未顯示資訊面板,請按一下選單列中的「顯示資訊面板」

  4. 在「權限」分頁中,按一下「新增主體」

  5. 輸入電子郵件地址。您可以將個人、服務帳戶或 Google 網路論壇新增為主體。

  6. 為主體選取角色。請根據最低權限安全原則,授予存取必要 Artifact Registry 資源所需的最低權限。如要瞭解 Artifact Registry 預先定義的角色和權限,請參閱預先定義的 Artifact Registry 角色

  7. 按一下 [儲存]

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. 您可以設定個別政策繫結的 IAM 集,或使用政策檔案。

    如要將角色授予單一主體,請執行下列指令:

    gcloud artifacts repositories add-iam-policy-binding REPOSITORY \
       --location=LOCATION \
       --member=PRINCIPAL \
       --role=ROLE

    其中

    • REPOSITORY 是存放區的 ID。
    • PRINCIPAL 是您要新增繫結的主體。請使用 user|group|serviceAccount:emaildomain:domain 格式。

      例如:user:test-user@gmail.comgroup:admins@example.comserviceAccount:test123@example.domain.comdomain:example.domain.com

    • ROLE 是您要授予的角色

    • LOCATION 是存放區的區域或多區域位置

    舉例來說,如要為使用者 write@gmail.com 新增角色 roles/artifactregistry.writer 的 IAM 政策繫結,並指定位置 --us-west1 中的存放區 my-repo,請執行下列指令:

    gcloud artifacts repositories add-iam-policy-binding my-repo \
    --location=us-west1 --member=user:write@gmail.com --role=roles/artifactregistry.writer

    如要使用政策檔案授予角色,請按照「以程式輔助方式授予或撤銷多個角色」一文所述的程序,使用 gcloud artifacts repositories get-iam-policygcloud artifacts repositories set-iam-policy 指令。

  3. Terraform

    使用 google_artifact_registry_repository_iam 資源設定 IAM 政策。以下範例會定義資源名稱為 repo-account 的服務帳戶,並授予該帳戶對資源名稱為 my-repo 的存放區的讀取權。

    如果您是初次使用 Terraform 搭配 Google Cloud,請參閱 HashiCorp 網站上的「 Get Started - Google Cloud」頁面。

    provider "google" {
        project = "PROJECT-ID"
    }
    
    resource "google_artifact_registry_repository" "my-repo"     {
      provider = google-beta
    
      location = "LOCATION"
      repository_id = "REPOSITORY"
      description = "DESCRIPTION"
      format = "FORMAT"
    }
    
    resource "google_service_account" "repo-account" {
      provider = google-beta
    
      account_id   = "ACCOUNT-ID"
      display_name = "Repository Service Account"
    }
    
    resource "google_artifact_registry_repository_iam_member" "repo-iam" {
      provider = google-beta
    
      location = google_artifact_registry_repository.my-repo.location
      repository = google_artifact_registry_repository.my-repo.name
      role   = "roles/artifactregistry.reader"
      member = "serviceAccount:${google_service_account.repo-account.email}"
    }
    

    ACCOUNT-ID 是服務帳戶的 ID。這是服務帳戶電子郵件欄位中 @ 符號前的部分。

    如需其他範例,請參閱 google_artifact_registry_repository_iam 資源的說明文件。

設定存放區的公開存取權

如要讓網路上任何人都能存取構件,而不需驗證身分,請將構件儲存在公開存放區。

如要設定存放區的公開唯讀存取權,請將 Artifact Registry Reader 角色授予主體 allUsers。我們也建議限制使用者要求配額,避免單一使用者耗盡專案的整體配額。

主控台

  1. 在 Google Cloud 控制台中開啟「Repositories」(存放區) 頁面。

    開啟「存放區」頁面

  2. 選取適當的存放區。

  3. 如果未顯示資訊面板,請按一下選單列中的「顯示資訊面板」

  4. 在「權限」分頁中,按一下「新增主體」

  5. 在「New principals」(新增主體) 欄位中輸入 allUsers

  6. 選取「Artifact Registry Reader」角色。

  7. 設定每位使用者的 Artifact Registry API 要求上限,防止未經驗證的使用者濫用。如需操作說明,請參閱「設定用量上限」。

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. 執行下列指令:

    gcloud artifacts repositories add-iam-policy-binding REPOSITORY \
    --location=LOCATION --member=allUsers --role=ROLE

    其中

    • REPOSITORY 是存放區的 ID。

    • ROLE 是您要授予的角色

    • LOCATION 是存放區的區域或多區域位置

    舉例來說,如要將 --us-west1 位置的存放區 my-repo 設為公開,請執行:

    gcloud artifacts repositories add-iam-policy-binding my-repo \
     --location=us-west1 --member=allUsers --role=roles/artifactregistry.reader

  3. 設定每位使用者的 Artifact Registry API 要求上限,防止未經驗證的使用者濫用。如需操作說明,請參閱「設定用量上限」。

正在撤銷角色

如要撤銷存放區的存取權,請從授權主體清單中移除主體。

如要移除存放區的公開存取權,請移除 allUsers 主體。

主控台

如要撤銷權限,請按照下列步驟操作:

  1. 在 Google Cloud 控制台中開啟「Repositories」(存放區) 頁面。

    開啟「存放區」頁面

  2. 選取適當的存放區。

  3. 如果未顯示資訊面板,請按一下選單列中的「顯示資訊面板」

  4. 在「權限」分頁中,展開適當的主體。如果要將公開存放區設為私人,請展開 allUsers 主體。

  5. 按一下「移除主體」即可撤銷存取權。

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. 如要在專案層級撤銷角色,請執行下列指令:

    gcloud projects remove-iam-policy-binding PROJECT \
       --member=PRINCIPAL \
       --role=ROLE
    • PROJECT 是專案 ID。
    • PRINCIPAL 是要移除繫結的主體。請使用 user|group|serviceAccount:emaildomain:domain 格式。

      例如:user:test-user@gmail.comgroup:admins@example.comserviceAccount:test123@example.domain.comdomain:example.domain.com

    • ROLE 是您要撤銷的角色

    如要撤銷存放區的角色,請執行下列指令:

    gcloud artifacts repositories remove-iam-policy-binding REPOSITORY
       --location=LOCATION \
       --member=PRINCIPAL \
       --role=ROLE

    其中

    • REPOSITORY 是存放區的 ID。
    • PRINCIPAL 是要移除繫結的主體。請使用 user|group|serviceAccount:emaildomain:domain 格式。

      例如:user:test-user@gmail.comgroup:admins@example.comserviceAccount:test123@example.domain.comdomain:example.domain.com

      如要撤銷存放區的公開存取權,請指定主體 allUsers

    • ROLE 是您要撤銷的角色

    舉例來說,如要移除位置 --us-west1 中,使用者 write@gmail.com 存放區 my-reporoles/artifactregistry.writer 角色政策繫結,請執行下列指令:

    gcloud artifacts repositories remove-iam-policy-binding my-repo \
       --location=us-west1 \
       --member=user:write@gmail.com \
       --role=roles/artifactregistry.writer

    如要撤銷位置 my-repo--us-west1 的公開存取權,請執行下列指令:

    gcloud artifacts repositories remove-iam-policy-binding my-repo \
       --location=us-west1 \
       --member=allUsers \
       --role=roles/artifactregistry.reader

使用標記授予條件存取權

專案管理員可以為 Google Cloud中的資源建立標記,並在 Resource Manager 中管理這些標記。將標記附加至 Artifact Registry 存放區後,管理員就能搭配 IAM 條件使用標記,授予存放區的條件式存取權。

您無法將標記附加至個別構件。

詳情請參閱下列說明文件:

與 Google Cloud 服務整合

對於大多數 Google Cloud 服務帳戶,設定登錄檔的存取權時,只需要授予適當的 IAM 角色。

Google Cloud 服務的預設服務帳戶

Google Cloud Cloud Build 或 Google Kubernetes Engine 等服務會使用預設服務帳戶服務代理程式,與同一專案中的資源互動。

如有下列情況,您必須自行設定或修改權限:

  • Google Cloud 服務與 Artifact Registry 位於不同專案。
  • 預設權限不符合需求。
  • 您使用使用者提供的服務帳戶與 Artifact Registry 互動,而非預設服務帳戶。
  • 您的機構政策設定禁止自動授予預設服務帳戶角色。

下列服務帳戶通常會存取 Artifact Registry。服務帳戶的電子郵件地址包含服務執行的專案 Google Cloud 專案 ID 或專案編號

服務 服務帳戶 電子郵件地址
App Engine 彈性環境 App Engine 服務帳戶 PROJECT-ID@appspot.gserviceaccount.com
Compute Engine Compute Engine 預設服務帳戶 PROJECT-NUMBER-compute@developer.gserviceaccount.com
Cloud Build Compute Engine 服務帳戶

舊版 Cloud Build 服務帳戶
視機構設定而定,預設服務帳戶電子郵件地址為下列其中一項:
  • Compute Engine:PROJECT-NUMBER-compute@developer.gserviceaccount.com
  • Cloud Build:PROJECT-NUMBER@cloudbuild.gserviceaccount.com
Cloud Run Cloud Run 服務代理人
run.googleapis.com 的服務代理人。
service-PROJECT-NUMBER@serverless-robot-prod.iam.gserviceaccount.com
GKE Compute Engine 預設服務帳戶
節點的預設服務帳戶。
PROJECT-NUMBER-compute@developer.gserviceaccount.com

視機構政策設定而定,系統可能會自動將專案的編輯者角色授予預設服務帳戶。強烈建議您 強制執行 iam.automaticIamGrantsForDefaultServiceAccounts 機構政策限制,停用自動授予角色功能。如果您是在 2024 年 5 月 3 日後建立機構,系統預設會強制執行這項限制。

如果停用自動角色授予功能,您必須決定要授予預設服務帳戶哪些角色,然後自行授予這些角色

如果預設服務帳戶已具備「編輯者」角色,建議您將「編輯者」角色替換為權限較少的角色。如要安全地修改服務帳戶的角色,請使用 政策模擬器查看變更的影響,然後授予及撤銷適當的角色

授予 Compute Engine 執行個體的存取權

存取存放區的 VM 執行個體必須同時設定 Artifact Registry 權限和儲存空間存取權範圍

服務帳戶的存取層級取決於授予服務帳戶的 IAM 角色,但 VM 執行個體的存取權範圍會根據執行個體上 gcloud CLI 和用戶端程式庫發出的要求,定義預設的 OAuth 範圍。因此,透過應用程式預設憑證進行驗證時,存取權範圍可能會進一步限制 API 方法的存取。

Compute Engine 使用下列預設值:

  • Compute Engine 預設服務帳戶是 VM 執行個體的身分。服務帳戶電子郵件地址的後置字串為 @developer.gserviceaccount.com
  • 如果您尚未停用這項行為,預設服務帳戶會具備 IAM 基本編輯者角色。
  • 使用預設服務帳戶建立的執行個體具有 Compute Engine 預設存取權範圍,包括儲存空間的唯讀存取權。雖然編輯者角色通常會授予寫入存取權,但 read-only 儲存空間存取範圍會限制執行個體服務帳戶,只能從同一專案中的任何存放區下載構件。

在下列情況下,您必須設定服務帳戶的存取權範圍:

  • VM 服務帳戶需要存取其他專案中的存放區。
  • VM 服務帳戶需要執行從存放區讀取構件以外的動作。這通常適用於 VM 上的第三方工具,這些工具需要推送映像檔或執行 Artifact Registry gcloud 指令。

如要設定角色和存取範圍,請按照下列步驟操作:

  1. 在含有 VM 執行個體的專案中,取得 Compute Engine 預設服務帳戶的名稱。服務帳戶電子郵件地址的後置字串為 @developer.gserviceaccount.com

  2. 在存放區所在的專案中授予權限,讓服務帳戶可以存取存放區。

  3. 使用 --scopes 選項設定存取權範圍。

    1. 停止 VM 執行個體。請參閱停止執行個體一文。

    2. 使用下列指令設定存取範圍:

      gcloud compute instances set-service-account INSTANCE --scopes=SCOPE
      

      SCOPE 改為適當的值。

      • Docker 支援下列選項:

        • storage-ro - 僅授予讀取權限,用於提取映像檔。
        • storage-rw - 授予推送或提取映像檔的讀取及寫入權限。
        • cloud-platform - 查看及管理各項服務的資料,包括中繼資料。Google Cloud
      • 如為其他格式,則必須使用 cloud-platform 範圍。

    3. 重新啟動 VM 執行個體。請參閱啟動已停止的執行個體

授予 Google Kubernetes Engine 叢集的存取權

如果符合下列所有條件,GKE 叢集和節點集區就能提取容器,不必進行任何額外設定:

如果 GKE 環境不符合這些規定,授權存取的指示會因您是使用 Compute Engine 預設服務帳戶,還是使用者提供的服務帳戶做為節點的身分而異。

預設服務帳戶

下列設定規定適用於 Compute Engine 預設服務帳戶

  1. 如果 GKE 與 Artifact Registry 位於不同專案,請授予服務帳戶必要權限。

  2. 如要推送映像檔、與容器以外格式的存放區互動,或從叢集執行 gcloud 指令,您必須在建立叢集或節點集區時,為服務帳戶設定存取權範圍

  3. 如果使用的 GKE 版本不支援,請設定 imagePullSecrets

使用者提供的服務帳戶

如要使用使用者提供的服務帳戶做為叢集的身分,請務必:

  1. 從 Artifact Registry 執行的Google Cloud 專案授予服務帳戶必要權限。

  2. 根據預設,使用使用者提供的服務帳戶建立叢集或節點集區時,系統會授予 cloud-platform 存取權範圍。

    如果您在 gcloud container clusters creategcloud container node-pools create 指令中使用 --scopes 標記,則必須加入適當的存取範圍,才能搭配 Artifact Registry 使用。

設定存取權範圍

存取權範圍是為 Compute Engine VM 指定授權的傳統方法。如要從 Artifact Registry 存放區提取映像檔,GKE 節點必須具備儲存空間唯讀存取範圍,或是包含儲存空間讀取存取權的其他儲存空間存取範圍。

您只能在建立叢集或節點集區時設定存取權範圍。您無法變更現有節點的存取權範圍。

  • 如果您使用 Compute Engine 預設服務帳戶,GKE 會建立具有 Compute Engine預設存取範圍的節點,包括儲存空間的唯讀存取權。
  • 如果您使用使用者提供的服務帳戶,GKE 會建立具有 cloud-platform 範圍的節點,這是大多數Google Cloud 服務所需的範圍。

如要在建立叢集時指定存取範圍,請執行下列指令:

gcloud container clusters create NAME --scopes=SCOPES

如要在建立節點集區時指定存取範圍,請執行下列指令:

gcloud container node-pools create NAME --scopes=SCOPES

替換下列值:

  • NAME 是叢集或節點集區的名稱。
  • SCOPES 是以半形逗號分隔的存取範圍清單,用來授予存取權。

    • 如要存取 Docker 存放區,請使用下列其中一個範圍:

    • storage-ro - 授予提取映像檔的唯讀權限。

    • storage-rw - 授予推送或提取映像檔的讀取及寫入權限。

    • cloud-platform - 查看及管理各項服務的資料,包括中繼資料。Google Cloud

    • 如要存取其他存放區,必須使用 cloud-platform 範圍。

    如需完整的範圍清單,請參閱 gcloud container clusters creategcloud container node-pools create 的說明文件。

如要進一步瞭解您在新建叢集時可以設定的範圍,請參閱 gcloud container clusters create 指令的說明文件。

設定 imagePullSecret

如何設定 imagePullSecret

  1. 在 GKE 專案中,找出 Compute Engine 預設服務帳戶。帳戶電子郵件地址的後置字串為 @developer.gserviceaccount.com

  2. 下載服務帳戶金鑰

  3. 在存放區所在的專案中,確認您已授予存放區權限

  4. 在叢集所在的專案中,使用服務帳戶金鑰建立名為 imagePullSecret 的密鑰 artifact-registry

    kubectl create secret docker-registry artifact-registry \
    --docker-server=https://LOCATION-docker.pkg.dev \
    --docker-email=SERVICE-ACCOUNT-EMAIL \
    --docker-username=_json_key \
    --docker-password="$(cat KEY-FILE)"
    

    更改下列內容:

    • LOCATION 是存放區的單一區域或多區域位置。
    • SERVICE-ACCOUNT-EMAIL 是 Compute Engine 服務帳戶的電子郵件地址。
    • KEY-FILE 是服務帳戶金鑰檔案的名稱。例如 `key.json`。
  5. 開啟預設服務帳戶:

    kubectl edit serviceaccount default --namespace default

    Kubernetes 叢集中的每個命名空間都有名為 default 的預設服務帳戶。這個預設服務帳戶會用於提取容器映像檔。

  6. 將新建立的 imagePullSecret 密鑰新增至預設服務帳戶:

    imagePullSecrets:
    - name: artifact-registry
    

    您的服務帳戶現在應如下所示:

    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: default
      namespace: default
      ...
    secrets:
    - name: default-token-zd84v
    # The secret you created:
    imagePullSecrets:
    - name: artifact-registry
    

現在,在目前的 default 命名空間中建立的任何新 Pod,都會定義 imagePullSecret 密鑰。

Artifact Registry 服務帳戶

Artifact Registry 服務代理程式是由 Google 代管的服務帳戶,與服務互動時會代表 Artifact Registry 執行動作。 Google Cloud如要進一步瞭解帳戶和權限,請參閱「Artifact Registry 服務帳戶」。

後續步驟

設定權限後,請進一步瞭解如何處理構件。

您也可以使用下載規則限制構件下載