Sub topic 7
Deployments: managing replicated Pods
This chapter assumes you can read a basic manifest (apiVersion, kind, metadata, spec), know what labels are, and have a Pod spec with containers in mind. You also need kubectl access to a cluster.
Why use a Deployment
A bare Pod is not replaced if its node fails or if you delete it. A Deployment declares how many identical Pods you want and keeps that number running. It also manages updates to a new image or configuration.
A Deployment does not create Pods directly. It creates a ReplicaSet, and the ReplicaSet creates the Pods. Each change to the Pod template produces a new ReplicaSet, and the Deployment moves Pods from the old one to the new one.
A complete example
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
namespace: demo
labels:
app: web
spec:
replicas: 2
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27
ports:
- containerPort: 80
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
memory: 256Mi
readinessProbe:
httpGet:
path: /
port: 80The parts to understand:
apiVersion: apps/v1is the group and version for Deployments. Usingv1alone fails.spec.replicasis the desired Pod count. If you omit it, the default is 1.spec.templateis the Pod template. It has its ownmetadataandspec, but noname, because Pod names are generated.spec.selectortells the Deployment which Pods it owns.
The selector must match the template labels
spec.selector.matchLabels must match the labels in spec.template.metadata.labels. If it does not, the API server rejects the manifest.
The selector is also immutable after creation. To change it, you must delete and recreate the Deployment. Choose stable labels such as app: web, and do not put values that change, like a version number, in the selector.
How updates roll out
Changing anything under spec.template, such as the image tag, triggers a rolling update. Changing replicas does not, because it only scales the existing ReplicaSet. The default strategy is RollingUpdate. It replaces Pods gradually and uses the readiness probe to decide when a new Pod can receive traffic. You can tune it:
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0maxSurge is how many extra Pods may exist during the update. maxUnavailable is how many Pods may be missing. With maxUnavailable: 0, a new Pod must become ready before an old one is removed. Use kubectl explain deployment.spec.strategy to see all fields.
Watching and undoing a rollout
kubectl rollout status deployment/web -n demo
kubectl rollout history deployment/web -n demo
kubectl rollout undo deployment/web -n demoCommon errors
| Symptom | Likely cause |
|---|---|
| `selector does not match template labels` | `matchLabels` and template labels differ |
| `field is immutable` | You edited `spec.selector` on an existing Deployment |
| Rollout stalls | New Pods fail the readiness probe or cannot be scheduled |
| `ImagePullBackOff` | Wrong image name or tag, or missing registry credentials |
Exercise
You change replicas from 2 to 4 and apply the file. Does the Deployment create a new ReplicaSet? What if you change the image tag instead?
Show answer
Changing replicas does not create a new ReplicaSet. The existing one is scaled to 4 Pods. Changing the image tag changes the Pod template, so the Deployment creates a new ReplicaSet and does a rolling update.
Exercise
Why is maxUnavailable: 0 combined with a readiness probe useful?
Show answer
Kubernetes keeps all old Pods until each new Pod passes its readiness probe, so capacity never drops during the update.