Kubernetes foundations: the cluster and kubectl · 6 MIN READ

Pods: shared containers, phases, container states and restart policy

Run and inspect pods, tell the pod phase from container states and the STATUS column, choose a restartPolicy, use logs and exec, and explain why controllers rather than people should create pods.

A Pod is the smallest thing Kubernetes schedules: one or more containers that always run together on one node and share a network identity. Most debugging starts by reading a pod's state, and the STATUS column that kubectl prints mixes three different things. This lesson separates them, shows how restart policy changes what a failure looks like, and explains why you will rarely create a Pod yourself.

kubernetes · A MAP OF THE IDEA

  1. Group co-located containers
  2. Choose a restartPolicy
  3. Read container states
  4. Use logs --previous and exec
  5. Let a controller own the pod

Useful when

  • Co-located containers share network and volumes without extra plumbing.
  • Container states and reasons pinpoint most start-up failures.

Watch out

  • A bare pod is never replaced after deletion or node loss.
  • The STATUS column is not the phase; read the container state.

Implementation & operation cost: Learning three overlapping status vocabularies and wrapping every long-lived pod in a controller.

01

One pod, several containers, one network namespace

Containers in a pod share a network namespace: one IP address, one port space and localhost between them, so two containers in a pod cannot both listen on port 80. They also share the hostname, and they can share volumes, such as an emptyDir that lives exactly as long as the pod. They are scheduled together onto one node and deleted together. That is the test for putting containers into one pod: they must be co-located and share a lifecycle, like an application and the agent that ships its logs, or a proxy that serves only it. A web application and its database scale and fail independently, so they belong in separate pods. Init containers run to completion before the app containers start, and native sidecars, init containers with restartPolicy: Always, start first and keep running beside them.

yaml
apiVersion: v1
kind: Pod
metadata:
  name: pair
  labels:
    app: pair
spec:
  volumes:
  - name: html
    emptyDir: {}
  containers:
  - name: web
    image: nginx:1.27
    ports:
    - containerPort: 80
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
  - name: writer
    image: busybox
    command: ["sh", "-c", "while true; do date > /html/index.html; sleep 10; done"]
    volumeMounts:
    - name: html
      mountPath: /html
Read the code

The writer container rewrites index.html every ten seconds in the shared emptyDir, and nginx serves the same file from its own mount path: one volume, two mount points. Because the containers share the pod's network, the writer could also reach nginx at http://localhost:80. If either process exits, the kubelet restarts only that container, according to the pod's restartPolicy, and leaves the other one running.

02

Pod phase, container state and the STATUS column

status.phase is a coarse summary with five values. Pending: accepted, but not every container has started, which covers scheduling and image pulls. Running: bound to a node, with at least one container running, starting or restarting. Succeeded: every container exited with code 0 and none will restart. Failed: every container has terminated and at least one failed. Unknown: the node stopped reporting. Each container has its own state: Waiting with a reason such as ContainerCreating, ErrImagePull, ImagePullBackOff or CrashLoopBackOff; Running with a start time; or Terminated with an exit code and a reason such as Completed, Error or OOMKilled. The STATUS column of kubectl get pods is computed by kubectl from all of this and shows the most telling reason. So a pod in CrashLoopBackOff is in phase Running, and an ImagePullBackOff pod is Pending.

03

restartPolicy decides what a failure looks like

The pod's restartPolicy applies to all of its app containers and is enforced by the kubelet on the same node; it never moves a pod to another node. Always, the default and the only value a Deployment accepts, restarts a container whenever it exits, even with code 0, after an exponential back-off of 10, 20, 40 seconds and so on, capped at five minutes; the waits between attempts are what CrashLoopBackOff means. OnFailure restarts only after a non-zero exit, so a successful run ends in Succeeded. Never leaves the container terminated and the pod Succeeded or Failed; Jobs use OnFailure or Never. A restart creates a fresh process from the same image in the same pod: same IP and volumes, but no in-memory state. kubectl logs --previous shows the output of the run before.

04

kubectl run, logs and exec

kubectl run creates a single Pod. --image is required, --restart sets restartPolicy, --port records a containerPort, --labels sets labels, and everything after -- becomes the container's args, or its command when you add --command. kubectl logs reads a container's stdout and stderr as kept by the kubelet on the node, so the logs disappear with the pod; ship them to a log store for anything you need later. Use -c to choose a container, --previous for the last terminated instance, --tail and --since to limit the output, and -f to follow it. kubectl exec starts a new process inside a running container with whatever tools its image contains. Minimal images often have no shell at all, which is what kubectl debug and ephemeral containers are for on a real cluster; the lab does not offer kubectl debug.

bash
kubectl run web --image=nginx:1.27 --port=80
kubectl run once --image=busybox --restart=Never -- echo done
kubectl logs web --tail=20
kubectl exec web -- cat /etc/hostname
kubectl delete pod web
Read the code

The first pod runs nginx with the default restartPolicy Always. The second runs echo done once and ends in phase Succeeded, which kubectl shows as Completed. Once web is running, logs --tail=20 prints nginx's start-up lines, and exec runs cat inside its container and prints the hostname, web. delete removes the pod for good: nothing recreates it.

05

Why you rarely create bare Pods

A Pod is bound to one node for its whole life. If that node fails or is drained, or if you delete the pod by mistake, the pod is gone and nothing creates a replacement. Pods are also nearly immutable: most of the spec cannot change after creation, the container image being one of the few exceptions, so changing ports, environment or volumes means deleting and recreating the pod. Controllers solve both problems. A Deployment keeps a number of interchangeable replicas and replaces pods when the template changes, a StatefulSet adds stable names and storage, a DaemonSet runs one pod per node, and a Job runs pods to completion with retries. Bare pods are fine for a one-off debugging shell or a quick experiment; everything you want to keep running should have an owner.

06

Try it in the lab terminal

Start four single-container pods that end up in four different states, let half a minute of cluster time pass with cluster tick, and compare the STATUS column with the phase and container state underneath it. Then read the events of the pod that cannot pull its image and the logs of the one that crashes. Finally run the two-container pod from above, and delete a bare pod to see that nothing brings it back.

bash
kind create cluster
kubectl run web --image=nginx:1.27 --port=80
kubectl run once --image=busybox --restart=Never -- echo done
kubectl run typo --image=nginx:1.277
kubectl run crash --image=gopherforge/web:v3-crash
cluster tick 30s
kubectl get pods
kubectl get pods -o custom-columns=NAME:.metadata.name,PHASE:.status.phase,RESTARTS:.status.containerStatuses[0].restartCount,WAITING:.status.containerStatuses[0].state.waiting.reason,EXIT:.status.containerStatuses[0].state.terminated.exitCode
kubectl describe pod typo
kubectl logs crash --previous
cat > pair.yaml <<'EOF'
apiVersion: v1
kind: Pod
metadata:
  name: pair
  labels:
    app: pair
spec:
  volumes:
  - name: html
    emptyDir: {}
  containers:
  - name: web
    image: nginx:1.27
    ports:
    - containerPort: 80
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
  - name: writer
    image: busybox
    command: ["sh", "-c", "while true; do date > /html/index.html; sleep 10; done"]
    volumeMounts:
    - name: html
      mountPath: /html
EOF
kubectl apply -f pair.yaml
kubectl wait --for=condition=Ready pod/pair
kubectl exec pair -c writer -- hostname
kubectl delete pod web
kubectl get pods
Read the code

After 30 seconds get pods shows four STATUS values: crash CrashLoopBackOff with 2 restarts, once Completed, typo ImagePullBackOff, web Running. The custom columns show what lies underneath: crash is in phase Running while its container waits in CrashLoopBackOff, once is Succeeded with exit code 0, and typo is still Pending. describe pod typo ends with Failed to pull image "nginx:1.277" ... not found and Back-off pulling image events, and logs --previous shows the Go panic that killed crash. pair turns Ready with 2/2 containers, exec -c writer -- hostname prints pair, and after delete pod web nothing replaces it.

TAKE THIS WITH YOU

A pod's phase, its containers' states and kubectl's STATUS column are three different views; read the container state and its reason to learn what is wrong. Containers in a pod share network, hostname and volumes, and pods are disposable, so let a controller own them.

Check your understanding

kubectl get pods shows STATUS CrashLoopBackOff for a pod that a Deployment owns. What is the pod's status.phase?

Go to the source

Progress stays in this browser. No account needed.