Course contents · Lesson 9 of 22

What Is a Kubernetes Pod? Explained with Examples

Kubernetes Concepts · Lesson 9 of 22 · 11 min Engineer

In lesson 7 you were asked to think of a pod as one container that Kubernetes looks after. Now say your web app needs a helper beside it to ship its logs. This lesson explains what a Kubernetes pod really is, how to write one, how it lives and ends, and why you rarely create one on its own.

What a Kubernetes pod is

A pod is the smallest unit Kubernetes can create and manage: one or more containers that always run together on one node. You never ask Kubernetes for a lone container. You ask for a pod, and the kubelet from lesson 8 starts its containers.

What they share is the network. In lesson 5, each Docker container had its own network namespace, the kernel feature that gives it private network interfaces, IP addresses and ports. Kubernetes puts all of a pod's containers into one network namespace: the pod has one IP address, its containers share its ports, and they reach each other on localhost.

In the diagram, notice that the helper reaches the web app at localhost:80, while another pod must use the pod's IP. Since the ports are shared, two containers in one pod cannot both listen on port 80.

A node, one machine, holds a pod named web with one IP address, 10.244.0.5, labelled illustrative. Inside the pod are two containers, each with its own filesystem: a web app, nginx on port 80, and a helper, a sidecar such as a log shipper. An arrow shows the helper reaching the web app at localhost:80. Both containers mount one shared volume, marked optional. Outside, another pod calls 10.244.0.5:80. Notes: two containers cannot both listen on port 80, and the pod is one unit, on the same node with the same lifetime, deleted together
One flat, one address: inside the pod, localhost reaches every container.

Files are not shared by default: each container has its own filesystem from its own image. To share files, the pod declares a volume, a directory its containers can mount; lesson 14 covers the kinds.

Most pods hold one main container. Kubernetes scales by adding or removing whole pods, so only containers that must live and scale together share one. If one container crashes, the kubelet restarts just that container; the pod and its IP stay.

A minimal pod, line by line

You describe a pod in YAML, as you did for the Deployment in lesson 7. This file, pod.yaml, asks for one nginx container with a pinned image tag:

apiVersion: v1
kind: Pod
metadata:
  name: web
spec:
  containers:
    - name: web
      image: nginx:1.27-alpine
      ports:
        - containerPort: 80

The diagram ties each field to its meaning. Notice that containers is a list: a second container would be a second item starting with -.

The file pod.yaml, line by line, with an arrow from each field to its meaning. apiVersion v1: which API version defines Pod. kind Pod: the type of object, a Pod. metadata with name web: unique in the namespace. spec: desired state, what should run. containers, with one item named web: a list, one item per container. image nginx:1.27-alpine: the image to pull, tag pinned. ports with containerPort 80: the app's port, for information only. A note: indent with spaces, never tabs; a dash starts a list item
Four top-level fields; everything that runs sits under spec.

Two fields need more than a label. apiVersion: v1 means Pods belong to the core API, while Deployments live in apps/v1. And containerPort, like EXPOSE in lesson 3, only documents the port: leaving it out blocks nothing.

Once you have a cluster, from lesson 17, you send the file with kubectl; read along for now. The second command lists the pod, and -o wide adds its IP address and node:

kubectl apply -f pod.yaml
kubectl get pod web -o wide

On lesson 8's kind cluster, lab, you should see something like this; your age and IP will differ:

pod/web created
NAME   READY   STATUS    RESTARTS   AGE   IP           NODE                NOMINATED NODE   READINESS GATES
web    1/1     Running   0          12s   10.244.0.5   lab-control-plane   <none>           <none>

1/1 means one of one containers is ready. The most common mistake is a mistyped image tag: STATUS shows ErrImagePull, then ImagePullBackOff, and READY stays 0/1. Lesson 20 shows how to find the cause.

Sidecars and init containers

A pod holds two or more containers when a helper must sit beside the app. Such a helper is a sidecar: an extra container that supports the main one for the pod's whole life. A typical one is a log shipper that reads the app's log files from a shared volume and sends them to a central store, as in log aggregation. In YAML, the classic form is a second item under containers.

An init container runs before the app containers and must finish. Init containers run one at a time, in order, and each must exit successfully before the next starts; only then do the app containers start. Typical jobs are waiting until a database accepts connections or writing a config file into a shared volume.

While they run, kubectl get pods shows a STATUS such as Init:0/1: zero of one init containers have finished. With the default settings, a failing init container is retried with a growing delay, STATUS reads Init:CrashLoopBackOff, and the app containers never start.

The pod lifecycle, and why pods are disposable

Every pod reports a phase, a one-word summary of where it is in its life. What happens when a container exits depends on the pod's restartPolicy: Always (the default), OnFailure or Never. In the flowchart, notice the default path: the container is restarted, so a web server's pod stays Running.

Flowchart. kubectl apply leads to Pending: accepted, not running yet, while waiting for a node and pulling the image. Then Running: bound to a node, with a container running, starting or restarting. Then a decision: do containers exit and stay stopped? No: the kubelet restarts it, the default, back to Running. Yes: Succeeded if all exited with code 0 and none will restart, or Failed if one exited non-zero or the system killed it. A dashed path from Running leads to Unknown: the node stopped reporting, so the phase cannot be read. Footer: kubectl's STATUS column also shows reasons, not phases: ContainerCreating, CrashLoopBackOff, Terminating
A long-running app never ends: its containers restart in place.

Pending means the cluster accepted the pod, but its containers are not all set up yet: the scheduler is still choosing a node, or the image is downloading. Running means the pod is bound to a node and all its containers were created, with at least one running, starting or restarting.

Succeeded and Failed are end states for containers meant to finish, such as a batch task, when the policy does not restart them. Unknown means the pod's state cannot be read, usually because its node stopped reporting. It is not an end state: it clears when the node returns, and Kubernetes deletes a lost node's pods after five minutes by default. The official Pod Lifecycle page defines each phase.

Pods are ephemeral: short-lived and replaceable. A pod is placed on a node once and stays there until it ends, is deleted, is evicted (removed because the node runs short of resources such as memory) or its node fails. It never moves. A replacement is a new pod, with a new unique ID and usually a new IP address.

So if you delete the web pod and apply the same file again, the new pod has the same name but probably a new IP, such as 10.244.0.6. Anything that saved the old address now reaches nothing, which is why lesson 11 puts a stable Service in front of changing pods.

Why you rarely create bare pods

A pod you create yourself, with no controller behind it, is a bare pod. The kubelet on its node restarts crashed containers, but nothing else looks after it. If its node dies, the pod ends with it and nothing creates a replacement, because no controller holds a desired state that says it should exist.

So in practice you describe pods inside a controller. The template in lesson 7's Deployment holds the same metadata and spec as a pod. Its controllers, taken apart in lesson 10, keep the right number of pods running, replace pods lost with a node and roll out changes a few at a time. Bare pods suit quick experiments.

Check yourself

Your app calls a helper at localhost:9090 and gets "connection refused". The helper runs in another pod on the same node. Why?

Only containers in the same pod share a network namespace, so localhost never reaches another pod, even on the same node. Make the helper a sidecar if the two belong together, or call the other pod through a Service.

A teammate wants a web app and its database in one pod so they can talk over localhost. What goes wrong as traffic grows?

Kubernetes scales by copying whole pods, so every extra web pod brings an extra database with its own data. In separate pods, the web app scales alone.

You created a bare pod, and its node loses power. What happens to your app, and what would a Deployment have changed?

The app stays down: the pod ended with its node, and no controller wants it to exist, so nothing recreates it. A Deployment's controllers would notice one pod too few, after a few minutes by default, and create a replacement on a healthy node.