Skip to Content

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 , founding engineer running Kubernetes in production. Updated .

to 10.244.1.7:80pod: webone IP: 10.244.1.7container: appnginx:1.27logs sidecarships logslocalhostshared volume/var/log/nginxScheduledtogether,replacedtogether
The chapter in one pictureA pod is one or more containers that share an IP, a lifecycle and volumes.

By the end you can

0 of 5

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 localhost and 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.

pod.yaml
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

  1. Pending

    Accepted, not yet on a node or not started

  2. Waiting

    On a node, container not running yet

  3. Running

    Container is running

  4. Terminated

    Container exited

kubectl get pod web -w

What is happening

What to run next

The pattern behind all of them:

You seeIt meansRun
PendingNo node yet, or not startedkubectl describe pod, read Events
ErrImagePull, ImagePullBackOffImage name, tag or registry accesskubectl describe pod
CrashLoopBackOffThe app starts and exits, again and againkubectl logs --previous
OOMKilledUsed more memory than its limitkubectl describe pod, check Last State
Running but 0/1 readyReadiness 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.
api-pod.yaml
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

Live lab · no install

Create, inspect, break and fix a pod

kind-learn
  1. Tasks0/8
  2. Read the manifest

    Every object has apiVersion, kind, metadata and spec. Open the Files tab to edit it.

    cat pod.yaml
  3. Create the pod
    kubectl apply -f pod.yaml
  4. See 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 wide
  5. Look inside: logs and a command
    kubectl logs web
    kubectl exec web -- nginx -v
  6. Open a shell in the container

    Type exit to come back to your laptop.

    kubectl exec -it web -- sh
  7. Break 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.

  8. Find out why it died
    kubectl logs crash --previous
    kubectl describe pod crash | grep -A3 "Last State"
  9. Clean up
    kubectl delete pod crash
  10. Lab complete. Keep experimenting, or press Reset to start again.
↑↓ history · Tab complete · Ctrl+C stop
Cluster map live
  • 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:

Terminal
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.

Terminal
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:

Terminal
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)  17s
Terminal
kubectl 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.

broken.yaml
apiVersion: v1
kind: Pod
metadata:
  name: broken
spec:
  containers:
    - name: nginx
      image: nginx:1.27-typo
Reveal one safe solution
Terminal
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

  1. 1. Two containers in the same pod need to talk. How?
  2. 2. What does CrashLoopBackOff tell you?
  3. 3. A pod is Pending with "0/3 nodes are available: 3 Insufficient memory". What is wrong?
  4. 4. Why do you rarely create bare pods in production?

Further reading