Sub topic 16
Running Pods with a security context
This chapter assumes you can read a Pod manifest, know what a container image is, and understand basic Linux users and file permissions. A security context is a set of fields in the Pod spec that controls which Linux user a container runs as, what it may do, and who owns its mounted files.
Why it matters
By default, many images run as root inside the container. If an attacker escapes the application, root inside the container gives them more room to do damage. A security context applies least privilege: run as an unprivileged user, drop what the process doesn't need, and make the filesystem read-only.
Two levels
You can set a securityContext in two places.
spec.securityContextapplies to every container in the Pod.spec.containers[].securityContextapplies to one container. Where both set the same field, the container value wins.
| Field | Level | Effect |
|---|---|---|
| `runAsUser`, `runAsGroup` | Pod or container | Numeric user and group ID of the process |
| `runAsNonRoot` | Pod or container | Kubernetes refuses to start a container that would run as UID 0 |
| `fsGroup` | Pod only | Group ownership applied to supported volumes |
| `allowPrivilegeEscalation` | Container only | When `false`, a process cannot gain more privileges than its parent |
| `readOnlyRootFilesystem` | Container only | Mounts the container's root filesystem read-only |
| `capabilities.drop` | Container only | Removes Linux capabilities such as `NET_RAW` |
A hardened Pod
apiVersion: v1
kind: Pod
metadata:
name: secure-demo
spec:
securityContext:
runAsUser: 1000
runAsGroup: 3000
runAsNonRoot: true
fsGroup: 2000
containers:
- name: app
image: busybox:1.36
command:
- sh
- -c
- id; touch /data/hello && echo write-ok; sleep 3600
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
emptyDir: {}Apply it and read the logs:
kubectl apply -f secure-demo.yaml
kubectl logs secure-demoThe output shows uid=1000 and gid=3000, with 2000 among the groups, followed by write-ok. The write to /data works because fsGroup gives group 2000 access to the emptyDir volume. A write to any other path, such as /tmp, fails because the root filesystem is read-only.
Common errors
CreateContainerConfigErrorwith "container has runAsNonRoot and image will run as root". The image doesn't declare a non-root user. Set a numericrunAsUser, or use an image built for non-root use.- "Read-only file system" at runtime. The app writes to a path such as
/tmpor/var/cache. Mount anemptyDirvolume at that path. - "Permission denied" on a volume. The process user doesn't match the volume's ownership. Check
fsGroup. - Stock
nginxfails as non-root. It binds port 80 and writes to root-owned directories. Use an unprivileged image variant or a port above 1024.
Exercise
A Pod sets runAsUser: 1000 at the Pod level and runAsUser: 2000 in one container's securityContext. Which user does that container run as, and which user do the other containers run as?
Show answer
The container with its own setting runs as UID 2000, because container-level values override Pod-level values. The other containers run as UID 1000.