Kubernetes Deployments and ReplicaSets Explained
In the pods lesson, a bare pod on a failed node stayed gone, because nothing wanted it back. A real web app needs three copies running at all times, a lost copy replaced, and new versions rolled out without an outage. This lesson explains how Kubernetes deployments, and the ReplicaSets underneath them, do all three.
ReplicaSets: keep a set number of pods running
Controllers find their pods by label. A label is a key-value pair attached to an object, such as app: web. A label selector is a query over labels, such as "every pod with app=web". A pod matches when it carries every label the selector lists.
A ReplicaSet is an object whose job is to keep a stable number of identical pods running. It holds replicas, the number of pods; a selector, which says which pods count as its own; and a pod template, a pod description as in lesson 9, copied for each new pod.
Its controller runs the control loop from lesson 8: it counts the pods matching its selector, creates more from the template if there are too few and deletes some if there are too many. Because it counts by label, a replacement with a new name counts the same. Each pod records its owner in metadata.ownerReferences.
A ReplicaSet has one gap: if you change its template, for example to a new image, running pods stay as they are, and only pods it creates later use the change. It has no rolling update of its own.
Deployments: the object you actually create
A Deployment is an object one level up that manages ReplicaSets for you. You give it the same three things, and it creates a ReplicaSet for the current pod template. When the template changes, it creates a second ReplicaSet and moves pods across gradually. It keeps the old one, so you can return to the previous version: a rollback.
So you almost always write a Deployment, not a ReplicaSet or a bare pod. Two controllers in the kube-controller-manager share the work: the Deployment controller manages ReplicaSets, and the ReplicaSet controller manages pods.
In the diagram, notice that you write only the top box. The ReplicaSet is named after the Deployment plus a hash of the pod template, and each pod after its ReplicaSet plus a random suffix. The Deployment also adds that hash as a pod-template-hash label, so each ReplicaSet claims only the pods of its own version.
You create a cluster in lesson 17, so read along for now. With web.yaml from lesson 7 applied, this lists all three layers:
kubectl get deployment,replicaset,pod
You should see something like this; your hash, suffixes and ages will differ:
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/web 3/3 3 3 2m
NAME DESIRED CURRENT READY AGE
replicaset.apps/web-7c5ddbdf54 3 3 3 2m
NAME READY STATUS RESTARTS AGE
pod/web-7c5ddbdf54-2xkqn 1/1 Running 0 2m
pod/web-7c5ddbdf54-9tq4m 1/1 Running 0 2m
pod/web-7c5ddbdf54-ldv8h 1/1 Running 0 2m
Read the names top down to see the chain of ownership. If you get No resources found in default namespace., the file was never applied, or kubectl points at another cluster or namespace.
A Deployment YAML, line by line
Here is web.yaml again. The top half describes the Deployment itself; everything under template describes the pods it should create:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27-alpine
In the diagram, notice the two pink app: web lines: one is a query and the other a stamp, and the Deployment works only because the stamp satisfies the query.
replicas is how many identical pods to keep; leave it out and you get 1. selector.matchLabels lists the labels a pod must carry to count as this Deployment's pod. template is a pod, written as in lesson 9 but without a name, because the ReplicaSet names each copy.
The template's labels must include every label in the selector, or the API rejects the Deployment with an error containing `selector` does not match template `labels`. The selector is also immutable once the Deployment exists, so editing it and applying again fails with field is immutable. To change it, you delete the Deployment and create it again.
Changing the image: a rolling update
A rolling update replaces pods a few at a time, so some copies keep serving throughout. A Deployment starts one whenever its pod template changes, and only then: changing replicas is not a rollout. Say you change the image in web.yaml to nginx:1.30-alpine, apply it and follow progress:
kubectl apply -f web.yaml
kubectl rollout status deployment/web
The second command waits until the rollout ends. This sample is trimmed to one waiting line:
deployment.apps/web configured
Waiting for deployment "web" rollout to finish: 1 out of 3 new replicas have been updated...
deployment "web" successfully rolled out
Behind those lines, the Deployment controller created a new ReplicaSet for the new template, scaled it up a pod at a time and scaled the old one down. In the diagram, notice that an old pod stops only after a new one is ready. Without a readiness check, which lesson 15 adds, a pod counts as ready once its container runs.
With 3 replicas and the default settings, Kubernetes allows one extra pod and none missing: at most four pods up, at least three ready. The settings maxSurge and maxUnavailable control this; lesson 19 tunes them and practises rollbacks. Now list the ReplicaSets:
kubectl get replicaset
You should see two; your hashes and ages will differ:
NAME DESIRED CURRENT READY AGE
web-5f8b6d7c9d 3 3 3 40s
web-7c5ddbdf54 0 0 0 12m
The old ReplicaSet stays at 0 pods with the old template inside; a rollback scales it back up. The most common mistake is a typo in the new tag: the new pods show ImagePullBackOff, and rollout status waits until the Deployment's time limit for progress runs out.
Scaling and self-healing in practice
Self-healing is the ReplicaSet's loop at work. Delete one of the pods by name, taking the name from your own kubectl get pod output, not from this page, then list the pods again:
kubectl delete pod web-5f8b6d7c9d-4kx2p
kubectl get pod
You should see something like this; older kubectl versions end the first line at deleted:
pod "web-5f8b6d7c9d-4kx2p" deleted from default namespace
NAME READY STATUS RESTARTS AGE
web-5f8b6d7c9d-8wzrt 1/1 Running 0 3s
web-5f8b6d7c9d-hv7mn 1/1 Running 0 5m
web-5f8b6d7c9d-q2c9l 1/1 Running 0 5m
The deleted pod is gone for good. The one aged 3 seconds is new, with a new name, because the ReplicaSet counted two of three; if you are quick, it shows ContainerCreating. A NotFound error means a wrong name.
So deleting pods never stops an app; scaling to 0 does. To scale is to change the number of replicas, and kubectl can do it without editing the file:
kubectl scale deployment/web --replicas=5
It prints deployment.apps/web scaled, and kubectl get replicaset soon shows 5 desired and 5 ready on the same ReplicaSet, since the template did not change. If the file still says replicas: 3, the next kubectl apply sets it back, so edit the file for a lasting change.
Check yourself
You scale web to 5 with kubectl scale, then a teammate applies web.yaml, which says replicas: 3. How many pods run, and why?
Three. kubectl apply sets every field in the file that differs from the live object, so the file's desired state wins.
You change replicas from 3 to 5 and apply, then change the image and apply. How many ReplicaSets exist after each step?
One, then two. Scaling leaves the template and its hash unchanged, so the same ReplicaSet grows; a new image changes the template, so a new ReplicaSet appears and the old one stays at 0.
You roll out a tag with a typo, and its pods never start. Why do users still get answers?
With 3 replicas and the defaults, the Deployment must keep three pods ready, so it removes an old pod only after a new one is ready. None ever is, so the old pods keep serving.