Kubernetes Field Guide · Chapter 3 of 16
Pods: the smallest thing you deploy
Everything you run in Kubernetes ends up as pods. Learn what a pod really is, read a manifest line by line, recognise every status kubectl shows you, and fix the failures you will meet in your first week.
- 35 min
- Beginner
- Hands-on
- Free
By Joshua Sequeira, founding engineer running Kubernetes in production. Updated .
By the end you can
0 of 5
Live lab in this chapter
A real-feeling terminal and a live cluster map, right in your browser. Every command block below has a ▶ Run in lab button.
What a pod is, and why not just a container
A pod is the smallest thing Kubernetes schedules: one or more containers that always run together on the same node. Containers in a pod share:
- One network identity. A single IP address, so they reach each other on
localhostand cannot both listen on the same port. - Volumes. Any volume in the pod can be mounted by each container.
- A lifecycle. They are scheduled, started and removed as a unit.
Most pods hold exactly one container. The pod wrapper exists for the cases where a helper must sit right next to your app, which you will see below.
Pods are also disposable. When one dies it is not repaired; a controller makes a new one with a new name and a new IP. That is why you will run pods through Deployments from the next chapter on. But every Deployment, Job and StatefulSet produces pods, so this is where debugging starts.
Reading a pod manifest
Every Kubernetes object has the same four top-level parts: apiVersion, kind, metadata and spec. You write the spec; the cluster fills in status.
apiVersion: v1
kind: Pod
metadata:
name: web
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.27
ports:
- containerPort: 80
resources:
requests:
cpu: 100m
memory: 64Mi
limits:
memory: 128Mi- Which API and object type. Pods are in the core v1 API.
- Key-value tags. Services and Deployments find pods by label, not by name.
- Always pin a tag. latest changes under you and makes rollbacks guesswork.
- Documentation for humans and tools. It does not open or block anything.
- What the scheduler reserves on a node: 100m is a tenth of a CPU core.
- Above 128Mi the container is killed (OOMKilled). Memory limits are a hard ceiling.
Pod lifecycle and the statuses you will see
The STATUS column in kubectl get pods mixes two things: the pod's phase (Pending, Running, Succeeded, Failed) and, when something is wrong, the reason its container is waiting or terminated. Replay the five situations you will meet most often:
Interactive
Pod lifecycle: what kubectl shows you
Pending
Accepted, not yet on a node or not started
Waiting
On a node, container not running yet
Running
Container is running
Terminated
Container exited
What is happening
What to run next
The pattern behind all of them:
| You see | It means | Run |
|---|---|---|
| Pending | No node yet, or not started | kubectl describe pod, read Events |
| ErrImagePull, ImagePullBackOff | Image name, tag or registry access | kubectl describe pod |
| CrashLoopBackOff | The app starts and exits, again and again | kubectl logs --previous |
| OOMKilled | Used more memory than its limit | kubectl describe pod, check Last State |
| Running but 0/1 ready | Readiness probe failing (chapter 10) | kubectl describe pod |
By default a pod's restartPolicy is Always, so the kubelet restarts a crashed container in place. The wait between restarts doubles from 10 seconds up to a cap of five minutes, and resets after the container runs cleanly for ten minutes.
Diagnose five broken pods
Now use that table. Each patient below shows what you would actually see: the kubectl get pods line, then the output of the command that table says to run. Pick a diagnosis before you read the answer.
Interactive · Pod doctor
Five broken pods. Read the evidence, pick the diagnosis.
What is wrong?
Multi-container pods: sidecars and init containers
Two patterns justify more than one container in a pod:
- Init containers run to completion, in order, before your app starts. Use them to wait for a dependency, fetch config or prepare files.
- Sidecars run alongside your app for its whole life: shipping logs, proxying traffic, refreshing credentials. Since Kubernetes 1.33 a sidecar is declared as an init container with
restartPolicy: Always, so it starts before your app and stops after it.
apiVersion: v1
kind: Pod
metadata:
name: api
spec:
initContainers:
- name: wait-for-db
image: busybox:1.37
command: ["sh", "-c", "until nslookup db; do sleep 2; done"]
- name: log-shipper
image: busybox:1.37
restartPolicy: Always
command: ["sh", "-c", "tail -F /var/log/app/app.log"]
volumeMounts:
- name: logs
mountPath: /var/log/app
containers:
- name: api
image: ghcr.io/example/api:1.4.2
volumeMounts:
- name: logs
mountPath: /var/log/app
volumes:
- name: logs
emptyDir: {}- Init container: loops until the db Service resolves, then exits so the next one can start.
- Turns an init container into a native sidecar that keeps running.
- A scratch volume that lives as long as the pod. Both containers mount it to share log files.
Hands-on: create, inspect, break and fix a pod
The lab is docked at the bottom of your screen.
Live lab · no install
Create, inspect, break and fix a pod
- Tasks0/8
Read the manifest
Every object has apiVersion, kind, metadata and spec. Open the Files tab to edit it.
cat pod.yamlCreate the pod
kubectl apply -f pod.yamlSee where it landed
Wait for Running, then -o wide shows its IP and node.
kubectl wait --for=condition=Ready pod/web kubectl get pod web -o wideLook inside: logs and a command
kubectl logs web kubectl exec web -- nginx -vOpen a shell in the container
Type exit to come back to your laptop.
kubectl exec -it web -- shBreak one on purpose
This container prints a line and exits with an error. Watch its status in the map turn red.
kubectl run crash --image=busybox:1.37 -- sh -c "echo starting; exit 1"Error, then CrashLoopBackOff: the kubelet restarts it with a growing delay.
Find out why it died
kubectl logs crash --previous kubectl describe pod crash | grep -A3 "Last State"Clean up
kubectl delete pod crash- Lab complete. Keep experimenting, or press Reset to start again.
- ready
- starting / not ready
- failing
- terminating
Prefer your own machine? The same steps on a local cluster
Save the first manifest above as pod.yaml, then:
kubectl apply -f pod.yaml
kubectl get pod web -o wide
pod/web created
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web 1/1 Running 0 8s 10.244.0.9 learn-control-plane <none> <none>-o wide- Adds the pod IP and the node it was scheduled on.
Look inside it from four angles: its events, its logs, a shell, and its port.
kubectl describe pod web
kubectl logs web
kubectl exec -it web -- nginx -v
kubectl port-forward pod/web 8080:80
describe- Everything about the pod, ending with Events: your first stop for any problem.
logs- Standard output of the container. Add -f to follow, --previous for the last crashed one.
exec -it ... --- Runs a command inside the container. Use sh instead of nginx -v for a shell.
port-forward- Tunnels localhost:8080 to the pod. Open http://localhost:8080, then Ctrl+C.
Now break one on purpose. This container prints a line and exits with an error:
kubectl run crash --image=busybox:1.37 -- sh -c "echo starting; exit 1"
kubectl get pod crash -w
pod/crash created
NAME READY STATUS RESTARTS AGE
crash 0/1 ContainerCreating 0 1s
crash 0/1 Error 0 3s
crash 0/1 Error 1 (2s ago) 5s
crash 0/1 CrashLoopBackOff 1 (14s ago) 17skubectl logs crash --previous
kubectl describe pod crash | grep -A3 "Last State"
kubectl delete pod crash
starting
Last State: Terminated
Reason: Error
Exit Code: 1--previous- Logs from the container that crashed, not the one waiting to start.
Exit Code: 1- The app chose to exit with an error. 137 would mean it was killed, usually OOM.
Mini mission
Fix a broken pod
Apply this manifest. Find out why it never becomes Running, and fix it without deleting the pod.
apiVersion: v1
kind: Pod
metadata:
name: broken
spec:
containers:
- name: nginx
image: nginx:1.27-typoReveal one safe solution
kubectl apply -f broken.yaml
kubectl describe pod broken | tail -n 5
kubectl set image pod/broken nginx=nginx:1.27
kubectl get pod broken -w
The events show Failed to pull image "nginx:1.27-typo". The image is one of the few fields you can change on a running pod, so kubectl set image fixes it in place and the kubelet pulls the right one. Clean up with kubectl delete pod broken web.
Check yourself
Quiz
0 of 4 answered