在命名空间级别应用 Pod 安全标准

Pod Security Admission 是一个准入控制器,用于在创建 Pod 时应用 Pod 安全标准。它是一个在 v1.25 中 GA 的特性。在本教程中,你将一次一个命名空间地强制执行 baseline Pod 安全标准。

你还可以在集群级别一次性对多个命名空间应用 Pod 安全标准。有关说明,请参阅 在集群级别应用 Pod 安全标准

准备工作

在你的工作站上安装以下内容

创建集群

  1. 按如下方式创建一个 kind 集群

    kind create cluster --name psa-ns-level
    

    输出类似于:

    Creating cluster "psa-ns-level" ...
     ✓ Ensuring node image (kindest/node:v1.34.0) 🖼 
     ✓ Preparing nodes 📦  
     ✓ Writing configuration 📜 
     ✓ Starting control-plane 🕹️ 
     ✓ Installing CNI 🔌 
     ✓ Installing StorageClass 💾 
    Set kubectl context to "kind-psa-ns-level"
    You can now use your cluster with:
    
    kubectl cluster-info --context kind-psa-ns-level
    
    Not sure what to do next? 😅  Check out https://kind.kubernetes.ac.cn/docs/user/quick-start/
    
  2. 将 kubectl 上下文设置为新集群

    kubectl cluster-info --context kind-psa-ns-level
    

    输出类似于:

    Kubernetes control plane is running at https://127.0.0.1:50996
    CoreDNS is running at https://127.0.0.1:50996/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
    
    To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
    

创建命名空间

创建一个名为 example 的新命名空间

kubectl create ns example

输出类似于:

namespace/example created

启用该命名空间的 Pod 安全标准检查

  1. 使用内置 Pod 安全准入支持的标签在该命名空间上启用 Pod 安全标准。在此步骤中,你将配置一个检查,以对不符合最新版 baseline Pod 安全标准的 Pod 发出警告。

    kubectl label --overwrite ns example \
       pod-security.kubernetes.io/warn=baseline \
       pod-security.kubernetes.io/warn-version=latest
    
  2. 你可以使用标签在任何命名空间上配置多个 Pod 安全标准检查。以下命令将 enforce baseline Pod 安全标准,但会根据最新版本(默认值)对 restricted Pod 安全标准发出 warnaudit 警告。

    kubectl label --overwrite ns example \
      pod-security.kubernetes.io/enforce=baseline \
      pod-security.kubernetes.io/enforce-version=latest \
      pod-security.kubernetes.io/warn=restricted \
      pod-security.kubernetes.io/warn-version=latest \
      pod-security.kubernetes.io/audit=restricted \
      pod-security.kubernetes.io/audit-version=latest
    

验证 Pod 安全标准执行

  1. example 命名空间中创建一个 baseline Pod

    kubectl apply -n example -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
    
  2. default 命名空间中创建一个 baseline Pod

    kubectl apply -n default -f https://k8s.io/examples/security/example-baseline-pod.yaml
    

    输出类似于此

    pod/nginx created
    

Pod 安全标准的强制执行和警告设置仅应用于 example 命名空间。你可以在 default 命名空间中创建相同的 Pod,而不会收到任何警告。

清理

现在通过运行以下命令删除你上面创建的集群

kind delete cluster --name psa-ns-level

下一步

上次修改时间:2023 年 11 月 8 日晚上 10:12 PST: 修复英文文档中的 KinD 拼写错误。(ad52e828b5)