SecurityKubernetes
securityContext: the settings every pod should carry
The Kubernetes application security checklist recommends a short list of securityContext settings. Three can be set once for the pod. Three exist only on the container and go on every container.

Six settings and what they do
The application security checklist in the Kubernetes documentation lists the first five under base hardening and the seccomp profile under advanced hardening.
runAsNonRoot: truerequires the container to run as a user other than root. The kubelet validates the image at runtime and fails to start the container if it would run as UID 0.runAsUseris the UID that runs the container's entrypoint. It defaults to the user specified in the image metadata.allowPrivilegeEscalation: falseis the setting that governs whether a process may gain privileges beyond its parent's. Running a setuid binary is one way a process gains them. It decides whether theno_new_privsflag is set on the container process. If the field is not defined, it defaults to true.readOnlyRootFilesystem: truemounts the container's root filesystem as read-only. The default is false.capabilities.drop: ["ALL"]drops all Linux capabilities. With nocapabilitiesfield, a container gets the default set granted by the container runtime. The checklist says to drop all and add back only the specific ones the container needs.seccompProfile.type: RuntimeDefaultapplies the container runtime's default seccomp profile. If no profile is specified, the default isUnconfined, unless seccomp defaulting is enabled on the node with the kubelet flag--seccomp-default.
Pod level and container level
A pod has two places for these fields. spec.securityContext holds pod-level attributes and common container settings. securityContext on a container applies to that container. When a field is set in both, the container's value takes precedence.
- Both levels:
runAsUser,runAsGroup,runAsNonRoot,seccompProfile,seLinuxOptions,appArmorProfile,windowsOptions. - Pod level only:
fsGroup,fsGroupChangePolicy,supplementalGroups,supplementalGroupsPolicy,sysctls,seLinuxChangePolicy. - Container level only:
allowPrivilegeEscalation,readOnlyRootFilesystem,capabilities,privileged,procMount.
Three of the six settings exist only on the container. Set them on every container in the pod, init containers included.
One full example
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
securityContext:
runAsNonRoot: true
runAsUser: 10001
runAsGroup: 10001
seccompProfile:
type: RuntimeDefault
containers:
- name: app
image: registry.example.com/my-app:1.0.0
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]The name, the image and the ID 10001 are choices made for this example. Use the UID and GID that your image is built for.
runAsGroup sets the primary group ID for all processes in the containers. If you omit it, the primary group ID is root (0).
Combinations that do not work
- A root image with
runAsNonRoot: true. The kubelet fails to start the container. SetrunAsUserto a non-zero UID, and configure appropriate permissions on the files and directories inside the image, as the checklist says. allowPrivilegeEscalation: falseon a privileged container. The documentation calls the setting inconsistent with a container that runs as privileged or has the capabilityCAP_SYS_ADMIN. It cannot be set in combination with either.- A seccomp profile on a privileged container. It is not possible to apply one. Privileged containers always run as
Unconfined. - Capability names with the
CAP_prefix. In a manifest you omit that part. To addCAP_SYS_TIME, writeSYS_TIME. - Linux fields on Windows pods. The API reference notes for
runAsUser,allowPrivilegeEscalation,readOnlyRootFilesystem,capabilitiesandseccompProfilethat the field cannot be set whenspec.os.nameiswindows.
How to verify a running pod
The Kubernetes task page checks its example pod, security-context-demo, with these commands:
kubectl exec -it security-context-demo -- id
kubectl exec -it security-context-demo -- sh
kubectl get pod security-context-demo -o yamlThe first prints the UID, the primary group and the supplementary groups of the process. In the shell opened by the second, cd /proc/1 and cat status print the capability bitmaps of process 1 on the CapPrm and CapEff lines. The third prints the pod object, with its status, as YAML. Replace the pod name with your own.
How to enforce the settings
The restricted Pod Security Standard requires four of the six:
allowPrivilegeEscalationmust befalse.runAsNonRootmust betrue, andrunAsUsermust not be 0.- The seccomp profile must be
RuntimeDefaultorLocalhost. - Containers must drop
ALLcapabilities. The only one they may add back isNET_BIND_SERVICE.
Pod Security Admission checks that level for a namespace. With the label pod-security.kubernetes.io/warn: restricted, a violation triggers a user-facing warning and the pod is still allowed. With the enforce label, the pod is rejected.
First steps
- Add the pod-level and the container-level settings to the pod template of each workload.
- Set the
warnlabel forrestrictedon a namespace and read the warnings that the API server returns. - Follow the checklist item on namespaces: deploy the application into a namespace that enforces an appropriate Pod Security Standard.
Sources
Want a second pair of eyes on yours? We do this work for a living.
More guides

Pod Security Standards: baseline, restricted and how to enforce them
Security · 4 min read
Three levels, six namespace labels and a rollout that warns before it blocks.

Kyverno policies: block what should never deploy
Security · 4 min read
Five policy types, one documented example, and what happened to ClusterPolicy in 1.20.

Service account tokens in Kubernetes: bound and short-lived
Security · 4 min read
What changed from Secret-based tokens to bound ones, and how to find the old kind.