Deployments and ReplicaSets

Keep copies running, even when a node goes.

Beginner20 min · lesson 6 of 21
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (2 files, 1 KB): deployments.tar.gz. The lab VM shares no folders with your computer, so fetch them inside the VM: cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/k8s-fund/deployments.tar.gz && tar -xzf deployments.tar.gz, which creates ~/lab/deployments/. SHA-256: 2a5b1a13d1f666a816441afa0fdededcad037d11eba0cffdacf94f21658b3006

The Pod you made in lesson 3 was gone for good the moment you deleted it. Lose the node it runs on and the effect is the same. An app that must stay up needs copies that come back on their own, and you need a way to empty a node for maintenance without taking the app down with it.

A Deployment handles both, and you already used one in lesson 1. Under the hood it does not manage Pods directly. It goes through a second object, the ReplicaSet, and once you know that chain, most of the names in kubectl get output make sense.

Three layers

web.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 4
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.30.6-alpine

The selector says which Pods belong to this Deployment: those with the label app: web. The template is the Pod to stamp out, and it carries that same label. replicas: 4 is how many copies to keep.

ubuntu@secopslog-k8s:~/lab/deployments · Kubernetes 1.37
$ kubectl apply -f web.yaml && kubectl rollout status deployment/web
deployment.apps/web created Waiting for deployment "web" rollout to finish: 0 out of 4 new replicas have been updated... Waiting for deployment "web" rollout to finish: 0 of 4 updated replicas are available... Waiting for deployment "web" rollout to finish: 1 of 4 updated replicas are available... Waiting for deployment "web" rollout to finish: 2 of 4 updated replicas are available... Waiting for deployment "web" rollout to finish: 3 of 4 updated replicas are available... deployment "web" successfully rolled out
$ kubectl get deployments,replicasets,pods
NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/web 4/4 4 4 1s NAME DESIRED CURRENT READY AGE replicaset.apps/web-56d6d5bd78 4 4 4 1s NAME READY STATUS RESTARTS AGE pod/web-56d6d5bd78-46nqb 1/1 Running 0 1s pod/web-56d6d5bd78-c8h2z 1/1 Running 0 1s pod/web-56d6d5bd78-kvp82 1/1 Running 0 1s pod/web-56d6d5bd78-x5l55 1/1 Running 0 1s

One file made three kinds of object. The Deployment web created a ReplicaSet called web- plus a string of characters, and the ReplicaSet created four Pods whose names are the ReplicaSet's name plus five more characters. Each layer has one job:

Who manages whom
1Deploymentwhich version, how to update2ReplicaSetkeeps N copies of one Pod…3Podsthe running containers
You edit the Deployment. The other two are created for you.

The ReplicaSet only counts Pods and adds or removes them. The Deployment sits on top so it can switch between versions: an update creates a new ReplicaSet and shrinks the old one, as lesson 10, Rolling updates, shows. Older guides also create bare ReplicaSets, or the even older ReplicationController. You do not need to write either yourself.

How they know each other

ubuntu@secopslog-k8s:~/lab/deployments · Kubernetes 1.37
$ kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS web-56d6d5bd78-46nqb 1/1 Running 0 1s app=web,pod-template-hash=56d6d5bd78 web-56d6d5bd78-c8h2z 1/1 Running 0 1s app=web,pod-template-hash=56d6d5bd78 web-56d6d5bd78-kvp82 1/1 Running 0 1s app=web,pod-template-hash=56d6d5bd78 web-56d6d5bd78-x5l55 1/1 Running 0 1s app=web,pod-template-hash=56d6d5bd78
$ kubectl get replicasets,pods -o custom-columns=NAME:.metadata.name,OWNER-KIND:.metadata.ownerReferences[0].kind,OWNER:.metadata.ownerReferences[0].name
NAME OWNER-KIND OWNER web-56d6d5bd78 Deployment web web-56d6d5bd78-46nqb ReplicaSet web-56d6d5bd78 web-56d6d5bd78-c8h2z ReplicaSet web-56d6d5bd78 web-56d6d5bd78-kvp82 ReplicaSet web-56d6d5bd78 web-56d6d5bd78-x5l55 ReplicaSet web-56d6d5bd78
$ kubectl get replicaset -l app=web -o jsonpath='{.items[0].spec.selector.matchLabels}{"\n"}'
{"app":"web","pod-template-hash":"56d6d5bd78"}

Every Pod has two labels: app=web from your template, and pod-template-hash, which the Deployment added. The hash is computed from the Pod template, so each version of the template gets its own value, and it is the string in the ReplicaSet's name. The ReplicaSet selects Pods with both labels, so two versions never claim each other's Pods.

The custom columns read ownerReferences, a field in each object's metadata. The ReplicaSet's owner is the Deployment web, and each Pod's owner is the ReplicaSet. These links tell Kubernetes what to clean up: delete the Deployment and the garbage collector removes everything it owns, as the end of this lesson shows.

Self-healing

The $(...) part of the next command picks the name of the first web Pod, so you do not have to copy it by hand:

ubuntu@secopslog-k8s:~/lab/deployments · Kubernetes 1.37
$ kubectl delete pod "$(kubectl get pods -l app=web -o name | head -1 | cut -d/ -f2)" && kubectl get pods
pod "web-56d6d5bd78-46nqb" deleted from default namespace NAME READY STATUS RESTARTS AGE web-56d6d5bd78-82hqn 1/1 Running 0 1s web-56d6d5bd78-c8h2z 1/1 Running 0 3s web-56d6d5bd78-kvp82 1/1 Running 0 3s web-56d6d5bd78-x5l55 1/1 Running 0 3s
$ kubectl rollout status deployment/web && kubectl get pods
deployment "web" successfully rolled out NAME READY STATUS RESTARTS AGE web-56d6d5bd78-82hqn 1/1 Running 0 1s web-56d6d5bd78-c8h2z 1/1 Running 0 3s web-56d6d5bd78-kvp82 1/1 Running 0 3s web-56d6d5bd78-x5l55 1/1 Running 0 3s
$ kubectl describe replicaset -l app=web | sed -n '/^Events:/,$p'
Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal SuccessfulCreate 4s replicaset-controller Created pod: web-56d6d5bd78-kvp82 Normal SuccessfulCreate 4s replicaset-controller Created pod: web-56d6d5bd78-c8h2z Normal SuccessfulCreate 4s replicaset-controller Created pod: web-56d6d5bd78-46nqb Normal SuccessfulCreate 4s replicaset-controller Created pod: web-56d6d5bd78-x5l55 Normal SuccessfulCreate 2s replicaset-controller Created pod: web-56d6d5bd78-82hqn

By the time kubectl delete returned, a replacement Pod, a couple of seconds younger than the others, was already running. The ReplicaSet's events show who made it: SuccessfulCreate lines for the four original Pods and one more for the replacement. The ReplicaSet saw three Pods where it wanted four and created one. The Deployment had nothing to do, because the template had not changed.

Taking a node out

Before maintenance on a node, such as a kernel update, you drain it: Kubernetes marks it unschedulable and evicts its Pods, so their owners recreate them elsewhere. First see where the Pods are:

ubuntu@secopslog-k8s:~/lab/deployments · Kubernetes 1.37
$ kubectl get pods -l app=web -o custom-columns=POD:.metadata.name,NODE:.spec.nodeName
POD NODE web-56d6d5bd78-82hqn lab-worker2 web-56d6d5bd78-c8h2z lab-worker web-56d6d5bd78-kvp82 lab-worker2 web-56d6d5bd78-x5l55 lab-worker
$ kubectl drain lab-worker --ignore-daemonsets
node/lab-worker cordoned Warning: ignoring DaemonSet-managed Pods: kube-system/kindnet-kczpg, kube-system/kube-proxy-9mcnv evicting pod default/web-56d6d5bd78-x5l55 evicting pod default/web-56d6d5bd78-c8h2z pod/web-56d6d5bd78-x5l55 evicted pod/web-56d6d5bd78-c8h2z evicted node/lab-worker drained

drain first cordons the node, which means no new Pods may land there. Then it evicts each Pod on it. The warning line shows it skipped the DaemonSet Pods (kindnet and kube-proxy). A DaemonSet would put them straight back on the same node, so evicting them is pointless, and without --ignore-daemonsets drain stops with an error instead.

ubuntu@secopslog-k8s:~/lab/deployments · Kubernetes 1.37
$ kubectl rollout status deployment/web && kubectl get nodes && kubectl get pods -l app=web -o custom-columns=POD:.metadata.name,NODE:.spec.nodeName
deployment "web" successfully rolled out NAME STATUS ROLES AGE VERSION lab-control-plane Ready control-plane 38s v1.37.0 lab-worker Ready,SchedulingDisabled <none> 27s v1.37.0 lab-worker2 Ready <none> 27s v1.37.0 POD NODE web-56d6d5bd78-82hqn lab-worker2 web-56d6d5bd78-hsnxj lab-worker2 web-56d6d5bd78-j8ghh lab-worker2 web-56d6d5bd78-kvp82 lab-worker2
$ kubectl get pods -n kube-system --field-selector spec.nodeName=lab-worker
NAME READY STATUS RESTARTS AGE kindnet-kczpg 1/1 Running 0 27s kube-proxy-9mcnv 1/1 Running 0 27s

lab-worker shows Ready,SchedulingDisabled: it still works but takes no new Pods. All four web Pods now run on lab-worker2, the evicted ones replaced by new Pods there. Only the two DaemonSet Pods remain on the drained node. On a real cluster you would now do the maintenance, reboot, and let the node take work again:

ubuntu@secopslog-k8s:~/lab/deployments · Kubernetes 1.37
$ kubectl uncordon lab-worker && kubectl get nodes && kubectl get pods -l app=web -o custom-columns=POD:.metadata.name,NODE:.spec.nodeName
node/lab-worker uncordoned NAME STATUS ROLES AGE VERSION lab-control-plane Ready control-plane 38s v1.37.0 lab-worker Ready <none> 27s v1.37.0 lab-worker2 Ready <none> 27s v1.37.0 POD NODE web-56d6d5bd78-82hqn lab-worker2 web-56d6d5bd78-hsnxj lab-worker2 web-56d6d5bd78-j8ghh lab-worker2 web-56d6d5bd78-kvp82 lab-worker2

The node is schedulable again, but the Pods did not move back. Kubernetes does not rebalance running Pods. New Pods may land on lab-worker from now on, and the existing four stay where they are until something replaces them.

Watch out
Drain respects PodDisruptionBudgets, which limit how many Pods of an app may be evicted at once. With a budget in place, drain waits instead of evicting everything together. The administration course covers them.

Hands-on

Use a cordon to steer where new Pods go.

1. Close lab-worker2 to new Pods: kubectl cordon lab-worker2

2. Delete all the web Pods: kubectl delete pods -l app=web

3. Wait for the replacements: kubectl rollout status deployment/web

4. See where they run: kubectl get pods -l app=web -o custom-columns=POD:.metadata.name,NODE:.spec.nodeName

5. Open the node again: kubectl uncordon lab-worker2, then check kubectl get nodes

6. Remove the app: kubectl delete -f web.yaml, then kubectl get deployments,replicasets,pods

What you should see: step 1 prints node/lab-worker2 cordoned and step 2 deletes four Pods. In step 4 all four new Pods are on lab-worker, because lab-worker2 was cordoned and the control-plane node does not take apps. After step 5 all three nodes show plain Ready. In step 6 deleting the Deployment also removes its ReplicaSet and Pods, and the list prints No resources found in default namespace.

Remember

A Deployment owns a ReplicaSet, which owns the Pods, tied together by ownerReferences and the pod-template-hash label. Draining a node makes its Pods go missing on purpose, and the ReplicaSet recreates them on the nodes that are still open.

Quick check
01You delete one Pod of a Deployment that wants four. Which object notices and creates the replacement?
Incorrect — A new ReplicaSet appears only when the Pod template changes. The same one stayed in charge here.
Correct — Its events listed a SuccessfulCreate for the replacement Pod.
Incorrect — The kubelet runs Pods bound to its node. It does not create new Pod objects.
Incorrect — The scheduler only assigns a node to Pods that already exist.
02You run kubectl drain lab-worker --ignore-daemonsets. Which Pods are still on lab-worker afterwards?
Incorrect — kindnet and kube-proxy were still listed on the drained node.
Incorrect — They were evicted and recreated on lab-worker2.
Incorrect — Eviction happens straight away. The Deployment Pods were evicted before the command returned.
Correct — --ignore-daemonsets leaves them, since a DaemonSet would recreate them on the node anyway.
03After kubectl uncordon lab-worker, all four web Pods are still on lab-worker2. Why?
Correct — Uncordon only allows new Pods on the node. Existing Pods stay until something replaces them.
Incorrect — kubectl get nodes showed lab-worker back to plain Ready straight away.
Incorrect — Their labels are app and pod-template-hash. Neither says anything about nodes.
Incorrect — ReplicaSets do not track nodes. The scheduler picks a node for each new Pod.

Related