Pods
A Pod is the smallest thing Kubernetes runs. It wraps one container (sometimes a few tightly coupled ones) and gives it:
- its own IP address in the cluster network, shared by all containers in the Pod (they talk to each other on
localhost), - shared volumes the containers can mount,
- a lifecycle: Kubernetes starts, health-checks and restarts its containers.
Pods are disposable. They are never moved: if a node dies, its Pods are gone and a controller creates new Pods (new names, new IPs) elsewhere. That is why you rarely create bare Pods in production; you create a Deployment or another controller that manages them. Learning Pods first is still essential, because every workload is built from a Pod template.
A Pod manifest
apiVersion: v1
kind: Pod
metadata:
name: hello
labels:
app: hello
spec:
restartPolicy: Always # Always (default) | OnFailure | Never
containers:
- name: web
image: nginx:1.27 # always pin a tag; avoid :latest
ports:
- containerPort: 80
env:
- name: GREETING
value: "hi there"kubectl apply -f hello-pod.yaml
kubectl get pod hello -o wide
kubectl logs hello
kubectl exec -it hello -- sh -c 'echo $GREETING'
kubectl port-forward pod/hello 8080:80 # then open http://localhost:8080
kubectl delete pod helloSample output:
pod/hello created
NAME READY STATUS RESTARTS AGE IP NODE
hello 1/1 Running 0 8s 10.244.1.12 demo-worker
hi therePod phases
status.phase summarises where the Pod is in its life:
| Phase | Meaning |
|---|---|
Pending |
Accepted, but not running yet: waiting for scheduling or pulling images |
Running |
Bound to a node, at least one container running (or starting/restarting) |
Succeeded |
All containers exited with code 0 and won't be restarted |
Failed |
All containers terminated and at least one failed |
Unknown |
The node stopped reporting |
The STATUS column of kubectl get pods shows more specific reasons such as ContainerCreating, CrashLoopBackOff, ImagePullBackOff or Completed.
Restart policy and CrashLoopBackOff
When a container exits, the kubelet consults restartPolicy:
def kubelet_decision(policy, exit_code):
if policy == "Always":
return "restart"
if policy == "OnFailure":
return "restart" if exit_code != 0 else "leave stopped (Succeeded)"
return "leave stopped (" + ("Succeeded" if exit_code == 0 else "Failed") + ")"
for policy in ["Always", "OnFailure", "Never"]:
for code in [0, 1]:
print(f"{policy:9} exit={code} -> {kubelet_decision(policy, code)}")Always exit=0 -> restart
Always exit=1 -> restart
OnFailure exit=0 -> leave stopped (Succeeded)
OnFailure exit=1 -> restart
Never exit=0 -> leave stopped (Succeeded)
Never exit=1 -> leave stopped (Failed)
A container that keeps crashing isn't restarted instantly forever. The kubelet waits longer each time: 10 s, doubling up to a cap of 5 minutes, and the Pod shows CrashLoopBackOff while it waits. (The back-off resets after the container has run fine for 10 minutes.)
def backoff_delays(crashes, base=10, cap=300):
delays = []
delay = base
for _ in range(crashes):
delays.append(delay)
delay = min(delay * 2, cap)
return delays
delays = backoff_delays(8)
print("delays (s):", delays)
print("total wait after 8 crashes:", sum(delays), "s")delays (s): [10, 20, 40, 80, 160, 300, 300, 300]
total wait after 8 crashes: 1210 s
Tip:
CrashLoopBackOffis a symptom, not a cause. Runkubectl logs <pod> --previousto see the output of the container run that crashed.
Container essentials in a Pod spec
| Field | Purpose |
|---|---|
image |
Image reference, e.g. ghcr.io/acme/api:2.3.1 |
imagePullPolicy |
IfNotPresent (default for pinned tags), Always (default for :latest), Never |
command / args |
Override the image's ENTRYPOINT / CMD |
env / envFrom |
Environment variables (literal, or from ConfigMaps/Secrets) |
ports |
Documentation of listening ports (informational, but used by named ports) |
resources |
CPU/memory requests and limits (see Resources) |
volumeMounts |
Where volumes appear in the filesystem |
command and args interact with the image's defaults in a specific way. A model of the rule:
def effective(image_entrypoint, image_cmd, command=None, args=None):
if command is not None:
return command + (args if args is not None else [])
return image_entrypoint + (args if args is not None else image_cmd)
ENTRY, CMD = ["python", "app.py"], ["--port", "8000"]
print(effective(ENTRY, CMD))
print(effective(ENTRY, CMD, args=["--port", "9000"]))
print(effective(ENTRY, CMD, command=["sh", "-c", "echo debug"]))
print(effective(ENTRY, CMD, command=["python"], args=["-m", "http.server"]))['python', 'app.py', '--port', '8000']
['python', 'app.py', '--port', '9000']
['sh', '-c', 'echo debug']
['python', '-m', 'http.server']
So setting command throws away the image's CMD too, while setting only args keeps the entrypoint.
Test yourself
Two containers in the same Pod want to talk to each other. Which address do they use?
- The node's IP
- localhost
- A Service name only
- They can't communicate
A Pod with restartPolicy: OnFailure exits with code 0. What happens?
- It is restarted
- It is left stopped and the Pod becomes Succeeded
- It is moved to another node
- The node reboots
Which command shows the logs of the previous, crashed run of a container?
- kubectl logs pod --previous
- kubectl describe logs
- kubectl get logs
- kubectl events --crash