Kubernetes Pod YAML example
A Pod is the smallest thing Kubernetes runs: one or more containers that share a network address and are scheduled together. This manifest is close to the shortest Pod you can write.
You rarely create bare Pods in production, because nothing restarts or replaces them when a node fails. A Deployment does that for you, and its Pod template looks exactly like the spec below.
What this manifest does
This manifest defines 1 resource in the default namespace: the main thing it runs is 1 Pod. Let’s walk through it.
Pod hello is a single Pod, running nginxdemos/hello:0.3.
Things to know before using it
No resource limitsPod hello
Container "web" has no resources.limits. One Pod can starve the whole node of CPU or memory. Fix: Set resources.limits.cpu and resources.limits.memory.
No resource requestsPod hello
Container "web" has no resources.requests. The scheduler can't pack it correctly. Fix: Set resources.requests.cpu and resources.requests.memory.
No livenessProbePod hello
Container "web" has no livenessProbe. Kubernetes can't detect and restart a hung container. Fix: Add a livenessProbe with httpGet, tcpSocket or exec.
No readinessProbePod hello
Container "web" has no readinessProbe. Pods may receive traffic before they're ready. Fix: Add a readinessProbe so Services route traffic only when the Pod is healthy.
Container may run as rootPod hello
Container "web" doesn't enforce runAsNonRoot. A compromise gives the attacker root inside the container. Fix: Set securityContext.runAsNonRoot: true and runAsUser to a non-zero UID.
allowPrivilegeEscalation not setPod hello
Container "web" doesn't explicitly disable privilege escalation. Defaults to true on older kubelets. Fix: Set securityContext.allowPrivilegeEscalation: false.
Writable root filesystemPod hello
Container "web" doesn't set readOnlyRootFilesystem. Attackers can write tools into / at runtime. Fix: Set securityContext.readOnlyRootFilesystem: true and use volumeMounts for paths that need write access (/tmp, /var/run, ...).
Linux capabilities not droppedPod hello
Container "web" doesn't drop ALL capabilities. By default it runs with ~14 caps including NET_RAW, MKNOD. Fix: Set securityContext.capabilities.drop: ["ALL"] and add only what you need under capabilities.add.
The full manifest
# Step 1: the smallest unit of work in Kubernetes — a single Pod.
apiVersion: v1
kind: Pod
metadata:
name: hello
labels:
app: hello
spec:
containers:
- name: web
image: nginxdemos/hello:0.3
ports:
- containerPort: 80
Related examples
Stuck on a Kubernetes concept?
Ask anything and get a narrated visual lesson, pitched at your level. Free.
Try an example