Policy Controller 隨附預設的限制範本庫,可以與 Pod 安全性政策套裝組合搭配使用,實現與 Kubernetes Pod 安全性政策 (PSP) 相同的多項保護措施,也能在強制執行政策之前,先進行測試並將特定資源從涵蓋範圍中排除。
本頁說明如何手動套用政策組合。 或者,您也可以直接套用政策組合。
如果您是 IT 管理員和營運人員,想確保雲端平台中執行的所有資源都符合機構的法規遵循規定,請參閱本頁面,瞭解如何提供及維護自動化稽核或強制執行功能。如要進一步瞭解我們在內容中提及的常見角色和範例工作,請參閱「常見的 GKE Enterprise 使用者角色和工作」。 Google Cloud
這個套裝組合包含下列限制,可提供對應至下列 Kubernetes Pod 安全性政策 (PSP) 欄位名稱 (控制項 ID) 的參數:
限制名稱 | 限制說明 | 控制項 ID |
---|---|---|
psp-v2022-psp-allow-privilege-escalation | 限制提升為根層級權限 | allowPrivilegeEscalation |
psp-v2022-psp-apparmor | 容器使用的 AppArmor 設定檔 | 註解 |
psp-v2022-psp-capabilities | Linux 功能 | allowedCapabilities、requiredDropCapabilities |
psp-v2022-flexvolume-drivers | 允許特定 FlexVolume 驅動程式 | allowedFlexVolumes |
psp-v2022-psp-forbidden-sysctls | 容器使用的 sysctl 設定檔 | forbiddenSysctls |
psp-v2022-psp-fsgroup | 分配擁有 Pod 磁碟區的 FSGroup | fsGroup |
psp-v2022-psp-host-filesystem | 使用主機檔案系統 | allowedHostPaths |
psp-v2022-psp-host-namespace | 主機命名空間的使用方式 | hostPID、hostIPC |
psp-v2022-psp-host-network-ports | 主機網路和通訊埠的使用情形 | hostNetwork、hostPorts |
psp-v2022-psp-privileged-container | 執行具備特殊權限的容器 | 特殊權限 |
psp-v2022-psp-proc-mount | 容器允許的 Proc Mount 類型 | allowedProcMountTypes |
psp-v2022-psp-readonlyrootfilesystem | 要求使用唯讀根檔案系統 | readOnlyRootFilesystem |
psp-v2022-psp-seccomp | 容器使用的 seccomp 設定檔 | 註解 |
psp-v2022-psp-selinux-v2 | 容器的 SELinux 環境 | seLinux |
psp-v2022-psp-pods-allowed-user-ranges | 容器的使用者和群組 ID | runAsUser、runAsGroup、supplementalGroups、fsGroup |
psp-v2022-psp-volume-types | 磁碟區類型用量 | volumes |
事前準備
- 安裝並初始化 Google Cloud CLI,其中提供這些操作說明中使用的
gcloud
和kubectl
指令。如果您使用 Cloud Shell,Google Cloud CLI 會預先安裝。 - 在叢集上安裝 Policy Controller,並使用預設的限制範本庫。
稽核 Pod 安全性政策政策套裝組合
您可以透過 Policy Controller,對 Kubernetes 叢集強制執行政策。如要測試工作負載及其是否符合上表列出的 Google 建議最佳做法,您可以「稽核」模式部署這些限制,找出違規事項,更重要的是,在 Kubernetes 叢集上強制執行限制前,您有機會修正這些問題。
您可以使用 kubectl、kpt 或 Config Sync,將 spec.enforcementAction
設為 dryrun
來套用這些政策。
kubectl
(選用) 使用 kubectl 預覽政策限制:
kubectl kustomize https://github.com/GoogleCloudPlatform/gke-policy-library.git/bundles/psp-v2022
使用 kubectl 套用政策限制:
kubectl apply -k https://github.com/GoogleCloudPlatform/gke-policy-library.git/bundles/psp-v2022
輸出內容如下:
k8spspallowprivilegeescalationcontainer.constraints.gatekeeper.sh/psp-v2022-psp-allow-privilege-escalation created k8spspallowedusers.constraints.gatekeeper.sh/psp-v2022-psp-pods-allowed-user-ranges created k8spspapparmor.constraints.gatekeeper.sh/psp-v2022-psp-apparmor created k8spspcapabilities.constraints.gatekeeper.sh/psp-v2022-psp-capabilities created k8spspfsgroup.constraints.gatekeeper.sh/psp-v2022-psp-fsgroup created k8spspflexvolumes.constraints.gatekeeper.sh/psp-v2022-psp-flexvolume-drivers created k8spspforbiddensysctls.constraints.gatekeeper.sh/psp-v2022-psp-forbidden-sysctls created k8spsphostfilesystem.constraints.gatekeeper.sh/psp-v2022-psp-host-filesystem created k8spsphostnamespace.constraints.gatekeeper.sh/psp-v2022-psp-host-namespace created k8spsphostnetworkingports.constraints.gatekeeper.sh/psp-v2022-psp-host-network-ports created k8spspprivilegedcontainer.constraints.gatekeeper.sh/psp-v2022-psp-privileged-container created k8spspprocmount.constraints.gatekeeper.sh/psp-v2022-psp-proc-mount created k8spspreadonlyrootfilesystem.constraints.gatekeeper.sh/psp-v2022-psp-readonlyrootfilesystem created k8spspselinuxv2.constraints.gatekeeper.sh/psp-v2022-psp-selinux-v2 created k8spspseccomp.constraints.gatekeeper.sh/psp-v2022-psp-seccomp created k8spspvolumetypes.constraints.gatekeeper.sh/psp-v2022-psp-volume-types created
確認已安裝政策限制,並檢查叢集是否發生違規情形:
kubectl get -k https://github.com/GoogleCloudPlatform/gke-policy-library.git/bundles/psp-v2022
輸出結果會與下列內容相似:
NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spspallowprivilegeescalationcontainer.constraints.gatekeeper.sh/psp-v2022-psp-allow-privilege-escalation dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spspallowedusers.constraints.gatekeeper.sh/psp-v2022-psp-pods-allowed-user-ranges dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spspapparmor.constraints.gatekeeper.sh/psp-v2022-psp-apparmor 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spspcapabilities.constraints.gatekeeper.sh/psp-v2022-psp-capabilities dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spspfsgroup.constraints.gatekeeper.sh/psp-v2022-psp-fsgroup 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spspflexvolumes.constraints.gatekeeper.sh/psp-v2022-psp-flexvolume-drivers 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spspforbiddensysctls.constraints.gatekeeper.sh/psp-v2022-psp-forbidden-sysctls 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spsphostfilesystem.constraints.gatekeeper.sh/psp-v2022-psp-host-filesystem 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spsphostnamespace.constraints.gatekeeper.sh/psp-v2022-psp-host-namespace dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spsphostnetworkingports.constraints.gatekeeper.sh/psp-v2022-psp-host-network-ports dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spspprivilegedcontainer.constraints.gatekeeper.sh/psp-v2022-psp-privileged-container dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spspprocmount.constraints.gatekeeper.sh/psp-v2022-psp-proc-mount 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spspreadonlyrootfilesystem.constraints.gatekeeper.sh/psp-v2022-psp-readonlyrootfilesystem 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spspselinuxv2.constraints.gatekeeper.sh/psp-v2022-psp-selinux-v2 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spspseccomp.constraints.gatekeeper.sh/psp-v2022-psp-seccomp dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spspvolumetypes.constraints.gatekeeper.sh/psp-v2022-psp-volume-types 0
(選用) 視叢集環境需求,調整限制檔案中的 PSP 欄位名稱
parameters
。如要瞭解詳情,請查看上表中特定 PSP 欄位名稱的連結。例如,在psp-host-network-ports
中:parameters: hostNetwork: true min: 80 max: 9000
KPT
安裝及設定 kpt。這些操作說明會使用 kpt 自訂及部署 Kubernetes 資源。
使用 kpt 從 GitHub 下載 Pod 安全性政策 (PSP) 2022 版政策套件:
kpt pkg get https://github.com/GoogleCloudPlatform/gke-policy-library.git/bundles/psp-v2022
執行
set-enforcement-action
kpt 函式,將政策的強制執行動作設為dryrun
:kpt fn eval psp-v2022 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 \ -- enforcementAction=dryrun
使用 kpt 初始化工作目錄,這會建立資源來追蹤變更:
cd psp-v2022 kpt live init
(選用) 視叢集環境需求,調整限制檔案中的 PSP 欄位名稱
parameters
。如要瞭解詳情,請查看上表中特定 PSP 欄位名稱的連結。例如,在psp-host-network-ports
中:parameters: hostNetwork: true min: 80 max: 9000
使用 kpt 套用政策限制:
kpt live apply
確認已安裝政策限制,並檢查叢集是否發生違規情形:
kpt live status --output table --poll-until current
狀態為
CURRENT
,表示限制條件已成功安裝。
Config Sync
- 安裝及設定 kpt。這些操作說明會使用 kpt 自訂及部署 Kubernetes 資源。
如果運算子使用 Config Sync 將政策部署至叢集,可以按照下列指示操作:
變更為 Config Sync 的同步目錄:
cd SYNC_ROOT_DIR
如要使用
resourcegroup.yaml
建立或附加.gitignore
,請按照下列步驟操作:echo resourcegroup.yaml >> .gitignore
建立專屬的
policies
目錄:mkdir -p policies
使用 kpt 從 GitHub 下載 Pod 安全性政策 (PSP) 2022 版政策套件:
kpt pkg get https://github.com/GoogleCloudPlatform/gke-policy-library.git/bundles/psp-v2022 policies/psp-v2022
執行
set-enforcement-action
kpt 函式,將政策的強制執行動作設為dryrun
:kpt fn eval policies/psp-v2022 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=dryrun
(選用) 視叢集環境需求,調整限制檔案中的 PSP 欄位名稱
parameters
。如要瞭解詳情,請查看上表中特定 PSP 欄位名稱的連結。例如,在psp-host-network-ports
中:parameters: hostNetwork: true min: 80 max: 9000
(選用) 預覽要建立的政策限制:
kpt live init policies/psp-v2022 kpt live apply --dry-run policies/psp-v2022
輸出內容如下:
Dry-run strategy: client inventory update started inventory update finished apply phase started k8spspallowprivilegeescalationcontainer.constraints.gatekeeper.sh/psp-v2022-psp-allow-privilege-escalation apply skipped: inventory policy prevented actuation (strategy: Apply, status: Empty, policy: MustMatch) k8spspallowedusers.constraints.gatekeeper.sh/psp-v2022-psp-pods-allowed-user-ranges apply skipped: inventory policy prevented actuation (strategy: Apply, status: Empty, policy: MustMatch) k8spspapparmor.constraints.gatekeeper.sh/psp-v2022-psp-apparmor apply skipped: inventory policy prevented actuation (strategy: Apply, status: Empty, policy: MustMatch) k8spspcapabilities.constraints.gatekeeper.sh/psp-v2022-psp-capabilities apply skipped: inventory policy prevented actuation (strategy: Apply, status: Empty, policy: MustMatch) k8spspfsgroup.constraints.gatekeeper.sh/psp-v2022-psp-fsgroup apply skipped: inventory policy prevented actuation (strategy: Apply, status: Empty, policy: MustMatch) k8spspflexvolumes.constraints.gatekeeper.sh/psp-v2022-psp-flexvolume-drivers apply skipped: inventory policy prevented actuation (strategy: Apply, status: Empty, policy: MustMatch) k8spspforbiddensysctls.constraints.gatekeeper.sh/psp-v2022-psp-forbidden-sysctls apply skipped: inventory policy prevented actuation (strategy: Apply, status: Empty, policy: MustMatch) k8spsphostfilesystem.constraints.gatekeeper.sh/psp-v2022-psp-host-filesystem apply skipped: inventory policy prevented actuation (strategy: Apply, status: Empty, policy: MustMatch) k8spsphostnamespace.constraints.gatekeeper.sh/psp-v2022-psp-host-namespace apply skipped: inventory policy prevented actuation (strategy: Apply, status: Empty, policy: MustMatch) k8spsphostnetworkingports.constraints.gatekeeper.sh/psp-v2022-psp-host-network-ports apply skipped: inventory policy prevented actuation (strategy: Apply, status: Empty, policy: MustMatch) k8spspprivilegedcontainer.constraints.gatekeeper.sh/psp-v2022-psp-privileged-container apply skipped: inventory policy prevented actuation (strategy: Apply, status: Empty, policy: MustMatch) k8spspprocmount.constraints.gatekeeper.sh/psp-v2022-psp-proc-mount apply skipped: inventory policy prevented actuation (strategy: Apply, status: Empty, policy: MustMatch) k8spspreadonlyrootfilesystem.constraints.gatekeeper.sh/psp-v2022-psp-readonlyrootfilesystem apply skipped: inventory policy prevented actuation (strategy: Apply, status: Empty, policy: MustMatch) k8spspselinuxv2.constraints.gatekeeper.sh/psp-v2022-psp-selinux-v2 apply skipped: inventory policy prevented actuation (strategy: Apply, status: Empty, policy: MustMatch) k8spspseccomp.constraints.gatekeeper.sh/psp-v2022-psp-seccomp apply skipped: inventory policy prevented actuation (strategy: Apply, status: Empty, policy: MustMatch) k8spspvolumetypes.constraints.gatekeeper.sh/psp-v2022-psp-volume-types apply skipped: inventory policy prevented actuation (strategy: Apply, status: Empty, policy: MustMatch) apply phase finished inventory update started inventory update finished apply result: 16 attempted, 0 successful, 16 skipped, 0 failed
如果 Config Sync 的同步目錄使用 Kustomize,請將
policies/psp-v2022
新增至根目錄kustomization.yaml
。否則請移除policies/psp-v2022/kustomization.yaml
檔案:rm SYNC_ROOT_DIR/policies/psp-v2022/kustomization.yaml
將變更推送至 Config Sync 存放區:
git add SYNC_ROOT_DIR/policies/psp-v2022 git commit -m 'Adding Pod Security Policy audit enforcement' git push
驗證安裝狀態:
watch gcloud beta container fleet config-management status --project PROJECT_ID
如果狀態為
SYNCED
,表示政策已安裝完成。
查看政策違規事項
在稽核模式下安裝政策限制後,您可以使用 Policy Controller 資訊主頁,在 UI 中查看叢集違規情形。
您也可以使用 kubectl
,透過下列指令查看叢集上的違規事項:
kubectl get constraint -l policycontroller.gke.io/bundleName=psp-v2022 -o json | jq -cC '.items[]| [.metadata.name,.status.totalViolations]'
如有違規事項,您可以透過下列方式查看每個限制的違規訊息清單:
kubectl get constraint -l policycontroller.gke.io/bundleName=psp-v2022 -o json | jq -C '.items[]| select(.status.totalViolations>0)| [.metadata.name,.status.violations[]?]'
變更 Pod 安全性政策政策套件強制執行動作
審查叢集中的政策違規事項後,您可以考慮變更強制執行模式,讓 Admission Controller warn
或 deny
阻止不符規定的資源套用至叢集。
kubectl
使用 kubectl 將政策的強制執行動作設為
warn
:kubectl get constraint -l policycontroller.gke.io/bundleName=psp-v2022 -o name | xargs -I {} kubectl patch {} --type='json' -p='[{"op":"replace","path":"/spec/enforcementAction","value":"warn"}]'
確認政策限制強制執行動作已更新:
kubectl get constraint -l policycontroller.gke.io/bundleName=psp-v2022
KPT
執行
set-enforcement-action
kpt 函式,將政策的強制執行動作設為warn
:kpt fn eval -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=warn
套用政策限制:
kpt live apply
Config Sync
如果運算子使用 Config Sync 將政策部署至叢集,可以按照下列指示操作:
變更為 Config Sync 的同步目錄:
cd SYNC_ROOT_DIR
執行
set-enforcement-action
kpt 函式,將政策的強制執行動作設為warn
:kpt fn eval policies/psp-v2022 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=warn
將變更推送至 Config Sync 存放區:
git add SYNC_ROOT_DIR/policies/psp-v2022 git commit -m 'Adding Pod Security Policy policy bundle warn enforcement' git push
驗證安裝狀態:
nomos status
叢集應顯示
SYNCED
狀態,並列出已安裝的政策。
測試政策強制執行
使用下列指令在叢集上建立不符規定的資源:
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
namespace: default
name: wp-non-compliant
labels:
app: wordpress
spec:
containers:
- image: wordpress
name: wordpress
ports:
- containerPort: 80
name: wordpress
EOF
准入控制器應會產生警告,列出這項資源違反的政策,如下列範例所示:
Warning: [psp-v2022-psp-pods-allowed-user-ranges] Container wordpress is attempting to run without a required securityContext/fsGroup. Allowed fsGroup: {"ranges": [{"max": 200, "min": 100}], "rule": "MustRunAs"} Warning: [psp-v2022-psp-pods-allowed-user-ranges] Container wordpress is attempting to run without a required securityContext/runAsGroup. Allowed runAsGroup: {"ranges": [{"max": 200, "min": 100}], "rule": "MustRunAs"} Warning: [psp-v2022-psp-pods-allowed-user-ranges] Container wordpress is attempting to run without a required securityContext/runAsUser Warning: [psp-v2022-psp-pods-allowed-user-ranges] Container wordpress is attempting to run without a required securityContext/supplementalGroups. Allowed supplementalGroups: {"ranges": [{"max": 200, "min": 100}], "rule": "MustRunAs"} Warning: [psp-v2022-psp-allow-privilege-escalation] Privilege escalation container is not allowed: wordpress Warning: [psp-v2022-psp-seccomp] Seccomp profile 'not configured' is not allowed for container 'wordpress'. Found at: no explicit profile found. Allowed profiles: {"RuntimeDefault", "docker/default", "runtime/default"} Warning: [psp-v2022-psp-capabilities] container <wordpress> is not dropping all required capabilities. Container must drop all of ["must_drop"] or "ALL" Warning: [psp-v2022-psp-readonlyrootfilesystem] only read-only root filesystem container is allowed: wordpress pod/wp-non-compliant created
移除 Pod 安全性政策政策套裝組合
如有需要,可以從叢集移除 Pod 安全性政策政策套裝組合。
kubectl
使用 kubectl 移除政策:
kubectl delete constraint -l policycontroller.gke.io/bundleName=psp-v2022
KPT
移除政策:
kpt live destroy
Config Sync
如果運算子使用 Config Sync 將政策部署至叢集,可以按照下列指示操作:
將變更推送至 Config Sync 存放區:
git rm -r SYNC_ROOT_DIR/policies/psp-v2022 git commit -m 'Removing PSP policies' git push
確認狀態:
nomos status
叢集應顯示
SYNCED
狀態,且資源已移除。