為 GKE 設定二進位授權政策
本快速入門指南說明如何在二進位授權政策中設定及測試基本規則。
在本快速入門導覽課程中,您將查看及設定政策中的預設規則,預設規則允許部署所有映像檔。您會在 Google Kubernetes Engine (GKE) 叢集部署容器映像檔,藉此測試這項功能。接著,您會將預設規則設為禁止部署所有映像檔,並嘗試部署映像檔。
事前準備
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自體驗產品的實際應用成效。新客戶還能獲得價值 $300 美元的免費抵免額,能用於執行、測試及部署工作負載。
-
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.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Artifact Registry, Binary Authorization APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
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.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Artifact Registry, Binary Authorization APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init - 安裝
kubectl。
必要的角色
如要取得使用 GKE 設定二進位授權政策所需的權限,請要求管理員在專案中授予您下列 IAM 角色:
- 二進位授權政策編輯者 (
roles/binaryauthorization.policyEditor) - Compute 檢視者 (
roles/compute.viewer) - Kubernetes Engine 管理員 (
roles/container.admin) - 服務帳戶使用者 (
roles/iam.serviceAccountUser)
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
建立已啟用二進位授權強制執行功能的叢集
現在,您要建立啟用二進位授權的 GKE 叢集。這是要執行已部署容器映像檔的叢集。
二進位授權適用於 Autopilot 或 Standard 叢集。
Google Cloud 控制台
下列步驟會設定 Autopilot 叢集。
在 Google Cloud 控制台中,前往 GKE「Kubernetes clusters」(Kubernetes 叢集) 頁面:
點選「建立」。
在「建立 Autopilot 叢集」中,執行下列步驟:
在「Name」(名稱) 欄位中,輸入
test-cluster。在「Region」(區域) 選單中,選取「
us-central1」。展開「進階設定」部分。
按一下「Security」連結,顯示「Security」面板。
在「Security」(安全性) 面板中,勾選「Enable Binary Authorization」(啟用二進位授權) 核取方塊。
選取「僅強制執行」。
依序點選「下一步」和「下一步:檢查並建立」。
如要開始建立叢集,請按一下「建立」。
gcloud
執行 gcloud container clusters create 並啟用 --binauthz-evaluation-mode=PROJECT_SINGLETON_POLICY_ENFORCE 旗標。
gcloud container clusters create \
--binauthz-evaluation-mode=PROJECT_SINGLETON_POLICY_ENFORCE \
--zone us-central1-a \
test-cluster
建立叢集可能需要幾分鐘的時間。
預設政策
根據預設,二進位授權政策會允許部署所有容器映像檔。
Google Cloud 控制台
如要查看預設政策,請按照下列步驟操作:
前往 Google Cloud 控制台的「二進位授權」頁面。
控制台會顯示政策的詳細資料。
點選「編輯政策」。
在「專案預設規則」中,選取「允許所有映像檔」選項。
gcloud
如要查看預設政策,請匯出政策 YAML 檔案,如下所示:
gcloud container binauthz policy export
根據預設,檔案包含下列內容:
globalPolicyEvaluationMode: ENABLE defaultAdmissionRule: evaluationMode: ALWAYS_ALLOW enforcementMode: ENFORCED_BLOCK_AND_AUDIT_LOG name: projects/PROJECT_ID/policy
REST API
如要查看預設政策,請按照下列步驟以 JSON 格式擷取:
curl \
-H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
-H "x-goog-user-project: ${PROJECT_ID}" \
"https://binaryauthorization.googleapis.com/v1/projects/${PROJECT_ID}/policy"
這個指令會產生下列輸出內容:
{
"name": "projects/PROJECT_ID/policy",
"globalPolicyEvaluationMode": "ENABLE",
"defaultAdmissionRule": {
"evaluationMode": "ALWAYS_ALLOW",
"enforcementMode": "ENFORCED_BLOCK_AND_AUDIT_LOG"
}
}
測試違規處置政策
您可以嘗試將範例容器映像檔部署至叢集,測試強制執行政策。
在本快速入門導覽課程中,您將使用 Artifact Registry 中路徑 us-docker.pkg.dev/google-samples/containers/gke/hello-app 的範例容器映像檔。這是 Google 建立的公開容器映像檔,內含「Hello, World!」範例應用程式。
Google Cloud 控制台
如要測試政策,請按照下列步驟操作:
前往Google Cloud 控制台的 GKE「Clusters」(叢集) 頁面。
點選「Deploy」(部署)。
控制台會提示您輸入部署作業的詳細資料。
在「Deployment name」(部署作業名稱) 欄位中輸入
hello-server。按一下「下一步:容器詳細資料」。
選取「現有容器映像檔」。
輸入
us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0做為容器映像檔路徑。點選「Deploy」(部署)。
kubectl
如要測試政策,請按照下列步驟操作:
更新本機
kubeconfig檔案:gcloud container clusters get-credentials \ --zone us-central1-a \ test-cluster這會提供在 GKE 中存取叢集所需的憑證和端點資訊。
部署映像檔:
kubectl run hello-server --image us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0 --port 8080
現在,請確認二進位授權是否允許部署作業。
Google Cloud 控制台
如要確認映像檔是否已部署,請前往 Google Cloud 控制台的 GKE「Workloads」(工作負載) 頁面。
部署作業的工作負載會顯示綠色圖示,表示映像檔已成功部署。
kubectl
如要確認映像檔已部署,請按照下列步驟操作:
kubectl get pods
這個指令會輸出類似以下的訊息,表示部署成功:
NAME READY STATUS RESTARTS AGE hello-server-579859fb5b-h2k8s 1/1 Running 0 1m
請務必刪除部署作業,才能繼續進行下一個步驟:
Google Cloud 控制台
如要刪除部署作業,請按照下列步驟操作:
返回Google Cloud 控制台的 GKE「Workloads」(工作負載) 頁面。
選取
hello-server工作負載。點選「刪除」。
kubectl
如要刪除部署作業,請按照下列步驟操作:
kubectl delete deployment hello-server
設定違規處置政策,禁止顯示所有映像檔
現在請修改政策,封鎖所有映像檔,不允許部署。
Google Cloud 控制台
如要修改政策,請按照下列步驟操作:
返回 Google Cloud 控制台的「Binary Authorization」(二進位授權) 頁面。
點選「編輯政策」。
選取「禁止所有圖片」。
點選 [儲存政策]。
gcloud
如要修改政策,請按照下列步驟操作:
匯出政策 YAML 檔案:
gcloud container binauthz policy export > /tmp/policy.yaml
在文字編輯器中,將
evaluationMode從ALWAYS_ALLOW變更為ALWAYS_DENY。政策 YAML 檔案應如下所示:
globalPolicyEvaluationMode: ENABLE defaultAdmissionRule: evaluationMode: ALWAYS_DENY enforcementMode: ENFORCED_BLOCK_AND_AUDIT_LOG name: projects/PROJECT_ID/policy
將政策 YAML 檔案匯回二進位授權:
gcloud container binauthz policy import /tmp/policy.yaml
REST API
如要修改政策,請按照下列步驟操作:
建立文字檔案,並以 JSON 格式輸入更新後的政策:
cat > /tmp/policy.json << EOM { "name": "projects/${PROJECT_ID}/policy", "globalPolicyEvaluationMode": "ENABLE", "defaultAdmissionRule": { "evaluationMode": "ALWAYS_DENY", "enforcementMode": "ENFORCED_BLOCK_AND_AUDIT_LOG" } } EOM將更新後的政策傳送至 REST API:
curl -X PUT \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \ -H "x-goog-user-project: ${PROJECT_ID}" \ --data-binary @/tmp/policy.json \ "https://binaryauthorization.googleapis.com/v1/projects/${PROJECT_ID}/policy"
重新測試政策
再次測試政策,將範例容器映像檔部署至叢集。這次,二進位授權會禁止部署映像檔。
Google Cloud 控制台
部署映像檔:
前往Google Cloud 控制台的 GKE「Clusters」(叢集) 頁面。
點選「Deploy」(部署)。
控制台會提示您輸入部署作業的詳細資料。
在「Deployment name」(部署作業名稱) 欄位中輸入
hello-server。按一下「下一步:容器詳細資料」。
選取「現有容器映像檔」。
輸入
us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0做為容器映像檔路徑。點選「Deploy」(部署)。
kubectl
部署映像檔:
kubectl run hello-server --image us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0 --port 8080
現在可以確認政策是否遭到封鎖:
Google Cloud 控制台
如要確認映像檔未部署,請執行下列操作:
返回Google Cloud 控制台的 GKE「Workloads」(工作負載) 頁面。
容器映像檔的工作負載會顯示紅色圖示,表示映像檔部署失敗。
kubectl
如要確認映像檔未部署,請執行下列指令:
kubectl get pods
指令會列印下列訊息,表示映像檔未部署:
No resources found.
您可以進一步瞭解部署作業:
kubectl get event --template \
'{{range.items}}{{"\033[0;36m"}}{{.reason}}:{{"\033[0m"}}{{.message}}{{"\n"}}{{end}}'
您會看到類似下列內容的回覆:
FailedCreate: Error creating: pods POD_NAME is forbidden: admission webhook "imagepolicywebhook.image-policy.k8s.io" denied the request: Image IMAGE_NAME denied by Binary Authorization default admission rule. Denied by always_deny admission rule
輸出內容如下:
- POD_NAME:Pod 的名稱。
- IMAGE_NAME:映文件名稱。
- ATTESTOR_NAME:驗證者名稱。
清除所用資源
為了避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請按照下列步驟操作。
刪除在 GKE 建立的叢集:
控制台
如要刪除叢集,請按照下列步驟操作:
前往Google Cloud 控制台的 GKE「Clusters」(叢集) 頁面。
選取
test-cluster叢集,然後按一下「刪除」。
gcloud
如要刪除叢集,請按照下列步驟操作:
gcloud container clusters delete \
--zone=us-central1-a \
test-cluster
後續步驟
- 使用
built-by-cloud-build驗證者僅部署 Cloud Build 建構的映像檔 (預先發布版)。 - 如需有關要求認證的端對端教學課程,請參閱:
- 參閱DevOps相關資源,並探索開發運作研究與評估 (DORA) 研究計畫。