Solution:
Root Cause: Persistent Volume Mount Permission Denied (EACCES)
When a container runs as a non-root user (configured via
securityContext.runAsUser), but mounted Persistent Volumes (PVCs) belong to
root:root, the application crashes upon attempting to write log files or database locks to the volume.
Diagnostic Verification:
1.Check previous container logs for permission errors:
bash
kubectl logs <pod_name> --previous -n <namespace> | grep -i 'permission denied'
2.Inspect security context in the Pod manifest:
bash
kubectl get pod <pod_name> -n <namespace> -o yaml | grep -A 10 securityContext
Step-by-Step Fix:
1.Configure Pod-Level fsGroup:
Set fsGroup in the Pod's securityContext to automatically recursively change volume group ownership on mount: yaml
spec:
securityContext:
runAsUser: 10001
runAsGroup: 10001
fsGroup: 10001
2.Use InitContainers to Chown Volume (Alternative):
If fsGroup is unsupported by the storage provider, use an initContainer running as root: yaml
initContainers:
- name: fix-permissions
image: busybox:latest
command: ["sh", "-c", "chown -R 10001:10001 /data"]
volumeMounts:
- name: storage-volume
mountPath: /data
3.Apply Updated Manifest:
Run kubectl apply -f deployment.yaml -n <namespace>.Prevention & Long-Term Monitoring:
Test non-root container deployment security policies in lower environments prior to cluster migration.