A Deployment keeps pods running
A Deployment is a Kubernetes object that keeps a set number of identical pods running. It matters because it replaces any pod that stops, so a crash at three in the morning needs nobody awake.
You tell it how many copies you want. It makes that number true, and keeps it true.
By the end of this lesson
Note
I can keep three copies of a pod running with a Deployment.
Key words
| Word | What it means | Example |
|---|---|---|
| Deployment | your wish for a number of identical pods | |
| Replica | one of the identical pods | 3 replicas |
| ReplicaSet | the part that counts the pods and starts more | |
| Scale | change how many replicas you want | |
| Desired state | what you asked for, which Kubernetes keeps true |
The cells students fill in are left blank in this preview.
A Deployment is a wish for pods
A Deployment does not run anything itself. It owns a ReplicaSet, and the ReplicaSet owns the pods.
You say replicas: 3. The ReplicaSet counts the pods it owns and starts or stops pods until the count is three. The number you asked for is the desired state.
Worked example: who owns what
| Level | Name in this lesson | Its job |
|---|---|---|
| Deployment | orders | Holds the wish: 3 replicas |
| ReplicaSet | orders-5c55dd9d86 | Counts the pods and starts more |
| Pod | orders-5c55dd9d86-9wc6g | Runs the program |
The cells students fill in are left blank in this preview.
Make a namespace for the shop
kubectl create namespace shopShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Ask for three copies of a program
kubectl create deployment orders -n shop --image=busybox:1.37.0 --replicas=3 -- sleep 3600Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Wait until all three are ready
kubectl rollout status deployment/orders -n shopShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Ask the ReplicaSet what it wants
kubectl get replicaset -n shop --no-headers -o custom-columns=WANT:.spec.replicas,READY:.status.readyReplicasShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Scaling changes the number in the wish
To scale, you change the number. Kubernetes does the rest. Going from three to five starts two new pods. Going down from five to two stops three.
The wait command below holds until the Deployment reports five ready pods.
Scale up to five copies
kubectl scale deployment orders -n shop --replicas=5Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Wait until five are ready
kubectl wait --for=jsonpath='{.status.readyReplicas}'=5 deployment/orders -n shop --timeout=60sShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A deleted pod is replaced at once
The ReplicaSet counts its pods all the time. If one is deleted, the count drops below five, so it starts a new one.
The next Run deletes the first pod in the list. The one after counts the pods again and finds five, not four.
Delete one pod of the five
kubectl get pods -n shop -o name | head -n 1 | xargs kubectl delete -n shop --grace-period=1Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Count the pods again
kubectl get pods -n shop --no-headers | wc -lShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A common mistake
Warning
Many people think scaling to zero deletes the Deployment. It does not. The Deployment stays, and its wish is now zero pods. Use kubectl delete deployment to remove it, and scale back up to start the pods again.
Scale down to nothing
kubectl scale deployment orders -n shop --replicas=0Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
The Deployment is still there
kubectl get deployment orders -n shopShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Remove the namespace
kubectl delete namespace shopShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.