Kubernetes securityContext - run as non-root, drop capabilities, read-only root filesystem
Answer: Include the securityContext field in the Pod specification for settings that apply to all Containers, and in a Container manifest for settings that apply only to that container. A security context defines privilege and access control settings for a Pod or Container.
Example
apiVersion: v1
kind: Pod
metadata:
name: security-context-demo
spec:
securityContext:
runAsUser: 1000
runAsGroup: 3000
fsGroup: 2000
seccompProfile:
type: RuntimeDefault
volumes:
- name: sec-ctx-vol
emptyDir: {}
containers:
- name: sec-ctx-demo
image: busybox:1.28
command: ["sh", "-c", "sleep 1h"]
volumeMounts:
- name: sec-ctx-vol
mountPath: /data/demo
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]
add: ["NET_BIND_SERVICE"]
Pod-level fields
runAsUser: 1000makes all processes in every container run with user ID 1000.runAsGroupsets the primary group ID; if omitted, the primary group of the containers will be root(0).- With
fsGroup: 2000, processes are also part of supplementary group ID 2000, which owns the volume and files created in it.
Container-level fields
- Container settings apply only to that container and override Pod-level settings when there is overlap. They do not affect the Pod's Volumes.
allowPrivilegeEscalationcontrols whether a process can gain more privileges than its parent, for example through a setuid binary. If not defined, it defaults to true.allowPrivilegeEscalation: falsecannot be combined with a privileged container or one withCAP_SYS_ADMIN.readOnlyRootFilesystemmounts the container's root filesystem as read-only.capabilitiesadd or drop Linux capabilities, granting certain privileges without all the privileges of the root user.
seccomp
seccompProfile.type can be RuntimeDefault, Unconfined, or Localhost; localhostProfile is only set with Localhost and is a path relative to the kubelet's seccomp directory.
See also
Claims
- id: C1 claim: allowPrivilegeEscalation defaults to true when not defined. source: S2 evidence: "then it will default to true."
- id: C2 claim: Container security settings override Pod-level settings. source: S2 evidence: "they override settings made at the Pod level when there is overlap."
- id: C3 claim: readOnlyRootFilesystem mounts the root filesystem read-only. source: S1 evidence: "Mounts the container's root filesystem as read-only."
Sources
- [S1] Configure a Security Context for a Pod or Container. Kubernetes docs. https://kubernetes.io/docs/tasks/configure-pod-container/security-context/ . License: CC-BY-4.0. Accessed 2026-11-18.
- [S2] security-context.md (source page). kubernetes/website (GitHub). https://github.com/kubernetes/website/blob/main/content/en/docs/tasks/configure-pod-container/security-context.md . License: CC-BY-4.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Kubernetes docs, Configure a Security Context for a Pod or Container; kubernetes/website (GitHub), security-context.md (source page). CC-BY-4.0 (https://raw.githubusercontent.com/kubernetes/website/main/LICENSE). Copyright The Kubernetes Authors. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Kubernetes docs, Configure a Security Context for a Pod or Container; kubernetes/website (GitHub), security-context.md (source page). CC-BY-4.0 (https://raw.githubusercontent.com/kubernetes/website/main/LICENSE). Copyright The Kubernetes Authors.
Cite this
iinventedlight. 2026. "Kubernetes securityContext - run as non-root, drop capabilities, read-only root filesystem." iinventedlight Library, iil:note:6ncacjv4, version of 2026-11-18. https://iinventedlight.com/library/notes/kubernetes-security-context/. License: CC-BY-4.0.
@misc{iil_note_6ncacjv4,
author = {{iinventedlight}},
title = {{Kubernetes securityContext - run as non-root, drop capabilities, read-only root filesystem}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/kubernetes-security-context/},
note = {Permanent ID iil:note:6ncacjv4. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- kubernetes-security-context.table.json (table; CC-BY-4.0)
- kubernetes-security-context.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/kubernetes-security-context.md.