本指南說明如何設定 Binary Authorization,強制執行 Cloud Run 服務和工作的政策式部署作業。
事前準備
設定 Cloud Run 並啟用 API,方法如下:
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Run, Artifact Registry, Binary Authorization APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
Install the Google Cloud CLI.
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Run, Artifact Registry, Binary Authorization APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
Install the Google Cloud CLI.
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init 前往 Google Cloud 控制台的 Cloud Run 頁面。
按一下所需服務。
按一下 [Security] (安全性) 分頁標籤。
如要啟用服務的二進位授權強制執行功能,請按一下「啟用」。
選用:如要設定二進位授權政策,請按一下「設定政策」。
如要建立新服務,請略過這個步驟。 如要更新現有服務,請下載其 YAML 設定:
gcloud run services describe SERVICE --format export > service.yaml
請按照下列方式更新
run.googleapis.com/binary-authorization:註解:apiVersion: serving.knative.dev/v1 kind: Service metadata: annotations: run.googleapis.com/binary-authorization: POLICY name: SERVICE spec: template:
更改下列內容:
- SERVICE:Cloud Run 的名稱
- POLICY:設為
default
使用下列指令,以新設定取代服務:
前往 Google Cloud 控制台的 Cloud Run 工作頁面。
按一下工作即可開啟工作詳細資料。
點選「設定」分頁標籤。
在「二進位授權」下方,從政策清單中選取政策。
按一下「套用」,即可對工作啟用二進位授權強制執行功能。
選用:如要設定二進位授權政策,請按一下「設定政策」。
JOB_NAME:工作名稱。POLICY:要套用的政策。如要使用預設政策,請使用default值。前往 Cloud Run 頁面:
按一下「Create service」(建立服務)。
在「建立服務」表單中:
- 選取 Cloud Run 做為開發平台。
- 選取您要使用服務的地區。
- 輸入服務名稱。
- 點選「下一步」,繼續前往「設定服務的第一個修訂版本」頁面。
- 選取「透過現有的容器映像檔部署單一修訂版本」。
- 輸入或選取要部署的映像檔。
- 展開「進階設定」部分。
- 按一下 [Security] (安全性) 分頁標籤。
勾選「以二進位授權驗證容器部署」核取方塊。
選用:按一下「設定政策」,設定二進位授權政策。如要進一步瞭解如何設定政策,請參閱「設定政策」。
部署服務。
SERVICE_NAME:服務名稱。IMAGE_URL:要部署的映像檔。REGION:要部署服務的區域。前往 Cloud Run 工作頁面:
按一下「建立工作」,顯示「建立工作」表單。
- 在表單中,指定含有工作程式碼的容器映像檔,或從先前部署的容器清單中選取。
- 系統會根據容器映像檔自動產生工作名稱。您可以視需要編輯或變更工作名稱。工作建立完成後,工作名稱即無法變更。
- 選取要執行工作的區域。
- 指定要在工作中執行的任務數量。所有工作都必須成功,工作才會視同成功。根據預設,工作會平行執行。
按一下「Container, Variables & Secrets, Connections, Security」(容器、變數和密鑰、連線、安全性),即可設定其他工作屬性。
按一下「設定」分頁標籤。在「資源」部分,設定下列項目:
- 在「記憶體」選單中,指定所需記憶體大小。 預設值為最低需求量 512 MiB。
- 在「CPU」選單中,指定所需的 CPU 數量。預設值為最低需求,也就是 1 個 CPU。
在「任務負荷能力」部分:
在「工作逾時」下方,以秒為單位指定工作可執行的最長時間,最多 168 小時 (7 天)。每項工作都必須在指定時間內完成。預設值為 10 分鐘。
在「每項失敗工作的重試次數」下方,指定工作失敗時的重試次數。預設值為 3 次重試。
在「Parallelism」(平行處理作業數量) 部分中:
- 在大多數情況下,您可以選取「盡可能並行執行更多工作」。
- 如果因為工作存取資源的調度限制,需要設定下限,請選取「限制並行任務數量上限」,並在「自訂平行處理限制」欄位中指定並行任務數量。
設定完工作後,請按一下「建立」,在 Cloud Run 中建立工作。
建立啟用二進位授權的新工作:
gcloud run jobs create JOB_NAME \ --image IMAGE_URL OPTIONS \ --binary-authorization=POLICY \ --region=REGION
更改下列內容:
等待工作建立完成。成功完成後,控制台會顯示成功訊息。
建立新的
job.yaml檔案,並加入以下內容:apiVersion: run.googleapis.com/v1 kind: Job metadata: name: JOB annotations: run.googleapis.com/binary-authorization: POLICY spec: template: spec: containers: - image: IMAGE
更改下列內容:
- JOB:Cloud Run 工作的名稱
- IMAGE:容器映像檔的網址
- POLICY:設為
default
使用下列指令部署新工作:
gcloud run jobs replace job.yaml
- 使用
built-by-cloud-build簽署者只部署 Cloud Build 建構的映像檔。 - 使用Google Cloud 控制台或指令列工具設定二進位授權政策。
- 使用認證,只部署已簽署的容器映像檔。
- 觀看影片,瞭解如何在 Cloud Run 中使用二進位授權,確保只部署核准的建構作業。
在現有 Cloud Run 服務上啟用二進位授權
您可以在現有服務上啟用二進位授權強制執行功能。 啟用強制執行後,您可能需要部署修訂版本或更新服務流量,才能啟動強制執行。
您可以使用 Google Cloud 控制台或 Google Cloud CLI,在現有服務上啟用二進位授權強制執行功能:
控制台
gcloud
在服務上啟用二進位授權並部署:
gcloud run services update SERVICE_NAME --binary-authorization=default
將 SERVICE_NAME 替換為服務名稱。
YAML
gcloud run services replace service.yaml
在現有 Cloud Run 工作上啟用二進位授權
您可以使用Google Cloud 控制台或 Google Cloud CLI,在現有工作中啟用二進位授權強制執行功能:
控制台
gcloud
在工作中啟用二進位授權:
gcloud run jobs update JOB_NAME --binary-authorization=POLICY
更改下列內容:
建議您為 Cloud Run 啟用二進位授權,方法是設定組織政策。如果未設定政策,Cloud Run 開發人員可以停用二進位授權。
查看政策
如要查看政策,請按一下「查看政策」。
詳情請參閱「設定二進位授權政策」。
服務或工作部署失敗
如果服務或工作因違反二進位授權政策而無法部署,您可能會看到類似下列內容的錯誤訊息:
Revision REVISION_NAME uses an unauthorized container image. Container image IMAGE_NAME is not authorized by policy.
錯誤訊息也會說明圖片違反政策的原因。在這種情況下,您可以使用急用權限略過政策強制執行,並部署映像檔。
在新服務上啟用二進位授權
您可以使用 Google Cloud 控制台或 Google Cloud CLI,在新服務上啟用二進位授權:
控制台
gcloud
在服務上啟用二進位授權並部署:
gcloud run deploy SERVICE_NAME --image=IMAGE_URL --binary-authorization=default --region=REGION
更改下列內容:
在新工作中啟用二進位授權
您可以使用下列任一方法,在新工作中啟用二進位授權:
控制台
gcloud
建立新工作時,Cloud Run 服務代理程式必須能夠存取容器 (預設情況下可以)。