Sub topic 7
Reading and writing a Deployment manifest
This chapter assumes you can read YAML, run kubectl apply and kubectl get, and know what a Pod and a label are. It also assumes you have seen a ReplicaSet keep a fixed number of Pods running. A Deployment manifest looks much like a ReplicaSet manifest, with extra fields that control how updates happen.
The four parts of the manifest
Here is a complete Deployment. Save it as deployment.yaml.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
revisionHistoryLimit: 10
progressDeadlineSeconds: 600
selector:
matchLabels:
app: web
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.25
ports:
- containerPort: 80
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 3
periodSeconds: 5Read it in four parts.
- Identity.
apiVersion: apps/v1,kind: Deploymentandmetadata.namesay what the object is and what it is called. - Desired count.
spec.replicasis how many Pods you want. If you omit it, the default is 1. - Selector and template.
spec.selectorsays which Pods this Deployment owns.spec.templatedescribes the Pods it creates. - Update behavior.
strategy,revisionHistoryLimitandprogressDeadlineSecondscontrol rollouts.
Selector and template must agree
The selector app: web must match the labels in template.metadata.labels. If it does not, the API server rejects the manifest. The selector is also immutable in apps/v1. You cannot change it after creation, so choose labels that identify the application and do not change over time.
The template is the part that matters most for updates. Any change to it, such as a new image, produces a new revision. Changing replicas does not.
The update fields
| Field | Meaning in this manifest |
|---|---|
| `strategy.type` | `RollingUpdate` replaces Pods gradually. The other choice is `Recreate`. |
| `maxSurge: 1` | Up to one Pod above `replicas` may exist during an update. |
| `maxUnavailable: 0` | The number of ready Pods never drops below `replicas`. |
| `revisionHistoryLimit: 10` | Keep ten old ReplicaSets for rollbacks. |
| `progressDeadlineSeconds: 600` | Report a rollout as failed if it makes no progress for 600 seconds. |
With maxUnavailable: 0, an old Pod is removed only after a new Pod passes its readiness probe. That is why the probe is in the template. Without it, a Pod counts as ready as soon as its container starts, even if the application is not yet serving requests.
Create it and inspect the result
kubectl apply -f deployment.yaml
kubectl get deployment web
kubectl get rs -l app=web
kubectl get pods -l app=webYou wrote only the Deployment. The ReplicaSet and the three Pods were created by controllers. The ReplicaSet name is the Deployment name plus a hash of the template, and each Pod name adds a further random suffix.
Exercise
You change image from nginx:1.25 to nginx:1.26 and also change replicas from 3 to 5, then run kubectl apply. Which of the two changes creates a new ReplicaSet, and why?
Show answer
The image change does. It modifies spec.template, so the Deployment creates a new ReplicaSet for the new template. The replicas change only alters the desired count, so it adjusts the number of Pods without creating a new ReplicaSet.