Security contexts and Pod Security
Constrain process privileges and use admission policy to keep unsafe Pod settings out of a namespace.
What you will understand
- Explain UID, capabilities, seccomp and privilege escalation settings
- Handle read-only filesystems and volume ownership deliberately
- Separate Pod Security Admission enforcement from runtime controls
Builds on: Authentication, RBAC, and ServiceAccounts
Mental model: authorization to create a Pod is only the first boundary
A user who can create Pods can ask a node to execute processes. The Pod’s security context determines important parts of the resulting privilege boundary: user identity, Linux capabilities, privilege escalation, syscall filtering and filesystem access. Running as a non-root UID reduces exposure, but UID alone is not a complete sandbox. Host mounts, privileged mode, host namespaces and powerful capabilities can expose the host even when an image is described as a container.
Security settings exist at Pod and container levels, with particular fields applying at one or both levels. A Pod-level runAsUser provides a default for eligible containers; a container can override fields where the API permits it. runAsNonRoot asks Kubernetes to prevent root execution according to its checks. allowPrivilegeEscalation: false restricts gaining additional privileges through mechanisms such as setuid executables on Linux. Dropping capabilities removes selected kernel powers, while seccomp filters the syscall surface. RuntimeDefault selects the runtime’s default profile, not a universal identical syscall list on every runtime.
Constrain the process without hiding its required writes
Connect UID, capability, seccomp and filesystem choices to a non-root writable-path example.
The process runs as UID 1000. runAsNonRoot checks that it is not configured to execute as root.
Inspect this state in YAML / kubectl
runAsNonRoot: true
runAsUser: 1000Read the complete visual sequence
- Choose process identity
The process runs as UID 1000. runAsNonRoot checks that it is not configured to execute as root.
- Reduce kernel powers
Dropping capabilities and disabling privilege escalation reduce privilege paths independently of the UID choice.
- Protect the image filesystem
The root filesystem is read-only, so an application writing there should receive an error rather than mutate its executable environment.
- Allow a deliberate writable path
A separately mounted emptyDir provides /work with suitable group permissions; read-only root does not override this mount.
Application writes to the wrong path
The process tries to write to its read-only root filesystem.
- Pod is admitted
The security settings are valid and the application process can start.
- Write fails at runtime
Writing outside /work fails because that path belongs to the read-only root filesystem.
- Fix application paths
Configure temporary and state paths or add a narrowly scoped writable mount; do not enable privileged mode to bypass a filesystem design mistake.
About this model
Linux node with RuntimeDefault seccomp and an emptyDir volume; not a complete hostile-code sandbox.
Walkthrough: supply the writable paths the application actually needs
This complete Pod uses a non-root training process and a read-only root filesystem, with a deliberate writable volume at /work. It demonstrates mechanics rather than claiming that arbitrary production software works without additional image or permission changes.
apiVersion: v1
kind: Pod
metadata:
name: restricted-writer
spec:
securityContext:
runAsNonRoot: true
runAsUser: 1000
runAsGroup: 1000
fsGroup: 1000
seccompProfile:
type: RuntimeDefault
containers:
- name: writer
image: busybox:1.36
command: ["sh", "-c", "id; echo ready > /work/status; sleep 3600"]
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: [ALL]
volumeMounts:
- name: work
mountPath: /work
volumes:
- name: work
emptyDir: {}
readOnlyRootFilesystem prevents writes to the container root filesystem; it does not make separately mounted volumes read-only automatically. Give applications explicit writable paths for temporary files or state. fsGroup can help make supported mounted volumes accessible to the process group, but ownership handling depends on the volume type and CSI behavior. An existing backend with incompatible ownership may still fail. Test actual write permissions instead of solving every error by enabling root or making all directories world-writable.
Reject an unsafe Pod before node execution
Trace namespace policy labels through PSA evaluation and distinguish warning from enforcement.
Namespace labels choose the Restricted standard and version for enforcement; the labels themselves require controlled ownership.
- Namespace to PSApolicy selection
Read the complete visual sequence
- Select a policy version
Namespace labels choose the Restricted standard and version for enforcement; the labels themselves require controlled ownership.
- Evaluate the proposal
The privileged container violates Restricted requirements. Authentication and RBAC can succeed before this separate admission rejection.
- Reject before persistence
Enforcement rejects the violating Pod, so this Pod never becomes a node execution request.
- Admit a compliant configuration
After using compliant fields, a new proposal can persist and reach the node. Runtime permissions still need actual testing.
Only warn mode is configured
The policy emits warnings but does not reject the unsafe proposal through that mode.
- Warning policy selected
Warn is feedback about violations rather than the enforcement action.
- Violation is reported
The API response warns about privileged settings, but warn alone does not reject the object.
- Unsafe Pod may persist
Without an enforcing rule or another rejecting admission control, the Pod can reach the node despite the warning.
About this model
Pod Security Admission is enabled with no exemption for the training namespace or identity.
Practice: reject an unsafe proposal before it reaches a node
Pod Security Standards define Privileged, Baseline and Restricted policy levels. Pod Security Admission, or PSA, evaluates Pods against the chosen level according to namespace labels and cluster configuration. Enforcement rejects violating new Pod admissions; warning and audit modes communicate violations without the same rejection behavior. These checks evaluate Pod configuration, not application code quality, image provenance or every tenant risk.
# Use a new disposable namespace with no production workloads.
kubectl create namespace security-practice
kubectl label namespace security-practice \
pod-security.kubernetes.io/enforce=restricted \
pod-security.kubernetes.io/enforce-version=v1.37
kubectl apply -n security-practice -f restricted-writer.yaml
kubectl exec -n security-practice restricted-writer -- cat /work/status
kubectl exec -n security-practice restricted-writer -- id
Expect the compliant Pod to be admitted and the mounted-path write to work on a compatible cluster. Try a separately named copy with privileged: true only in this disposable namespace and expect rejection under Restricted enforcement. For workload controllers, admission may accept the controller object while later Pod creation is rejected; inspect ReplicaSet or Job events for the denied template. A namespace policy change does not retroactively rewrite an already running Pod’s security context.
Pitfalls: admission and runtime protection are complementary
PSA does not install NetworkPolicy, limit RBAC grants or prove the container image lacks vulnerabilities. Namespace labels also need governance: a tenant able to remove enforcement labels may weaken the intended boundary. Pinning the policy version makes upgrades deliberate, but it also means reviewing new restrictions rather than leaving a historical policy forever. Linux security fields and operating-system support vary; verify the target nodes and runtime, especially in mixed-OS clusters.
Check your understanding
A non-root container has a read-only root filesystem but mounts a writable PVC. Can it still modify that PVC?
Yes, if the volume is mounted writable and its permissions allow the process UID or group to write. The read-only root setting applies to the container root filesystem, not every mount. Set mount access and filesystem permissions deliberately, then test the exact path the application uses.
Use the security lab to compare an API rejection with a runtime permission error. Next, protect credentials and combine namespace controls into a realistic tenant boundary.
Primary documentation
Go to the source
Reviewed 2026-10-27 · Kubernetes 1.37 · Related local exercise