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

Note

本教程仅适用于新集群。

Pod 安全准入(PSA)在 v1.23 及更高版本默认启用, 因为它升级到测试版(beta)。 Pod 安全准入是在创建 Pod 时应用 Pod 安全标准的准入控制器。 在本教程中,你将应用 baseline Pod 安全标准,每次一个名字空间。

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

准备开始

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

创建集群

  1. 按照如下方式创建一个 KinD 集群:

    1. kind create cluster --name psa-ns-level --image kindest/node:v1.23.0

    输出类似于:

    1. Creating cluster "psa-ns-level" ...
    2. Ensuring node image (kindest/node:v1.23.0) 🖼
    3. Preparing nodes 📦
    4. Writing configuration 📜
    5. Starting control-plane 🕹️
    6. Installing CNI 🔌
    7. Installing StorageClass 💾
    8. Set kubectl context to "kind-psa-ns-level"
    9. You can now use your cluster with:
    10. kubectl cluster-info --context kind-psa-ns-level
    11. Not sure what to do next? 😅 Check out https://kind.sigs.k8s.io/docs/user/quick-start/
  2. 将 kubectl 上下文设置为新集群:

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

    输出类似于:

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

创建名字空间

创建一个名为 example 的新名字空间:

  1. kubectl create ns example

输出类似于:

  1. namespace/example created

应用 Pod 安全标准

  1. 使用内置 Pod 安全准入所支持的标签在此名字空间上启用 Pod 安全标准。 在这一步中,我们将根据最新版本(默认值)对基线 Pod 安全标准发出警告。

    1. kubectl label --overwrite ns example \
    2. pod-security.kubernetes.io/warn=baseline \
    3. pod-security.kubernetes.io/warn-version=latest
  2. 可以使用标签在任何名字空间上启用多个 Pod 安全标准。 以下命令将强制(enforce) 执行基线(baseline)Pod 安全标准, 但根据最新版本(默认值)对受限(restricted)Pod 安全标准执行警告(warn)和审核(audit)。

    1. kubectl label --overwrite ns example \
    2. pod-security.kubernetes.io/enforce=baseline \
    3. pod-security.kubernetes.io/enforce-version=latest \
    4. pod-security.kubernetes.io/warn=restricted \
    5. pod-security.kubernetes.io/warn-version=latest \
    6. pod-security.kubernetes.io/audit=restricted \
    7. pod-security.kubernetes.io/audit-version=latest

验证 Pod 安全标准

  1. example 名字空间中创建一个最小的 pod:

    1. cat <<EOF > /tmp/pss/nginx-pod.yaml
    2. apiVersion: v1
    3. kind: Pod
    4. metadata:
    5. name: nginx
    6. spec:
    7. containers:
    8. - image: nginx
    9. name: nginx
    10. ports:
    11. - containerPort: 80
    12. EOF
  2. 将 Pod 规约应用到集群中的 example 名字空间中:

    1. kubectl apply -n example -f /tmp/pss/nginx-pod.yaml

    输出类似于:

    1. 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")
    2. pod/nginx created
  3. 将 Pod 规约应用到集群中的 default 名字空间中:

    1. kubectl apply -n default -f /tmp/pss/nginx-pod.yaml

    输出类似于:

    1. pod/nginx created

以上 Pod 安全标准仅被应用到 example 名字空间。 你可以在没有警告的情况下在 default 名字空间中创建相同的 Pod。

清理

运行 kind delete cluster -name psa-ns-level 删除创建的集群。

接下来

最后修改 June 23, 2022 at 8:06 PM PST: [zh]Update tutorials pages for links with ‘/zh/‘ prefix, using new prefix ‘/zh-cn/‘ (4ec6556889)