Enforcement — reject non-compliant Pods at admission.
1# Enforce restricted policy2apiVersion: v13kind: Namespace4metadata:5 name: production6 labels:7 pod-security.kubernetes.io/enforce: restricted8 pod-security.kubernetes.io/enforce-version: latest9 pod-security.kubernetes.io/audit: restricted10 pod-security.kubernetes.io/audit-version: latest11 pod-security.kubernetes.io/warn: restricted12 pod-security.kubernetes.io/warn-version: latest13---14# Non-compliant Pod (will be rejected)15apiVersion: v116kind: Pod17metadata:18 name: non-compliant19 namespace: production20spec:21 containers:22 - name: app23 image: nginx:latest24 # Missing required security context25---26# Compliant Pod27apiVersion: v128kind: Pod29metadata:30 name: compliant31 namespace: production32spec:33 securityContext:34 runAsNonRoot: true35 runAsUser: 100036 seccompProfile:37 type: RuntimeDefault38 containers:39 - name: app40 image: nginx:latest41 securityContext:42 allowPrivilegeEscalation: false43 capabilities:44 drop:45 - ALL46 volumeMounts:47 - name: tmp48 mountPath: /tmp49 volumes:50 - name: tmp51 emptyDir: {}
1# Check namespace enforcement2kubectl get namespace production -o yaml | grep pod-security34# Test enforcement5kubectl apply -f non-compliant.yaml # Should fail6kubectl apply -f compliant.yaml # Should succeed78# View violations9kubectl apply -f non-compliant.yaml 2>&1 | grep Warning1011# Check audit events12kubectl get events --field-selector reason=FailedCreate --sort-by=.lastTimestamp1314# Bypass for system namespaces15kubectl label namespace kube-system pod-security.kubernetes.io/enforce=privileged
Enforcement Levels:
Modes: