本頁面說明如何使用 Gatekeeper 許可控制器,對 Google Kubernetes Engine (GKE) 叢集套用 Pod 層級的安全控管機制。本頁面說明如何使用 Gatekeeper 套用限制,以便套用安全性政策,協助您滿足組織的安全性需求。
本頁面適用於想對 GKE 叢集套用安全控管機制的資安專員。如要進一步瞭解 Google Cloud 內容中提及的常見角色和工作範例,請參閱「常見的 GKE 使用者角色和工作」。
閱讀本頁面前,請先熟悉下列概念:
Gatekeeper 總覽
Gatekeeper 是一種許可控制器,可使用 Open Policy Agent (OPA) 驗證在 Kubernetes 叢集建立及更新 Pod 的要求。
管理員可以使用 Gatekeeper,透過限制定義政策,也就是一組允許或拒絕 Kubernetes 部署行為的條件。接著,您可以使用 ConstraintTemplate 對叢集強制執行這些政策。本文提供限制工作負載安全功能的範例,確保使用 Gatekeeper 強制執行、測試及稽核安全性政策。
Gatekeeper 還可以:
- 推出政策:逐步在特定範圍內強制執行政策,降低工作負載中斷的風險。
- 試行政策變更:提供機制,在強制執行前測試政策影響和範圍。
- 稽核現有政策:確保安全控管機制套用至新舊工作負載 (稽核控管機制)。
Gatekeeper 的重要概念
Gatekeeper 導入了兩項概念,為管理員提供強大且彈性的叢集控制方式:限制和限制範本,這兩項概念都沿用自 Open Policy Agent 限制架構。
限制代表安全性政策,定義了強制執行的需求和範圍。限制範本是可重複使用的陳述式 (以 Rego 編寫),可根據限制中定義的需求,套用邏輯來評估 Kubernetes 物件中的特定欄位。
舉例來說,您可能有一項限制,宣告可套用至特定命名空間中 Pod 的允許 seccomp 設定檔,以及一項可比較的限制範本,提供擷取這些值和處理強制執行的邏輯。
下列限制範本來自 Gatekeeper 存放區,會檢查 Pod 規格中是否存在 securityContext.privileged:
apiVersion: templates.gatekeeper.sh/v1beta1
kind: ConstraintTemplate
metadata:
name: k8spspprivilegedcontainer
spec:
crd:
spec:
names:
kind: K8sPSPPrivilegedContainer
targets:
- target: admission.k8s.gatekeeper.sh
rego: |
package k8spspprivileged
violation[{"msg": msg, "details": {}}] {
c := input_containers[_]
c.securityContext.privileged
msg := sprintf("Privileged container is not allowed: %v, securityContext: %v", [c.name, c.securityContext])
}
input_containers[c] {
c := input.review.object.spec.containers[_]
}
input_containers[c] {
c := input.review.object.spec.initContainers[_]
}
如要擴充先前的限制範本範例,下列限制會定義在 dryrun 模式中,此限制範本特定強制執行的範圍 (kinds):
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sPSPPrivilegedContainer
metadata:
name: psp-privileged-container
spec:
enforcementAction: dryrun
match:
kinds:
- apiGroups: [""]
kinds: ["Pod"]
您可以使用 Gatekeeper 建立自己的限制和限制範本,滿足特定需求。您也可以使用 Gatekeeper 存放區中標準的一組限制和限制範本,這些限制和限制範本已定義,可快速採用並強制執行安全性。每項限制也附有 Pod 設定範例。
Google Cloud 提供官方支援的開放原始碼 Gatekeeper 代管版本,稱為 Policy Controller。Google 並未正式支援開放原始碼 Gatekeeper 專案。
事前準備
開始之前,請確認您已執行下列工作:
- 啟用 Google Kubernetes Engine API。 啟用 Google Kubernetes Engine API
- 如要使用 Google Cloud CLI 執行這項工作,請安裝並初始化 gcloud CLI。如果您先前已安裝 gcloud CLI,請執行
gcloud components update指令,取得最新版本。較舊的 gcloud CLI 版本可能不支援執行本文件中的指令。
使用 Policy Controller 在叢集上啟用 Gatekeeper
Policy Controller 是以 Gatekeeper 開放原始碼專案為基礎建構的政策引擎。Google 建議使用 Policy Controller,因為這項工具包含其他功能,可協助大規模強制執行政策,包括政策即程式碼、多叢集支援、與 Cloud Logging 整合,以及在 Google Cloud 控制台查看政策審查狀態。Policy Controller 適用於 GKE,但您也可以在叢集上安裝 Gatekeeper。
如要在叢集上啟用 Policy Controller,請按照安裝指南操作。
啟用限制和限制範本
您可以安裝並啟用 Gatekeeper 及其限制範本,不會對現有或新的工作負載造成負面影響。因此,建議您將所有適用的 Pod 安全性限制範本套用至叢集。
此外,您也可以實施 Gatekeeper 限制,強制控管特定物件,例如命名空間和 Pod。
請參考下方範例,在限制比對陳述式中定義 Pods,將範圍限制在 production 命名空間中的 Pod:
...
spec:
match:
kinds:
- apiGroups: [""]
kinds: ["Pod"]
namespaces:
- "production"
如要進一步瞭解 Constraint 和 ConstraintTemplate 物件的可用選項,請參閱「如何使用 Gatekeeper」。
測試政策
對現有叢集導入新政策可能會造成不良行為,例如限制現有工作負載。使用 Gatekeeper 確保 Pod 安全性的好處之一,是可以使用模擬測試模式,在不實際變更的情況下,測試政策的有效性和影響。這樣一來,您就能在不強制執行的情況下,針對執行中的叢集測試政策設定。系統會記錄並找出違反政策的行為,不會受到干擾。
下列步驟說明開發人員、操作人員或管理員如何套用限制範本和限制,判斷其成效或潛在影響:
套用 Gatekeeper 設定,複製資料以進行稽核和模擬測試:
kubectl create -f- <<EOF apiVersion: config.gatekeeper.sh/v1alpha1 kind: Config metadata: name: config namespace: "gatekeeper-system" spec: sync: syncOnly: - group: "" version: "v1" kind: "Namespace" - group: "" version: "v1" kind: "Pod" EOF在未套用任何限制的情況下,以進階權限執行工作負載:
kubectl create -f- <<EOF apiVersion: v1 kind: Pod metadata: name: nginx labels: app: nginx spec: containers: - name: nginx image: nginx securityContext: privileged: true EOF載入先前的
k8spspprivilegedcontainer限制範本:kubectl create -f- <<EOF apiVersion: templates.gatekeeper.sh/v1beta1 kind: ConstraintTemplate metadata: name: k8spspprivilegedcontainer spec: crd: spec: names: kind: K8sPSPPrivilegedContainer targets: - target: admission.k8s.gatekeeper.sh rego: | package k8spspprivileged violation[{"msg": msg, "details": {}}] { c := input_containers[_] c.securityContext.privileged msg := sprintf("Privileged container is not allowed: %v, securityContext: %v", [c.name, c.securityContext]) } input_containers[c] { c := input.review.object.spec.containers[_] } input_containers[c] { c := input.review.object.spec.initContainers[_] } EOF建立新的限制,擴充這個限制範本。這次請將
enforcementAction設為dryrun:kubectl create -f- <<EOF apiVersion: constraints.gatekeeper.sh/v1beta1 kind: K8sPSPPrivilegedContainer metadata: name: psp-privileged-container spec: enforcementAction: dryrun match: kinds: - apiGroups: [""] kinds: ["Pod"] EOFGatekeeper 會同步處理執行中的物件資料,並被動檢查是否有違規情形。請檢查限制的
status,確認是否發現任何違規情形:kubectl get k8spspprivilegedcontainer.constraints.gatekeeper.sh/psp-privileged-container -o yamlapiVersion: constraints.gatekeeper.sh/v1beta1 kind: K8sPSPPrivilegedContainer metadata: ... name: psp-privileged-container ... spec: enforcementAction: dryrun match: kinds: - apiGroups: - "" kinds: - Pod status: auditTimestamp: "2019-12-15T22:19:54Z" byPod: - enforced: true id: gatekeeper-controller-manager-0 violations: - enforcementAction: dryrun kind: Pod message: 'Privileged container is not allowed: nginx, securityContext: {"privileged": true}' name: nginx namespace: default如要確認政策不會干擾部署作業,請執行另一個具特殊權限的 Pod:
kubectl create -f- <<EOF apiVersion: v1 kind: Pod metadata: name: privpod labels: app: privpod spec: containers: - name: nginx image: nginx securityContext: privileged: true EOF這個新的 Pod 將成功部署。
如要清除在本節中建立的資源,請執行下列指令:
kubectl delete k8spspprivilegedcontainer.constraints.gatekeeper.sh/psp-privileged-container kubectl delete constrainttemplate k8spspprivilegedcontainer kubectl delete pod/nginx kubectl delete pod/privpod
強制執行政策
現在,您可以確認政策的有效性和影響,而不會影響現有或新的工作負載,因此可以全面實施政策。
以下步驟以用於驗證上述政策的範例為基礎,說明開發人員、操作人員或管理員如何套用限制範本和限制來強制執行政策:
載入先前所述的
k8spspprivilegedcontainer限制範本:kubectl create -f- <<EOF apiVersion: templates.gatekeeper.sh/v1beta1 kind: ConstraintTemplate metadata: name: k8spspprivilegedcontainer spec: crd: spec: names: kind: K8sPSPPrivilegedContainer targets: - target: admission.k8s.gatekeeper.sh rego: | package k8spspprivileged violation[{"msg": msg, "details": {}}] { c := input_containers[_] c.securityContext.privileged msg := sprintf("Privileged container is not allowed: %v, securityContext: %v", [c.name, c.securityContext]) } input_containers[c] { c := input.review.object.spec.containers[_] } input_containers[c] { c := input.review.object.spec.initContainers[_] } EOF建立新的限制,擴充這個限制範本。這次請勿設定
enforcementAction鍵。根據預設,enforcementAction鍵會設為deny:kubectl create -f- <<EOF apiVersion: constraints.gatekeeper.sh/v1beta1 kind: K8sPSPPrivilegedContainer metadata: name: psp-privileged-container spec: match: kinds: - apiGroups: [""] kinds: ["Pod"] EOF嘗試部署宣告特殊權限的容器:
kubectl create -f- <<EOF apiVersion: v1 kind: Pod metadata: name: nginx labels: app: nginx spec: containers: - name: nginx image: nginx securityContext: privileged: true EOF您應該會收到下列錯誤訊息:
Error from server ([denied by psp-privileged-container] Privileged container is not allowed: nginx, securityContext: {"privileged": true}): error when creating "STDIN": admission webhook "validation.gatekeeper.sh" denied the request: [denied by psp-privileged-container] Privileged container is not allowed: nginx, securityContext: {"privileged": true}如要清除,請執行下列指令:
kubectl delete k8spspprivilegedcontainer.constraints.gatekeeper.sh/psp-privileged-container kubectl delete constrainttemplate k8spspprivilegedcontainer
Gatekeeper 的替代方案
Gatekeeper 可讓您宣告及套用自訂 Pod 層級安全性政策。您也可以使用 Kubernetes 內建的 PodSecurity 許可控制器,套用預先定義的 Pod 層級安全性政策。這些預先定義的政策符合 Pod 安全性標準定義的層級。
後續步驟
Gatekeeper 提供極為強大的方法,可使用宣告式政策,在 GKE 叢集上強制執行及驗證安全性。不過,Gatekeeper 的用途不限於安全性,還可用於管理和作業的其他方面。