INFRA-DEVOPS Contents

Kubernetes SecurityContext Patterns

Use SecurityContext patterns that actually work in production: non-root, FSGroup, drop caps, and safe defaults.

On this page

SecurityContext Production Defaults

  • Run as non-root; set explicit UID/GID when possible.
  • Drop capabilities; disable privilege escalation.
  • Use read-only rootfs; mount writable paths explicitly.

Pod Spec Example

securityContext:
  runAsNonRoot: true
  runAsUser: 10001
  runAsGroup: 10001
  fsGroup: 10001

containers:
- name: api
  securityContext:
    allowPrivilegeEscalation: false
    readOnlyRootFilesystem: true
    capabilities:
      drop: ["ALL"]
  volumeMounts:
  - name: tmp
    mountPath: /tmp

Troubleshooting: Permission Errors

# If you see "permission denied" on mounted volumes:
1) verify fsGroup matches volume permissions
2) check initContainers for chmod/chown
3) confirm the image does not require root-owned paths

Failure Modes

  • UID mismatch with persistent volumes: app can't write (fix fsGroup/permissions).
  • Read-only rootfs without /tmp mount: many runtimes fail.