Sub topic 10
Applying the same controls in Kubernetes
This chapter assumes you have read the earlier chapters on capabilities, seccomp, and read-only filesystems. You should also be able to read a basic Pod manifest. Here you translate the Docker flags you already know into securityContext fields.
Docker flags and their Kubernetes equivalents
Kubernetes has no single "hardening" switch. Each control you used with docker run maps to a field in the securityContext section.
| Docker option | Kubernetes field | Level |
|---|---|---|
| `--cap-drop ALL` | `capabilities.drop: ["ALL"]` | container |
| `--cap-add NET_BIND_SERVICE` | `capabilities.add: ["NET_BIND_SERVICE"]` | container |
| `--security-opt no-new-privileges:true` | `allowPrivilegeEscalation: false` | container |
| `--user 10001:10001` | `runAsUser`, `runAsGroup`, `runAsNonRoot: true` | pod or container |
| `--read-only` | `readOnlyRootFilesystem: true` | container |
| `--tmpfs /tmp` | an `emptyDir` volume mounted at `/tmp` | pod volume |
| `--security-opt seccomp=...` | `seccompProfile` | pod or container |
Settings at the pod level apply to every container. A container-level setting overrides the pod-level one. Capabilities, privilege escalation, and the read-only root filesystem exist only at the container level.
A hardened Pod
This manifest combines all of the controls.
apiVersion: v1
kind: Pod
metadata:
name: hardened-app
spec:
securityContext:
runAsNonRoot: true
runAsUser: 10001
seccompProfile:
type: RuntimeDefault
containers:
- name: app
image: python:3.12-slim
command: ["sleep", "3600"]
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]
volumeMounts:
- name: tmp
mountPath: /tmp
volumes:
- name: tmp
emptyDir: {}The pod-level block sets the user and the seccomp profile for the whole pod. The container-level block drops every capability, blocks privilege gain, and makes the root filesystem read-only. The emptyDir volume mounted at /tmp is the only writable path.
Differences from Docker
Docker applies its default seccomp profile automatically. Kubernetes does not, unless the kubelet runs with the SeccompDefault option. Without a seccompProfile, a pod may run with no syscall filtering at all. Set type: RuntimeDefault explicitly.
An emptyDir volume is backed by node disk by default. To get tmpfs behavior like --tmpfs, set medium: Memory and a size limit:
volumes:
- name: tmp
emptyDir:
medium: Memory
sizeLimit: 64MiMemory-backed volumes count against the container's memory limit, so size them with that in mind.
Using a custom seccomp profile
To use a custom profile such as deny-mkdir.json, set the type to Localhost:
seccompProfile:
type: Localhost
localhostProfile: profiles/deny-mkdir.jsonThe path is relative to the kubelet's seccomp directory, normally /var/lib/kubelet/seccomp. The file must exist on every node where the pod might be scheduled. If it is missing, the pod fails to start.
Verifying from outside and inside
Apply the manifest, then inspect the running container:
kubectl apply -f hardened-app.yaml
kubectl exec hardened-app -- grep -E 'Cap|NoNewPrivs|Seccomp' /proc/self/status
kubectl exec hardened-app -- touch /etc/blocked
kubectl exec hardened-app -- touch /tmp/okExpect CapEff to be all zeros, NoNewPrivs: 1, and Seccomp: 2 (filter mode). The write to /etc fails with a read-only filesystem error. The write to /tmp succeeds.
Exercise
A Pod sets readOnlyRootFilesystem: true and capabilities.drop: ["ALL"], but the app crashes because it cannot write cache files to /var/cache/app. What is the least permissive fix?
Show answer
Mount a volume at /var/cache/app, for example an emptyDir with a sizeLimit. Keep the root filesystem read-only and keep all capabilities dropped. Only that one path becomes writable.
Exercise
You set no seccompProfile on a pod and assume it gets the same filtering as docker run. Why might this be wrong, and what is the fix?
Show answer
Kubernetes does not apply the runtime default profile unless the kubelet enables SeccompDefault. Without it, the container can run unconfined. Add seccompProfile: {type: RuntimeDefault} to the pod or container securityContext.