Kubernetes SecurityContext is a Kubernetes setting that defines security-related runtime options for a Pod or container. In practical terms, it controls whether a workload runs as root, which Linux capabilities it gets, whether it can escalate privileges, which user ID it uses, and whether its filesystem should be read-only.
A SecurityContext lets you reduce the privileges available to a workload at runtime. Platform and security teams use it to make containers safer by default, especially in shared clusters or production namespaces.
Common controls include:
runAsUser, runAsGroup, and fsGroup.NET_RAW or all capabilities by default.allowPrivilegeEscalation: false.You can define securityContext at two main levels:
fsGroup, runAsUser, and runAsNonRoot.allowPrivilegeEscalation, capabilities, privileged, and readOnlyRootFilesystem.Container-level settings can override Pod-level settings where both are supported. This matters for Pods with multiple containers, such as an app container plus a sidecar, where each container may need different permissions.
This example runs a container as a non-root user, blocks privilege escalation, drops Linux capabilities, and makes the root filesystem read-only:
apiVersion: v1
kind: Pod
metadata:
name: secure-app
spec:
securityContext:
runAsNonRoot: true
runAsUser: 10001
runAsGroup: 10001
fsGroup: 10001
containers:
- name: app
image: example/app:1.0.0
ports:
- containerPort: 8080
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL
seccompProfile:
type: RuntimeDefault
In a real service, this often means the application must listen on a non-privileged port such as 8080 instead of 80, and write temporary files to a mounted volume instead of the container root filesystem.
If your team manages Kubernetes manifests through infrastructure as code, SecurityContext settings should live beside the workload definition. For example, they can be included when you deploy Kubernetes resources using Terraform.
runAsNonRootRequires the container to run as a non-root user. This is one of the most common baseline settings for application workloads.
runAsUser and runAsGroupSet the Linux user ID and group ID for the container process. Use explicit numeric IDs where possible, such as 10001, instead of relying only on image defaults.
fsGroupSets a group ID for mounted volumes so the workload can read or write files as needed. This is useful for persistent volumes, but it can slow startup on large volumes because Kubernetes may need to adjust file ownership.
allowPrivilegeEscalationControls whether a process can gain more privileges than its parent process. For most application containers, set it to false.
privilegedGives the container broad access to host devices and kernel features. Avoid it for application workloads. It is usually limited to low-level infrastructure components such as certain CNI, CSI, or node agents.
capabilitiesAdds or drops Linux capabilities. A common secure default is to drop all capabilities and add back only what the container truly needs.
readOnlyRootFilesystemMakes the container’s root filesystem read-only. This helps prevent unexpected writes, but your application may need writable mounts for paths like /tmp, cache directories, or log buffers.
seccompProfileApplies a seccomp profile to restrict system calls. RuntimeDefault is a practical default for many workloads and uses the container runtime’s default seccomp profile.
SecurityContext defines settings on an individual Pod or container. Pod Security Admission checks whether a Pod meets a security policy before Kubernetes accepts it.
For example, a namespace using the restricted Pod Security Standard may reject a Pod that runs as privileged or allows privilege escalation. The SecurityContext is where you set the values that help the Pod pass those checks.
SecurityContext controls runtime privileges inside the container and at the node operating system level. RBAC controls what Kubernetes API actions an identity can perform, such as reading Secrets or creating Deployments.
You usually need both. A Pod can run as non-root and still have an over-permissive ServiceAccount. A Pod can also have minimal RBAC but still be risky if it runs as privileged.
runAsNonRoot: true for normal application workloads.10001.allowPrivilegeEscalation: false unless the workload has a clear need.readOnlyRootFilesystem: true and mount writable paths where needed.seccompProfile.type: RuntimeDefault as a sensible default.SecurityContext settings should also be reviewed during cluster maintenance. Kubernetes version changes, admission policy changes, and runtime updates can affect workload behavior, so include security settings in your upgrade checklist. This is especially useful when planning Kubernetes upgrades for startups where small teams own both delivery speed and production risk.
Suppose your team runs a web API on EKS. The container image originally runs as root, writes logs to /var/log/app, and listens on port 80. To harden it, you might:
8080.10001.emptyDir volume for temporary files if needed.The same approach applies to scheduled jobs, internal services, and stateful tools. For example, when you deploy Apache Airflow on AWS Elastic Kubernetes Service, SecurityContext settings can help separate what the scheduler, webserver, and workers are allowed to do at runtime.
Kubernetes SecurityContext is one of the main tools for applying least-privilege runtime settings to Pods and containers. It is simple to define in YAML, but it works best when your container images, volume mounts, application ports, and admission policies are designed with those restrictions in mind.