在命名空间级别应用 Pod 安全标准
注意
本教程仅适用于新集群。Pod 安全准入是一个准入控制器,它在创建 Pod 时应用 Pod 安全标准。它是 v1.25 中的 GA 功能。在本教程中,您将一次在一个命名空间中强制执行 baseline Pod 安全标准。
您也可以在集群级别将 Pod 安全标准应用于多个命名空间。有关说明,请参阅 在集群级别应用 Pod 安全标准。
开始之前
在您的工作站上安装以下内容
创建集群
按照以下步骤创建
kind集群kind create cluster --name psa-ns-level输出类似于以下内容
Creating cluster "psa-ns-level" ... ✓ Ensuring node image (kindest/node:v1.31.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/将 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 安全标准检查
使用内置 Pod 安全准入支持的标签,在该命名空间上启用 Pod 安全标准。在此步骤中,您将配置一个检查,以警告不符合最新版本的 baseline Pod 安全标准的 Pod。
kubectl label --overwrite ns example \ pod-security.kubernetes.io/warn=baseline \ pod-security.kubernetes.io/warn-version=latest您可以使用标签在任何命名空间上配置多个 Pod 安全标准检查。以下命令将
强制执行baselinePod 安全标准,但根据最新版本(默认值)警告和审计restrictedPod 安全标准。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 安全标准执行
在
example命名空间中创建一个基线 Podkubectl apply -n example -f https://k8s.io/examples/security/example-baseline-pod.yamlPod 正常启动;输出包含一个警告。例如
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在
default命名空间中创建一个基线 Podkubectl 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
下一步
运行一个 shell 脚本 以一次性执行所有上述步骤。
- 创建 kind 集群
- 创建新命名空间
- 在
enforce模式下应用baselinePod 安全标准,同时在warn和audit模式下应用restrictedPod 安全标准。 - 创建一个具有以下 Pod 安全标准的新 Pod
最后修改时间:2023 年 11 月 8 日太平洋时间晚上 10:12:修复 en 文档中的 KinD 错别字。 (ad52e828b5)