Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Kubernetes Deployments and replicas

After this lesson: I can keep three copies of a pod running with a Deployment.

50 min lesson11 commandsKubernetes lab: a one-node k3s cluster for each student on a private Ubuntu VM

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

WordWhat it meansExample
Deploymentyour wish for a number of identical pods
Replicaone of the identical pods3 replicas
ReplicaSetthe part that counts the pods and starts more
Scalechange how many replicas you want
Desired statewhat 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

LevelName in this lessonIts job
DeploymentordersHolds the wish: 3 replicas
ReplicaSetorders-5c55dd9d86Counts the pods and starts more
Podorders-5c55dd9d86-9wc6gRuns the program

The cells students fill in are left blank in this preview.

Make a namespace for the shop

Command
kubectl create namespace shop

Shown 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

Command
kubectl create deployment orders -n shop --image=busybox:1.37.0 --replicas=3 -- sleep 3600

Shown 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

Command
kubectl rollout status deployment/orders -n shop

Shown 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

Command
kubectl get replicaset -n shop --no-headers -o custom-columns=WANT:.spec.replicas,READY:.status.readyReplicas

Shown 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

Command
kubectl scale deployment orders -n shop --replicas=5

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Wait until five are ready

Command
kubectl wait --for=jsonpath='{.status.readyReplicas}'=5 deployment/orders -n shop --timeout=60s

Shown 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

Command
kubectl get pods -n shop -o name | head -n 1 | xargs kubectl delete -n shop --grace-period=1

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Count the pods again

Command
kubectl get pods -n shop --no-headers | wc -l

Shown 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

Command
kubectl scale deployment orders -n shop --replicas=0

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

The Deployment is still there

Command
kubectl get deployment orders -n shop

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Remove the namespace

Command
kubectl delete namespace shop

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

About this lab and its source: Kubernetes lab: a one-node k3s cluster for each student on a private Ubuntu VM

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.