Deployments and ReplicaSets
Keep copies running, even when a node goes.
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: 2a5b1a13d1f666a816441afa0fdededcad037d11eba0cffdacf94f21658b3006The 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
apiVersion: apps/v1kind: Deploymentmetadata:name: webspec:replicas: 4selector:matchLabels:app: webtemplate:metadata:labels:app: webspec:containers:- name: nginximage: 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.
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:
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
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:
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:
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.
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:
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.
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.
SuccessfulCreate for the replacement Pod.kubectl drain lab-worker --ignore-daemonsets. Which Pods are still on lab-worker afterwards?kindnet and kube-proxy were still listed on the drained node.lab-worker2.--ignore-daemonsets leaves them, since a DaemonSet would recreate them on the node anyway.kubectl uncordon lab-worker, all four web Pods are still on lab-worker2. Why?kubectl get nodes showed lab-worker back to plain Ready straight away.app and pod-template-hash. Neither says anything about nodes.