Sub topic 17
Why you rarely create bare Pods: controllers
This chapter assumes you can read a Pod manifest, know how labels and selectors match objects, and understand restartPolicy and node scheduling at a basic level.
What a bare Pod lacks
A bare Pod is a Pod you create directly with kubectl apply. It is tied to one node for its whole life. The kubelet restarts failed containers in it, but only on that node. If the node fails, the Pod is not rescheduled. If you delete the Pod, or the node evicts it, nothing recreates it.
A bare Pod also gives you no way to run several copies, no rolling updates, and no rollback. Pods are meant to be disposable. A controller is the part of Kubernetes that makes a group of disposable Pods dependable.
The controller pattern
A controller runs a loop. It compares the desired state you declared with the observed state of the cluster, then acts to close the gap. Most workload controllers find their Pods by label selector and create new Pods from a Pod template.
Common controllers
| Controller | Use it for |
|---|---|
| Deployment | Stateless apps with rolling updates and rollback |
| StatefulSet | Pods that need stable names and their own storage |
| DaemonSet | One Pod on every node, such as a log agent |
| Job | Work that runs to completion |
| CronJob | Jobs on a schedule |
A Deployment manages a ReplicaSet, and the ReplicaSet keeps the requested number of Pods running. You rarely work with ReplicaSets directly.
A Deployment around the web Pod
The Pod spec you already know moves into spec.template. The selector must match the template's labels.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.27
ports:
- containerPort: 80Apply it and watch the controller work:
kubectl apply -f deployment.yaml
kubectl get pods -l app=web
kubectl delete pod <one-of-the-pod-names>
kubectl get pods -l app=web
kubectl scale deployment web --replicas=5
kubectl set image deployment/web nginx=nginx:1.27.1
kubectl rollout status deployment/web
kubectl rollout undo deployment/webAfter the delete, a replacement Pod appears within seconds. The scale command adds Pods. The image change starts a rolling update, which replaces Pods gradually. The undo command returns to the previous revision.
Gotchas
- Pod names get a generated suffix, so do not depend on a fixed name.
- The template labels must match
spec.selector, or the API server rejects the Deployment. spec.selectoris immutable after creation inapps/v1.- Changing the Pod template triggers a rollout. Changing
replicasdoes not. - Deleting a Deployment deletes its Pods through owner references.
- A Pod that matches a controller's selector can be adopted by it. Reusing labels carelessly can cause surprises.
Exercise
You delete one Pod that belongs to a Deployment with replicas: 3. What happens, and why does the same action on a bare Pod behave differently?
Show answer
The ReplicaSet notices only two matching Pods exist and creates a new one from the Pod template, so three are running again. A bare Pod has no controller watching it, so nothing recreates it after deletion.
Exercise
A Deployment fails to apply because of a selector problem. What is the most likely cause?
Show answer
The labels in spec.template.metadata.labels do not match spec.selector.matchLabels.