Skip to main content
When you run a Docker container, you can set security-related options such as the UID the process runs as or Linux capabilities to add/remove. Kubernetes provides equivalent controls via the securityContext field. Containers run inside Pods, and security settings can be defined either at the Pod level or at the container level. Pod-level settings apply to all containers in the Pod; container-level settings override Pod-level values for the same fields.
A dark slide titled "Kubernetes Security" showing a circular diagram with user icons and two labeled buttons/boxes both reading "MAC_ADMIN" (one orange, one blue).
Container-level securityContext values take precedence over Pod-level values for the same fields.
Below are concise, practical examples and a quick reference for common securityContext fields.

Quick field reference

For full details, see the Kubernetes docs on security contexts: https://kubernetes.io/docs/tasks/configure-pod-container/security-context/

Examples

  1. Pod-level securityContext (applies to all containers in the Pod)
  1. Container-level securityContext (overrides Pod-level runAsUser)
  1. Adding Linux capabilities to a container

Best practices

  • Always use numeric UIDs/GIDs for runAsUser, runAsGroup, and fsGroup in manifests.
  • Prefer runAsNonRoot: true whenever your application is compatible — this prevents accidental UID 0 execution.
  • Apply the least-privilege principle: only add capabilities that your workload truly requires and explicitly drop capabilities you do not need.
  • Capabilities are applied per container; set them at the container level to avoid granting unnecessary privileges to other containers in the same Pod.
  • Enforce cluster-wide security posture using PodSecurity admission, Gatekeeper, or another policy controller—these provide consistent rules across your cluster. See Pod Security Standards: https://kubernetes.io/docs/concepts/security/pod-security-standards/
runAsNonRoot: true will fail pod creation if the container image has no declared non-root user and the process would run as UID 0. Ensure your image defines a non-root user or set runAsUser explicitly.

Viewing and troubleshooting

  • Inspect the resolved security context of a running Pod (full manifest):
  • See a human-readable summary and events:
  • If a Pod fails to start due to security validation, check the Pod events and admission controller logs (if configured).
You can practice viewing and adjusting security contexts in a local cluster (e.g., kind, minikube) or a sandboxed cloud cluster. That’s it for now.

Watch Video

Practice Lab