Sub topic 12
Pods: the smallest deployable unit
This chapter assumes you know what a container and a container image are, can use a Linux shell, and can read YAML. You should also know the cluster components from earlier chapters: the API server, the scheduler, and the kubelet.
What a Pod is
A Pod is the smallest unit you can create and manage in Kubernetes. It wraps one or more containers that run together on the same node. Kubernetes does not schedule bare containers. It schedules Pods.
Containers in one Pod share:
- A network namespace. They have one IP address and reach each other on
localhost. - Storage volumes that the Pod declares.
- A lifecycle. They are scheduled together onto one node and stop together.
Most Pods hold a single application container. Add a second container only when the two must run as one unit, such as a log shipper that reads files the main container writes. This is called a sidecar.
A minimal Pod
apiVersion: v1
kind: Pod
metadata:
name: web-pod
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27
ports:
- containerPort: 80Create it and see where it landed:
kubectl apply -f pod.yaml
kubectl get pods -o wideThe -o wide output includes the Pod IP and the node name. The scheduler picked that node, and the kubelet on it started the container through the container runtime.
Pods are ephemeral
A Pod is not repaired in place. If its node fails or the Pod is deleted, the Pod is gone. Its IP address goes with it, and a replacement gets a new one. Because of this, you rarely create bare Pods in practice. A Deployment, covered in a later chapter, creates Pods for you and replaces them when they fail. A Service gives clients a stable address.
A Pod moves through these phases:
| Phase | Meaning |
|---|---|
| Pending | The API server accepted the Pod, but it is not running yet. It may be waiting for a node or for image pulls. |
| Running | The Pod is bound to a node and at least one container is running or starting. |
| Succeeded | All containers exited with success and will not restart. |
| Failed | All containers have stopped and at least one failed. |
Common errors
When a Pod does not start, inspect it:
kubectl describe pod web-pod
kubectl logs web-podThe usual causes are:
- Pending for a long time. No node has enough CPU or memory, or the Pod cannot be scheduled. The Events section of
describenames the reason. - ImagePullBackOff. The image name or tag is wrong, or the node lacks credentials for a private registry.
- CrashLoopBackOff. The container starts and exits repeatedly. Read its logs, using
kubectl logs web-pod --previousfor the last crashed run.
Exercise
Why do you usually manage Pods through a Deployment instead of creating them directly?
Show answer
A Pod is ephemeral. If its node fails or it is deleted, nothing recreates it. A Deployment keeps the desired number of Pods running and replaces any that disappear.
Exercise
Two containers in the same Pod need to talk to each other. What address do they use?
Show answer
They use localhost, because containers in a Pod share one network namespace and one IP address.