Pod Security Admission — enforce pod security standards at namespace level.
1# Namespace labels for Pod Security Standards2apiVersion: 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# Compliant Pod15apiVersion: v116kind: Pod17metadata:18 name: compliant-pod19 namespace: production20spec:21 securityContext:22 runAsNonRoot: true23 runAsUser: 100024 runAsGroup: 100025 fsGroup: 100026 seccompProfile:27 type: RuntimeDefault28 containers:29 - name: app30 image: nginx:latest31 securityContext:32 allowPrivilegeEscalation: false33 capabilities:34 drop:35 - ALL36 readOnlyRootFilesystem: true37 volumeMounts:38 - name: tmp39 mountPath: /tmp40 - name: cache41 mountPath: /var/cache/nginx42 volumes:43 - name: tmp44 emptyDir: {}45 - name: cache46 emptyDir: {}
1# Check namespace enforcement2kubectl get namespace production -o yaml | grep pod-security34# Test policy (will be rejected)5kubectl run test --image=nginx --namespace=production --privileged67# View warnings8kubectl apply -f pod.yaml 2>&1 | grep Warning910# Bypass for system namespaces11kubectl label namespace kube-system pod-security.kubernetes.io/enforce=privileged
Standards:
Modes: