Deployments and ReplicaSets

Intermediate · 5 min

Why a Deployment manages ReplicaSets rather than Pods, how pod-template-hash ties them together, revision history, scaling, and when to touch a ReplicaSet.

Before thisLabels, selectors, annotations and namespacesControllers and the reconcile loop

Containers from the Couch · 5 min

Deployments and ReplicaSets

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Containers from the CouchReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A ReplicaSet does one job: keep N Pods matching its selector alive. It cannot change those Pods; a new template only affects Pods created later.
  • A Deployment manages versions by managing ReplicaSets. Each distinct Pod template gets its own ReplicaSet, named and labelled with a pod-template-hash.
  • Changing anything in spec.template starts a rollout to a new ReplicaSet. Changing replicas, labels or annotations on the Deployment does not.
  • Old ReplicaSets are kept at zero replicas as revision history, up to revisionHistoryLimit (10 by default). Going back to an old template reuses its ReplicaSet.
  • You almost never create a ReplicaSet yourself. If you delete one a Deployment owns, the Deployment simply recreates it.

Two jobs, two controllers

Running a stateless service has two separate problems. The first is keeping enough copies alive: if a Pod dies or its node vanishes, start another. The second is changing the version: move from image 1.0.0 to 1.0.1 without dropping traffic, and be able to go back.

Kubernetes splits these between two controllers. A ReplicaSet solves the first and nothing else. A Deployment solves the second by creating and scaling ReplicaSets. The devops survey showed that you write Deployments; this lesson shows what they do underneath.

A ReplicaSet counts, and that is all

A ReplicaSet has three things: a replicas count, a label selector, and a Pod template. Its loop is the one from the controllers lesson: count Pods that match the selector, create from the template if there are too few, delete if there are too many. The labels lesson showed it adopting a stray Pod that happened to match.

What a ReplicaSet never does is change a Pod. Edit its template to a new image and the existing Pods keep the old one; only Pods created afterwards get the new image. A ReplicaSet is a vending machine refiller: it keeps the slots full, but it won’t swap the cola already in the machine for the new recipe.

A Deployment manages versions as ReplicaSets

Here is a Deployment from the lab:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: catalog
spec:
  replicas: 3
  revisionHistoryLimit: 2
  selector:
    matchLabels: {app: catalog}
  template:
    metadata:
      labels: {app: catalog}
    spec:
      containers:
      - name: catalog
        image: ghcr.io/example/catalog:1.0.0
        resources: {requests: {cpu: 100m, memory: 64Mi}}

Applied, it produced one ReplicaSet and three Pods:

NAME                                 DESIRED   CURRENT   READY   IMAGES                          SELECTOR
replicaset.apps/catalog-7b74d576f9   3         3         0       ghcr.io/example/catalog:1.0.0   app=catalog,pod-template-hash=7b74...

catalog-7b74d576f9-42w9s   0/1   Pending   app=catalog,pod-template-hash=7b74d576f9,topology.kubernetes.io/zone=a
catalog-7b74d576f9-jc6bd   0/1   Pending   app=catalog,pod-template-hash=7b74d576f9,topology.kubernetes.io/zone=a
catalog-7b74d576f9-jr7mb   0/1   Pending   app=catalog,pod-template-hash=7b74d576f9,topology.kubernetes.io/zone=a

The Deployment controller hashed the Pod template and added the result, pod-template-hash=7b74d576f9, to the ReplicaSet’s name, its selector, its template and so to every Pod. That extra label is what keeps two ReplicaSets of the same Deployment from adopting each other’s Pods, since both would otherwise match app=catalog. (The topology.kubernetes.io/zone label was copied from the node when the Pod was bound, by the PodTopologyLabels admission plugin, which is on by default in v1.37.)

The API server also filled in defaults you didn’t write: progressDeadlineSeconds: 600, and a RollingUpdate strategy with maxSurge: 25% and maxUnavailable: 25%. The next lesson is about those.

A template change is a new ReplicaSet

The image was then changed three times, to 1.0.1, 1.0.2 and 1.0.3. Each change produced a new template hash and so a new ReplicaSet, and the Deployment moved the Pods across. The lab nodes run no containers, so to let each rollout finish, the new Pods’ status was written by hand as Running and Ready, playing the kubelet. Afterwards:

NAME                 REVISION   DESIRED   IMAGE
catalog-5c95b995bf   3          0         ghcr.io/example/catalog:1.0.2
catalog-6b8b988964   4          3         ghcr.io/example/catalog:1.0.3
catalog-8654fc956b   2          0         ghcr.io/example/catalog:1.0.1

Three things to read from that:

  • Each ReplicaSet carries a deployment.kubernetes.io/revision annotation. kubectl rollout history lists those revisions.
  • Old ReplicaSets stay, scaled to zero. That is the whole mechanism behind rollback: going back means scaling an old ReplicaSet up again.
  • Revision 1, the 1.0.0 ReplicaSet, is gone. With revisionHistoryLimit: 2, the controller keeps two old ReplicaSets and deletes older ones. The default is 10.

Setting the image back to 1.0.1 did not create a fourth ReplicaSet. The template hashed to the same value as revision 2, so the Deployment reused catalog-8654fc956b and relabelled it revision 5.

What counts as a template change: anything under spec.template, including its labels, annotations, env vars and resources. Scaling, or changing the Deployment’s own labels and annotations, is not a new version and starts no rollout. kubectl rollout restart uses this on purpose: it writes a kubectl.kubernetes.io/restartedAt annotation into the template, which changes the hash and replaces every Pod.

Scaling and ownership

kubectl scale deploy catalog --replicas=5 changes the Deployment, and the controller passes the new count down to its ReplicaSets. If a rollout is in progress, it spreads extra replicas across the old and new ReplicaSets in proportion to their sizes rather than giving them all to one, so a scale-up mid-rollout doesn’t jump ahead of the rollout’s pace.

ReplicaSets belong to the Deployment through ownerReferences. Deleting the active ReplicaSet in the lab achieved nothing lasting: within a few seconds the Deployment controller created a new one with the same name, because the template still hashed to the same value. Deleting the Deployment, by contrast, removes its ReplicaSets and their Pods through the garbage collector.

When to use a ReplicaSet directly

Practically never. A bare ReplicaSet gives you self-healing without any update story. The only common reason to look at ReplicaSets is debugging: kubectl get rs shows which versions exist and how many Pods each holds, which is often the fastest way to see that a rollout is stuck halfway. Its predecessor, the ReplicationController, still exists in the core API but is legacy; it supports only equality-based selectors.

About the video

The embed is Containers from the Couch’s “Kubernetes Pods, ReplicaSets, and Deployments in 5 Minutes” (May 2022), by AWS developer advocates. Its chapters cover an intro to Pods, Pod configuration, ReplicaSets and Deployments, and the controller loop, with real YAML. It is an introduction: it does not go into pod-template-hash, revision history or proportional scaling, which this lesson adds. English was judged from the “English (US) original” audio track. Nick True’s video in deeper covers ReplicaSet selectors and why Deployments are preferred, with a short demo. A popular course video on the same topic was dropped because its owner disallows embedding.

What was run: the Deployment, its defaults, the ReplicaSets and their hashes, the revision pruning, the reuse of an old ReplicaSet, the recreated ReplicaSet and the scale-up all ran against a local v1.37.3 control plane on 2026-11-28. Its nodes run no containers, so Pod readiness during the rollouts was written into Pod status by hand; everything the Deployment and ReplicaSet controllers did in response was real.

Where this fits

Next, how a Deployment actually moves Pods from one ReplicaSet to the other, and how to undo it: rolling updates and rollbacks.

Check yourself

  1. 01You edit a ReplicaSet's template to use a new image. What happens to its running Pods?

    Nothing. A ReplicaSet only counts Pods; it never updates them. New Pods it creates later use the new template, so you end up with a mix. Replacing Pods safely is exactly what a Deployment adds.

  2. 02You add an annotation to a Deployment's metadata, then add one to its Pod template. Which change starts a rollout?

    Only the Pod template change. The Deployment hashes spec.template; a different hash means a new ReplicaSet and a rolling replacement of every Pod. That is also how kubectl rollout restart works: it sets a timestamp annotation in the template.

  3. 03Why does kubectl get rs show several ReplicaSets with DESIRED 0 next to a healthy Deployment?

    They are the revision history: the ReplicaSets for previous templates, scaled to zero. They let kubectl rollout undo scale an old version back up. revisionHistoryLimit controls how many are kept.

  4. 04What is pod-template-hash for?

    It is a label the Deployment controller adds to each ReplicaSet's selector, template and Pods, computed from the template. It keeps ReplicaSets of the same Deployment from claiming each other's Pods, since they would otherwise all match app=catalog.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark