Handbooks / Kubernetes / Chapter 9
Pod Security and Observability
6 pages · ~4 min✓ Reviewed
Builds on NetworkPolicies. Next up: Helm and Kustomize.
securityContext: Hardening a Pod
1. What securityContext Is
- securityContext sets privilege and access controls
- Pod-level
spec.securityContextapplies to all containers - Container-level setting overrides the pod-level one
2. Users and Filesystem
| Field | Effect | Level |
|---|---|---|
runAsNonRoot | kubelet refuses a UID 0 image | either |
runAsUser / runAsGroup | set the UID / GID | either |
fsGroup | group owner of mounted volumes | pod only |
readOnlyRootFilesystem | blocks writes to container FS | container |
runAsNonRoot: trueonly checks, it never changes the user- Root image fails:
CreateContainerConfigError - To pass: set
runAsUserto a non-zero UID or use a non-rootUSERin the image - Read-only root: mount an
emptyDirat/tmpfor write paths
3. Lock Down the Container
yaml
securityContext: runAsNonRoot: true runAsUser: 1000 readOnlyRootFilesystem: true allowPrivilegeEscalation: false capabilities: drop: ["ALL"] add: ["NET_BIND_SERVICE"]
drop ALL first, add back only what is needed