使用 Pod 安全性標準受限制政策限制

Policy Controller 隨附預設的限制範本庫,可以與 Pod 安全性標準受限制政策套裝組合搭配使用。這個套裝組合不僅可以讓您實現與 Kubernetes Pod 安全性標準 (PSS) 受限制政策相同的多項保護措施,也能在強制執行政策之前,先進行測試並將特定資源從涵蓋範圍中排除。

本頁說明如何手動套用政策組合。 或者,您也可以直接套用政策組合

如果您是 IT 管理員和營運人員,想確保雲端平台中執行的所有資源都符合機構的法規遵循規定,請參閱本頁面,瞭解如何提供及維護自動化稽核或強制執行功能。如要進一步瞭解我們在內容中提及的常見角色和範例工作,請參閱「常見的 GKE Enterprise 使用者角色和工作」。 Google Cloud

這個套裝組合包含的限制會對應至下列 Kubernetes Pod 安全性標準 (PSS) 受限制政策控制項:

限制名稱 限制說明 控管機制名稱
pss-restricted-v2022-psp-volume-types 磁碟區類型用量 磁碟區類型
pss-restricted-v2022-privilege-escalation 限制提升為根層級權限 提權
pss-restricted-v2022-running-as-non-root 容器的 runAsNonRoot 以非根使用者身分執行
pss-restricted-v2022-running-as-non-root-user 容器的使用者 ID 以非超級使用者身分執行
pss-restricted-v2022-seccomp 容器使用的 seccomp 設定檔 Seccomp
pss-restricted-v2022-capabilities Linux 功能 功能

事前準備

  1. 安裝並初始化 Google Cloud CLI,其中提供這些操作說明中使用的 gcloudkubectl 指令。如果您使用 Cloud Shell,Google Cloud CLI 會預先安裝。
  2. 在叢集上安裝 Policy Controller 1.11.1 以上版本,並使用預設的限制範本程式庫。

稽核 Pod 安全性標準受限制政策套裝組合

您可以透過 Policy Controller,對 Kubernetes 叢集強制執行政策。如要測試工作負載及其是否符合上表列出的 Google 建議最佳做法,您可以「稽核」模式部署這些限制,找出違規事項,更重要的是,在 Kubernetes 叢集上強制執行限制前,您有機會修正這些問題。

您可以使用 kubectl、kptConfig Sync,將 spec.enforcementAction 設為 dryrun 來套用這些政策。

kubectl

  1. (選用) 使用 kubectl 預覽政策限制:

    kubectl kustomize https://github.com/GoogleCloudPlatform/gke-policy-library.git/anthos-bundles/pss-restricted-v2022
    
  2. 使用 kubectl 套用政策限制:

    kubectl apply -k https://github.com/GoogleCloudPlatform/gke-policy-library.git/anthos-bundles/pss-restricted-v2022
    

    輸出內容如下:

    k8spspallowprivilegeescalationcontainer.constraints.gatekeeper.sh/pss-restricted-v2022-privilege-escalation created
    k8spspallowedusers.constraints.gatekeeper.sh/pss-restricted-v2022-running-as-non-root created
    k8spspcapabilities.constraints.gatekeeper.sh/pss-restricted-v2022-capabilities created
    k8spspseccomp.constraints.gatekeeper.sh/pss-restricted-v2022-seccomp created
    k8spspvolumetypes.constraints.gatekeeper.sh/pss-restricted-v2022-psp-volume-types created
    
  3. 確認已安裝政策限制,並檢查叢集是否發生違規情形:

    kubectl get -k https://github.com/GoogleCloudPlatform/gke-policy-library.git/anthos-bundles/pss-restricted-v2022
    

    輸出結果會與下列內容相似:

    NAME                                                                                                          ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspallowprivilegeescalationcontainer.constraints.gatekeeper.sh/pss-restricted-v2022-privilege-escalation   dryrun               0
    
    NAME                                                                                    ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspallowedusers.constraints.gatekeeper.sh/pss-restricted-v2022-running-as-non-root   dryrun               0
    
    NAME                                                                             ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspcapabilities.constraints.gatekeeper.sh/pss-restricted-v2022-capabilities   dryrun               0
    
    NAME                                                                   ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspseccomp.constraints.gatekeeper.sh/pss-restricted-v2022-seccomp   dryrun               0
    
    NAME                                                                                ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspvolumetypes.constraints.gatekeeper.sh/pss-restricted-v2022-psp-volume-types   dryrun               0
    

KPT

  1. 安裝及設定 kpt。這些操作說明會使用 kpt 自訂及部署 Kubernetes 資源。

  2. 使用 kpt 從 GitHub 下載 Pod 安全性標準 (PSS) 受限制政策 v2022 組合:

    kpt pkg get https://github.com/GoogleCloudPlatform/gke-policy-library.git/anthos-bundles/pss-restricted-v2022
    
  3. 執行 set-enforcement-action kpt 函式,將政策的強制執行動作設為 dryrun

    kpt fn eval pss-restricted-v2022 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 \
      -- enforcementAction=dryrun
    
  4. 使用 kpt 初始化工作目錄,這會建立資源來追蹤變更:

    cd pss-restricted-v2022
    kpt live init
    
  5. 使用 kpt 套用政策限制:

    kpt live apply
    
  6. 確認已安裝政策限制,並檢查叢集是否發生違規情形:

    kpt live status --output table --poll-until current
    

    狀態為 CURRENT,表示限制條件已成功安裝。

Config Sync

  1. 安裝及設定 kpt。這些操作說明會使用 kpt 自訂及部署 Kubernetes 資源。

如果運算子使用 Config Sync 將政策部署至叢集,可以按照下列指示操作:

  1. 變更為 Config Sync 的同步目錄:

    cd SYNC_ROOT_DIR
    

    如要使用 resourcegroup.yaml 建立或附加 .gitignore,請按照下列步驟操作:

    echo resourcegroup.yaml >> .gitignore
    

  2. 建立專屬的 policies 目錄:

    mkdir -p policies
    
  3. 使用 kpt 從 GitHub 下載 Pod 安全性標準 (PSS) 受限制政策 v2022 組合:

    kpt pkg get https://github.com/GoogleCloudPlatform/gke-policy-library.git/anthos-bundles/pss-restricted-v2022 policies/pss-restricted-v2022
    
  4. 執行 set-enforcement-action kpt 函式,將政策的強制執行動作設為 dryrun

    kpt fn eval policies/pss-restricted-v2022 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=dryrun
    
  5. (選用) 預覽要建立的政策限制:

    kpt live init policies/pss-restricted-v2022
    kpt live apply --dry-run policies/pss-restricted-v2022
    
  6. 如果 Config Sync 的同步目錄使用 Kustomize,請將 policies/pss-baseline-v2022 新增至根目錄 kustomization.yaml。否則請移除 policies/pss-baseline-v2022/kustomization.yaml 檔案:

    rm SYNC_ROOT_DIR/policies/pss-baseline-v2022/kustomization.yaml
    
  7. 將變更推送至 Config Sync 存放區:

    git add SYNC_ROOT_DIR/policies/pss-restricted-v2022
    git commit -m 'Adding Pod Security Standards Restricted audit enforcement'
    git push
    
  8. 驗證安裝狀態:

    watch gcloud beta container fleet config-management status --project PROJECT_ID
    

    如果狀態為 SYNCED,表示政策已安裝完成。

查看政策違規事項

在稽核模式下安裝政策限制後,您可以使用 Policy Controller 資訊主頁,在 UI 中查看叢集違規情形。

您也可以使用 kubectl,透過下列指令查看叢集上的違規事項:

kubectl get constraint -l policycontroller.gke.io/bundleName=pss-restricted-v2022 -o json | jq -cC '.items[]| [.metadata.name,.status.totalViolations]'

如有違規事項,您可以透過下列方式查看每個限制的違規訊息清單:

kubectl get constraint -l policycontroller.gke.io/bundleName=pss-restricted-v2022 -o json | jq -C '.items[]| select(.status.totalViolations>0)| [.metadata.name,.status.violations[]?]'

變更 Pod 安全性標準受限制政策套裝組合的強制執行動作

審查叢集中的政策違規事項後,您可以考慮變更強制執行模式,讓 Admission Controller warndeny 阻止不符規定的資源套用至叢集。

kubectl

  1. 使用 kubectl 將政策的強制執行動作設為 warn

    kubectl get constraint -l policycontroller.gke.io/bundleName=pss-restricted-v2022 -o name | xargs -I {} kubectl patch {} --type='json' -p='[{"op":"replace","path":"/spec/enforcementAction","value":"warn"}]'
    
  2. 確認政策限制強制執行動作已更新:

    kubectl get constraint -l policycontroller.gke.io/bundleName=pss-restricted-v2022
    

KPT

  1. 執行 set-enforcement-action kpt 函式,將政策的強制執行動作設為 warn

    kpt fn eval -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=warn
    
  2. 套用政策限制:

    kpt live apply
    

Config Sync

如果運算子使用 Config Sync 將政策部署至叢集,可以按照下列指示操作:

  1. 變更為 Config Sync 的同步目錄:

    cd SYNC_ROOT_DIR
    
  2. 執行 set-enforcement-action kpt 函式,將政策的強制執行動作設為 warn

    kpt fn eval policies/pss-restricted-v2022 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=warn
    
  3. 將變更推送至 Config Sync 存放區:

    git add SYNC_ROOT_DIR/pss-restricted-v2022
    git commit -m 'Adding Pod Security Standards Restricted policy bundle warn enforcement'
    git push
    
  4. 驗證安裝狀態:

    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
        hostPort: 80
        name: wordpress
EOF

准入控制器應會產生警告,列出這項資源違反的政策,如下列範例所示:

Warning: [pss-baseline-v2022-host-ports] The specified hostNetwork and hostPort are not allowed, pod: wp-non-compliant. Allowed values: {"max": 0, "min": 0}
pod/wp-non-compliant created

移除 Pod 安全性標準受限制政策套裝組合

如有需要,可以從叢集中移除 Pod 安全性標準受限制政策套裝組合。

kubectl

  • 使用 kubectl 移除政策:

    kubectl delete constraint -l policycontroller.gke.io/bundleName=pss-restricted-v2022
    

KPT

  • 移除政策:

    kpt live destroy
    

Config Sync

如果運算子使用 Config Sync 將政策部署至叢集,可以按照下列指示操作:

  1. 將變更推送至 Config Sync 存放區:

    git rm -r SYNC_ROOT_DIR/policies/pss-restricted-v2022
    git commit -m 'Removing Pod Security Standards Restricted policies'
    git push
    
  2. 確認狀態:

    nomos status
    

    叢集應會顯示 SYNCED 狀態,且資源已移除。