在命名空間層級套用 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.32.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.sigs.k8s.io/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 安全標準檢查。以下命令將
強制執行
baseline
Pod 安全標準,但針對最新版本(預設值)的restricted
Pod 安全標準警告
和稽核
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
命名空間中建立 baseline Podkubectl 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
在
default
命名空間中建立 baseline 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 叢集
- 建立新命名空間
- 套用
baseline
Pod 安全標準(在enforce
模式中),同時也套用restricted
Pod 安全標準(在warn
和audit
模式中)。 - 建立套用以下 Pod 安全標準的新 Pod
上次修改時間為 2023 年 11 月 08 日下午 10:12 PST:修正英文文件中的 KinD 錯字。(ad52e828b5)