Deployments & ReplicaSets

Kubernetes Fundamentals

Chapter 5 · Deployments & ReplicaSets

Chapters 3 and 4 both pointed here: the controller you'll actually use almost all the time, rather than bare Pods. This chapter covers the Deployment, and the ReplicaSet mechanism underneath it.

The Problem With Bare Pods, Revisited

A bare Pod, once created, isn't automatically replaced if it fails or its node dies — unless something is actively watching it. You could theoretically write your own reconciliation logic by hand; Kubernetes already provides it, via Deployments.

ReplicaSets — Ensuring N Copies Are Always Running

The layer directly underneath Deployments. A ReplicaSet's job is simple and singular: ensure a specified number of pod replicas matching a given template are running at all times — a concrete instance of Chapter 2's reconciliation loop (observe how many matching pods exist, compare to the desired count, create or delete pods to reconcile). ReplicaSets use label selectors (Chapter 4's labels material) to know which pods belong to them — a genuinely important mechanism that explains a lot of later behavior.

Deployments — Managing ReplicaSets for You

A Deployment sits one layer above a ReplicaSet: you rarely create a ReplicaSet directly — you create a Deployment, and Kubernetes creates and manages the ReplicaSet(s) underneath it automatically. A Deployment's real value is managing the transition between different versions of a ReplicaSet — exactly what makes rolling updates and rollback possible.

Rolling Updates — Changing Versions Without Downtime

When a Deployment's pod template changes (a new container image version, for instance), Kubernetes creates a new ReplicaSet with the updated template, and gradually scales it up while scaling the old ReplicaSet down — a few pods at a time, never all at once. This is why routine deployment updates generally don't cause downtime: healthy pods are always serving traffic throughout the transition.

Two practical, configurable knobs: maxSurge (how many extra pods beyond the desired count can be created during rollout) and maxUnavailable (how many can be unavailable at once).

Rollback — Undoing a Bad Deployment

Kubernetes keeps a history of previous ReplicaSets/revisions for a Deployment. If a new rollout turns out to be broken, kubectl rollout undo reverts to the previous working ReplicaSet — genuinely fast compared to manually reconstructing the old configuration by hand. kubectl rollout status and kubectl rollout history are the practical commands for checking on this.

Self-Healing in Action

Tying together Chapter 3's "pods are disposable" material concretely: if a pod managed by a ReplicaSet dies or is deleted, the ReplicaSet's own reconciliation loop notices the actual count has dropped below the desired count, and creates a replacement automatically — the concrete mechanism behind the self-healing Chapter 1 promised. This happens without writing any recovery logic at all, simply by declaring the desired replica count.

A Complete Deployment YAML, Explained

apiVersion: apps/v1 kind: Deployment metadata: name: web-deployment spec: replicas: 3 selector: matchLabels: app: demo template: metadata: labels: app: demo spec: containers: - name: web image: nginx:latest ports: - containerPort: 80
spec.template IS a pod spec
Everything under spec.template is structurally the same Pod YAML from Chapter 4, just nested inside a Deployment. Nothing about Pod YAML needs relearning here — spec.selector.matchLabels is what ties the Deployment to the ReplicaSet's own label-selector mechanism above, and spec.replicas is the desired count that reconciliation loop maintains.
apps/v1, not v1 — a genuinely common early mistake
Chapter 4's Pod manifest used apiVersion: v1. Deployments live under a different API group entirely — apps/v1. Using the wrong apiVersion for a given kind is a genuinely common early mistake worth watching for directly.

Scaling — Changing Replica Count

Imperatively: kubectl scale deployment <name> --replicas=N. Declaratively (preferred, per Chapter 4's own guidance): edit the YAML's replicas field and re-apply. This chapter covers manual scaling; Course 2's k8s2-6 covers automatic scaling based on live metrics.

Hands-On Exercises

Exercise 1

Explain the relationship between a Deployment, a ReplicaSet, and a Pod — specifically which one manages which — and explain why you'd create a Deployment rather than a ReplicaSet or a bare Pod directly.

📄 View solution
Exercise 2

Explain how a rolling update avoids downtime, referencing maxSurge/maxUnavailable and the transition between the old and new ReplicaSets.

📄 View solution
Exercise 3

A pod managed by a Deployment with replicas: 3 is accidentally deleted by a user running kubectl delete pod directly. Explain exactly what happens next, and why.

📄 View solution

Chapter 5 Quick Reference

  • ReplicaSet — ensures N pods matching a template exist, via label selectors and the reconciliation loop
  • Deployment — manages ReplicaSets for you; its real value is managing the transition between versions
  • Rolling updates — new ReplicaSet scales up while old scales down gradually; maxSurge/maxUnavailable control the pace
  • Rollbackkubectl rollout undo reverts to a previous ReplicaSet revision fast
  • Self-healing — a deleted/failed pod is automatically replaced, with zero custom recovery code, purely from declaring a desired replica count
  • spec.template is structurally a full Pod spec — Chapter 4's YAML knowledge transfers directly
  • Deployments use apps/v1, not v1 — a common early mistake
  • Next chapter: Services & Networking Basics — ClusterIP/NodePort/LoadBalancer, service discovery, in-cluster DNS