Testing — validate Pod compliance with security standards.
1# Test Pod (restricted compliant)2apiVersion: v13kind: Pod4metadata:5 name: test-restricted6 namespace: test-ns7spec:8 securityContext:9 runAsNonRoot: true10 runAsUser: 100011 seccompProfile:12 type: RuntimeDefault13 containers:14 - name: app15 image: nginx:latest16 securityContext:17 allowPrivilegeEscalation: false18 capabilities:19 drop:20 - ALL21 volumeMounts:22 - name: tmp23 mountPath: /tmp24 volumes:25 - name: tmp26 emptyDir: {}27---28# Non-compliant Pod (baseline)29apiVersion: v130kind: Pod31metadata:32 name: test-baseline33 namespace: test-ns34spec:35 hostNetwork: true # Violates baseline36 containers:37 - name: app38 image: nginx:latest
1# Test restricted compliance2kubectl create namespace test-ns3kubectl label namespace test-ns pod-security.kubernetes.io/enforce=restricted4kubectl apply -f test-restricted.yaml # Should succeed5kubectl apply -f test-baseline.yaml 2>&1 # Should fail with Warning67# View violations8kubectl apply -f test-baseline.yaml 2>&1 | grep Warning910# Check audit events11kubectl get events -n test-ns --sort-by=.lastTimestamp1213# Verify Pod security14kubectl get pod test-restricted -o jsonpath='{.metadata.annotations}' | jq1516# Clean up17kubectl delete namespace test-ns
Testing Approaches:
Best Practices: