Sub topic 4
Reading and writing securityContext
This chapter assumes you can read Pod YAML and know what a Linux user ID and a capability are. It also assumes you know the Baseline and Restricted profiles from earlier chapters.
What securityContext is
securityContext is a block in a Pod spec that sets Linux security options for the Pod's processes. Pod Security Admission does not read a policy file. It inspects these fields and compares them with the profile. If you can read securityContext, you can predict whether a Pod will be admitted.
The block appears at two levels:
spec.securityContextis the pod-level context. It applies to every container in the Pod.spec.containers[].securityContextis the container-level context. It applies to one container and overrides the pod-level value when both set the same field.
Init containers and ephemeral containers have their own securityContext too. The Restricted profile checks them as well.
Which fields live where
Some fields exist at only one level, so placing them correctly matters.
| Field | Pod level | Container level | Restricted profile |
|---|---|---|---|
| `runAsNonRoot` | yes | yes | must be `true` |
| `runAsUser` | yes | yes | must not be 0 |
| `seccompProfile` | yes | yes | `RuntimeDefault` or `Localhost` |
| `allowPrivilegeEscalation` | no | yes | must be `false` |
| `capabilities` | no | yes | must drop `ALL` |
| `privileged` | no | yes | must not be `true` |
| `fsGroup` | yes | no | not checked |
| `readOnlyRootFilesystem` | no | yes | not checked |
readOnlyRootFilesystem is good practice, but the Restricted profile does not require it.
Writing a compliant Pod
Put the settings shared by all containers at pod level. Put container-only settings on each container. This Pod sets a pod-wide user and seccomp profile, then tightens one container.
apiVersion: v1
kind: Pod
metadata:
name: layered-demo
namespace: payments
spec:
securityContext:
runAsNonRoot: true
runAsUser: 1000
runAsGroup: 3000
fsGroup: 2000
seccompProfile:
type: RuntimeDefault
containers:
- name: app
image: busybox:1.36
command: ["sleep", "3600"]
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]Here app inherits the user and seccomp profile from the pod level. It adds the container-only fields itself. If the Pod had a second container, it would need its own allowPrivilegeEscalation: false and capabilities.drop, because those cannot be set at pod level.
Reading the result
Apply a Pod and read its effective settings:
kubectl apply -f layered-demo.yaml
kubectl get pod layered-demo -n payments \
-o jsonpath='{.spec.containers[0].securityContext}'The output shows only the container-level block. Pod-level values are under .spec.securityContext. To check the user, run kubectl exec layered-demo -n payments -- id. You should see uid=1000 gid=3000.
Common errors
- Forbidden at admission. A namespace enforcing Restricted rejects a Pod that misses a field. The message lists each violation, for example
allowPrivilegeEscalation != falseandunrestricted capabilities. Fix every field it lists. - CreateContainerConfigError. This is a kubelet error, not an admission error. With
runAsNonRoot: true, the image must run as a non-root user. If the image'sUSERis root and you set norunAsUser, the kubelet refuses to start the container. SetrunAsUseror use an image that declares a non-root user. - Dropped capabilities break the app. An app binding to a port below 1024 may need
NET_BIND_SERVICE. Restricted allows adding back only that one capability. Alternatively, listen on a higher port. - Settings on the wrong level. Putting
capabilitiesunderspec.securityContextfails schema validation, because the field does not exist there.
Exercise
A Pod sets runAsNonRoot: true and seccompProfile: RuntimeDefault at pod level. Its only container sets capabilities.drop: ["ALL"] and nothing else. Will a namespace enforcing Restricted admit it, and why?
Show answer
No. The container does not set allowPrivilegeEscalation: false, and that field exists only at container level, so nothing at pod level can supply it. Restricted requires it to be false. Adding it to the container's securityContext fixes the Pod.