Troubleshooting — debug Pod security policy violations.
1# Namespace with restricted policy2apiVersion: v13kind: Namespace4metadata:5 name: production6 labels:7 pod-security.kubernetes.io/enforce: restricted8 pod-security.kubernetes.io/enforce-version: latest9---10# Non-compliant Pod11apiVersion: v112kind: Pod13metadata:14 name: troubleshoot-pod15 namespace: production16spec:17 containers:18 - name: app19 image: nginx:latest20 # Missing security context
1# Test compliance2kubectl apply -f troubleshoot-pod.yaml 2>&134# View detailed error5kubectl apply -f troubleshoot-pod.yaml 2>&1 | grep -A 5 "Error"67# Check namespace policy8kubectl get namespace production -o yaml | grep pod-security910# View audit events11kubectl get events -n production --sort-by=.lastTimestamp | grep -i "audit\|warn\|error"1213# Check for violations14kubectl get events -n production --field-selector reason=FailedCreate --sort-by=.lastTimestamp1516# Test with dry-run17kubectl apply -f troubleshoot-pod.yaml --dry-run=server 2>&11819# Check Pod annotations20kubectl get pod troubleshoot-pod -o jsonpath='{.metadata.annotations}' 2>/dev/null | jq2122# Verify security context23kubectl get pod troubleshoot-pod -o jsonpath='{.spec.securityContext}' 2>/dev/null | jq
Common Issues:
Debug Steps: