在叢集層級套用 Pod 安全標準

Pod 安全是一種許可控制器,會在建立新的 Pod 時,根據 Kubernetes Pod 安全標準執行檢查。這是 v1.25 中正式發佈的功能。本教學課程示範如何在叢集層級強制執行 baseline Pod 安全標準,該標準會將標準組態套用至叢集中的所有命名空間。

若要將 Pod 安全標準套用至特定命名空間,請參閱在命名空間層級套用 Pod 安全標準

如果您執行的 Kubernetes 版本不是 v1.32,請查看該版本的說明文件。

開始之前

在您的工作站上安裝以下項目

本教學課程示範您可以為完全控制的 Kubernetes 叢集設定哪些項目。如果您正在學習如何為您無法設定控制平面的託管叢集設定 Pod 安全許可,請閱讀在命名空間層級套用 Pod 安全標準

選擇要套用的正確 Pod 安全標準

Pod 安全許可可讓您使用以下模式套用內建的Pod 安全標準enforceauditwarn

若要收集資訊以協助您選擇最適合您組態的 Pod 安全標準,請執行以下操作

  1. 建立未套用 Pod 安全標準的叢集

    kind create cluster --name psa-wo-cluster-pss
    

    輸出結果類似於

    Creating cluster "psa-wo-cluster-pss" ...
    ✓ Ensuring node image (kindest/node:v1.32.0) 🖼
    ✓ Preparing nodes 📦
    ✓ Writing configuration 📜
    ✓ Starting control-plane 🕹️
    ✓ Installing CNI 🔌
    ✓ Installing StorageClass 💾
    Set kubectl context to "kind-psa-wo-cluster-pss"
    You can now use your cluster with:
    
    kubectl cluster-info --context kind-psa-wo-cluster-pss
    
    Thanks for using kind! 😊
    
  2. 將 kubectl context 設定為新的叢集

    kubectl cluster-info --context kind-psa-wo-cluster-pss
    

    輸出結果與此類似

    Kubernetes control plane is running at https://127.0.0.1:61350
    
    CoreDNS is running at https://127.0.0.1:61350/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
    
    To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
    
  3. 取得叢集中的命名空間列表

    kubectl get ns
    

    輸出結果與此類似

    NAME                 STATUS   AGE
    default              Active   9m30s
    kube-node-lease      Active   9m32s
    kube-public          Active   9m32s
    kube-system          Active   9m32s
    local-path-storage   Active   9m26s
    
  4. 使用 --dry-run=server 來了解當套用不同的 Pod 安全標準時會發生什麼事

    1. 權限提升

      kubectl label --dry-run=server --overwrite ns --all \
      pod-security.kubernetes.io/enforce=privileged
      

      輸出結果類似於

      namespace/default labeled
      namespace/kube-node-lease labeled
      namespace/kube-public labeled
      namespace/kube-system labeled
      namespace/local-path-storage labeled
      
    2. 基線

      kubectl label --dry-run=server --overwrite ns --all \
      pod-security.kubernetes.io/enforce=baseline
      

      輸出結果類似於

      namespace/default labeled
      namespace/kube-node-lease labeled
      namespace/kube-public labeled
      Warning: existing pods in namespace "kube-system" violate the new PodSecurity enforce level "baseline:latest"
      Warning: etcd-psa-wo-cluster-pss-control-plane (and 3 other pods): host namespaces, hostPath volumes
      Warning: kindnet-vzj42: non-default capabilities, host namespaces, hostPath volumes
      Warning: kube-proxy-m6hwf: host namespaces, hostPath volumes, privileged
      namespace/kube-system labeled
      namespace/local-path-storage labeled
      
    3. 限制性

      kubectl label --dry-run=server --overwrite ns --all \
      pod-security.kubernetes.io/enforce=restricted
      

      輸出結果類似於

      namespace/default labeled
      namespace/kube-node-lease labeled
      namespace/kube-public labeled
      Warning: existing pods in namespace "kube-system" violate the new PodSecurity enforce level "restricted:latest"
      Warning: coredns-7bb9c7b568-hsptc (and 1 other pod): unrestricted capabilities, runAsNonRoot != true, seccompProfile
      Warning: etcd-psa-wo-cluster-pss-control-plane (and 3 other pods): host namespaces, hostPath volumes, allowPrivilegeEscalation != false, unrestricted capabilities, restricted volume types, runAsNonRoot != true
      Warning: kindnet-vzj42: non-default capabilities, host namespaces, hostPath volumes, allowPrivilegeEscalation != false, unrestricted capabilities, restricted volume types, runAsNonRoot != true, seccompProfile
      Warning: kube-proxy-m6hwf: host namespaces, hostPath volumes, privileged, allowPrivilegeEscalation != false, unrestricted capabilities, restricted volume types, runAsNonRoot != true, seccompProfile
      namespace/kube-system labeled
      Warning: existing pods in namespace "local-path-storage" violate the new PodSecurity enforce level "restricted:latest"
      Warning: local-path-provisioner-d6d9f7ffc-lw9lh: allowPrivilegeEscalation != false, unrestricted capabilities, runAsNonRoot != true, seccompProfile
      namespace/local-path-storage labeled
      

從先前的輸出結果,你會注意到套用 privileged Pod 安全標準對任何命名空間都沒有顯示警告。然而,baselinerestricted 標準都有警告,特別是在 kube-system 命名空間中。

設定模式、版本和標準

在本節中,您將把以下 Pod 安全標準套用到 latest 版本

  • baseline 標準,在 enforce 模式中。
  • restricted 標準,在 warnaudit 模式中。

baseline Pod 安全標準提供了一個方便的折衷方案,可以縮短豁免列表,並防止已知的權限提升。

此外,為了防止 Pod 在 kube-system 中失敗,您將豁免該命名空間套用 Pod 安全標準。

當您在自己的環境中實作 Pod 安全許可控制時,請考慮以下事項

  1. 根據套用到叢集的風險態勢,更嚴格的 Pod 安全標準(如 restricted)可能是一個更好的選擇。

  2. 豁免 kube-system 命名空間允許 Pod 在此命名空間中以 privileged 身份執行。對於真實世界的應用,Kubernetes 專案強烈建議您套用嚴格的 RBAC 策略,限制對 kube-system 的存取,並遵循最小權限原則。若要實作上述標準,請執行以下操作

  3. 建立一個設定檔,Pod 安全許可控制程式可以使用該設定檔來實作這些 Pod 安全標準

    mkdir -p /tmp/pss
    cat <<EOF > /tmp/pss/cluster-level-pss.yaml
    apiVersion: apiserver.config.k8s.io/v1
    kind: AdmissionConfiguration
    plugins:
    - name: PodSecurity
      configuration:
        apiVersion: pod-security.admission.config.k8s.io/v1
        kind: PodSecurityConfiguration
        defaults:
          enforce: "baseline"
          enforce-version: "latest"
          audit: "restricted"
          audit-version: "latest"
          warn: "restricted"
          warn-version: "latest"
        exemptions:
          usernames: []
          runtimeClasses: []
          namespaces: [kube-system]
    EOF
    
  4. 設定 API 伺服器,使其在叢集建立期間取用此檔案

    cat <<EOF > /tmp/pss/cluster-config.yaml
    kind: Cluster
    apiVersion: kind.x-k8s.io/v1alpha4
    nodes:
    - role: control-plane
      kubeadmConfigPatches:
      - |
        kind: ClusterConfiguration
        apiServer:
            extraArgs:
              admission-control-config-file: /etc/config/cluster-level-pss.yaml
            extraVolumes:
              - name: accf
                hostPath: /etc/config
                mountPath: /etc/config
                readOnly: false
                pathType: "DirectoryOrCreate"
      extraMounts:
      - hostPath: /tmp/pss
        containerPath: /etc/config
        # optional: if set, the mount is read-only.
        # default false
        readOnly: false
        # optional: if set, the mount needs SELinux relabeling.
        # default false
        selinuxRelabel: false
        # optional: set propagation mode (None, HostToContainer or Bidirectional)
        # see https://kubernetes.dev.org.tw/docs/concepts/storage/volumes/#mount-propagation
        # default None
        propagation: None
    EOF
    
  5. 建立一個使用 Pod 安全許可控制來套用這些 Pod 安全標準的叢集

    kind create cluster --name psa-with-cluster-pss --config /tmp/pss/cluster-config.yaml
    

    輸出結果與此類似

    Creating cluster "psa-with-cluster-pss" ...
     ✓ Ensuring node image (kindest/node:v1.32.0) 🖼
     ✓ Preparing nodes 📦
     ✓ Writing configuration 📜
     ✓ Starting control-plane 🕹️
     ✓ Installing CNI 🔌
     ✓ Installing StorageClass 💾
    Set kubectl context to "kind-psa-with-cluster-pss"
    You can now use your cluster with:
    
    kubectl cluster-info --context kind-psa-with-cluster-pss
    
    Have a question, bug, or feature request? Let us know! https://kind.sigs.k8s.io/#community 🙂
    
  6. 將 kubectl 指向該叢集

    kubectl cluster-info --context kind-psa-with-cluster-pss
    

    輸出結果與此類似

    Kubernetes control plane is running at https://127.0.0.1:63855
    CoreDNS is running at https://127.0.0.1:63855/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
    
    To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
    
  7. 在預設命名空間中建立一個 Pod

    apiVersion: v1
     kind: Pod
     metadata:
       name: nginx
     spec:
       containers:
         - image: nginx
           name: nginx
           ports:
             - containerPort: 80
     
    kubectl apply -f https://k8s.io/examples/security/example-baseline-pod.yaml
    

    Pod 已正常啟動,但輸出結果包含一個警告

    Warning: would violate PodSecurity "restricted:latest": allowPrivilegeEscalation != false (container "nginx" must set securityContext.allowPrivilegeEscalation=false), unrestricted capabilities (container "nginx" must set securityContext.capabilities.drop=["ALL"]), runAsNonRoot != true (pod or container "nginx" must set securityContext.runAsNonRoot=true), seccompProfile (pod or container "nginx" must set securityContext.seccompProfile.type to "RuntimeDefault" or "Localhost")
    pod/nginx created
    

清理

現在執行以下命令來刪除您在上面建立的叢集

kind delete cluster --name psa-with-cluster-pss
kind delete cluster --name psa-wo-cluster-pss

接下來是什麼?

最後修改時間:2023 年 11 月 30 日 凌晨 1:22 PST:[en] Update cluster-level-pss.md show the code sample what user will apply (310a1221ac)