2.4. Deployments
In this lab, you run an application as a Deployment, scale it, roll out a new version, roll it back and change it directly in the cluster.
Step 1: Create a Deployment
Create a directory for the files of this lab and change into it:
Create the file web.yaml in ~/labs/2.4 with a Deployment that runs three nginx Pods:
Create the Deployment:
List the Deployment, its ReplicaSet and its Pods:
If READY isn’t 3/3 yet, the image is still downloading. Wait a moment and repeat the command.
Explanation
You don’t create the Pods yourself anymore. The Deployment creates a ReplicaSet,
and the ReplicaSet creates and names the Pods: Deployment name, a hash of the Pod template and a random suffix.
That’s why the template has no name.
| Field | Meaning |
|---|---|
replicas | Number of Pods that should run |
selector | Labels the Deployment uses to find its Pods (the same as in the template) |
template | Manifest for the Pods: the same metadata and spec you wrote in 2.3 for a Pod |
Step 2: Replace a Deleted Pod
Delete one of the Pods. Use a name from your output:
List the Pods again:
If the new Pod isn’t Running yet, wait a moment and repeat the command.
Explanation
The ReplicaSet makes sure that three Pods run at all times. It noticed the missing Pod and created a new one. You can recognize it by its new name and its lower age.
Step 3: Scale the Deployment
Change the number of replicas in web.yaml from 3 to 5:
Apply the manifest and list the Pods:
If the new Pods aren’t Running yet, wait a moment and repeat the command.
Explanation
The ReplicaSet started two more Pods to reach the new number of replicas. Scaling down works the same way: with a lower number, it deletes Pods.
Step 4: Roll Out a New Version
Change the image in web.yaml to the newer version nginx:1.29:
Apply the manifest:
Follow the rollout right away. It takes about 5 seconds:
If the rollout has already finished, you only see the last line.
List the ReplicaSets:
Explanation
A new image means a new Pod template, so the Deployment creates a second ReplicaSet. During the rolling update, it starts new Pods and stops old ones step by step. Some Pods run at any time, so the application stays available.
The old ReplicaSet stays with 0 replicas. The Deployment needs it to roll back.
Step 5: Roll Back
Show the revisions of the Deployment:
Go back to the previous revision and check the image:
If UP-TO-DATE isn’t 5 yet, wait a moment and repeat the command.
The warning tells you that web.yaml doesn’t match the cluster anymore: it still says nginx:1.29.
Set the image in the file back to nginx:1.28 and apply it:
Explanation
Each revision is one Pod template and thus one ReplicaSet: revision 1 runs nginx:1.28, revision 2 nginx:1.29.
Scaling in Step 3 didn’t create a revision, because it doesn’t change the template. CHANGE-CAUSE stays empty. You
can ignore it.
kubectl rollout undo scales the old ReplicaSet up again and the new one down, in a rolling update like
before. -o wide adds the column IMAGES, which shows that the old version runs again.
rollout undo is a quick way back in an emergency, but it changes only the cluster, not your manifest. Without the
fix in the file, the next kubectl apply would roll out nginx:1.29 again. The apply after the fix changed no
Pods: they already run nginx:1.28. It reports configured and not unchanged, because it updated the copy of
your last applied manifest that the warning mentioned.
Step 6: Change the Deployment in the Cluster
kubectl edit opens a resource from the cluster in an editor. Choose nano as the editor and open the Deployment:
The editor shows the Deployment as Kubernetes stores it, with all default values. Near the top, in the section
spec, change replicas: 5 to replicas: 2. Leave the section status at the end as it is: Kubernetes fills it
in itself. Save with Ctrl+O and Enter, then close the editor with Ctrl+X:
If kubectl reports Edit cancelled, no changes made., you closed the editor without saving. Repeat the command.
List the Deployment:
If READY isn’t 2/2 yet, wait a moment and repeat the command.
web.yaml still says replicas: 5. Apply it and list the Deployment again:
If READY isn’t 5/5 yet, wait a moment and repeat the command.
Explanation
kubectl edit is imperative, like kubectl run in 2.2 and kubectl rollout undo in Step 5: you change the
Deployment directly in the cluster, and no file records the change. Your manifest still said replicas: 5, so the
next kubectl apply scaled the Deployment back up. Without KUBE_EDITOR, kubectl edit uses the editor in
EDITOR, or else vi. The export lasts until you close the terminal.
Use kubectl edit only for a quick fix in an emergency or to try something out, and write every change you want to
keep into the manifest. The manifest stays the source of truth: you can review it, keep it in Git and apply it
again at any time.
Optional Tasks
Finished early? Try these tasks. The following labs don’t depend on them. Each task lists the commands you need. Open the solution if you get stuck.
Task 1: Bad Release
Roll out a version that doesn’t exist, e.g. nginx:1.299. What happens to the running Pods? Then repair the
Deployment.
You know all the commands you need.
Task 2: Drain the Node
Take an agent node out of service for maintenance and watch where its Pods go. Then put the node back into service.
| Command | Effect |
|---|---|
kubectl get pods -o wide | Also shows the node of each Pod in the column NODE |
kubectl drain <node> --ignore-daemonsets --delete-emptydir-data | Moves all Pods away from a node and blocks new ones |
kubectl uncordon <node> | Allows new Pods on the node again |
Clean Up
Delete the Deployment, since the following labs don’t need it:
Kubernetes deletes the ReplicaSets and Pods of the Deployment along with it.
If you did Task 2, check with kubectl get nodes that no node shows SchedulingDisabled. If one does, run
kubectl uncordon <node>.