Pods, ReplicaSets, and Deployments
How Kubernetes layers Pods, ReplicaSets, and Deployments to run stateless workloads and perform zero-downtime rollouts and rollbacks.
TL;DR
A Pod runs one or more tightly coupled containers, a ReplicaSet keeps a desired count of identical Pods alive, and a Deployment manages ReplicaSets to give you declarative rollouts and instant rollbacks.
Why it exists
A single container process cannot heal itself if the node it runs on fails, and replacing Pods one by one by hand during a release causes downtime. Kubernetes separates the runtime unit (Pod), the replica count controller (ReplicaSet), and the release controller (Deployment) so each layer has one clear job.
Mental model
Think of the three objects as a nested ownership chain:
- Pod — the smallest deployable unit in Kubernetes. It gives its containers a shared network namespace (one Pod IP) and shared storage volumes. Pods are mortal: if a Pod dies, it is not restarted in place as a new object; a controller creates a replacement Pod.
- ReplicaSet — watches a label selector (
matchLabels) and reconciles the live Pod count to matchspec.replicas. If a node vanishes, the ReplicaSet controller creates new Pods on healthy nodes. - Deployment — sits above ReplicaSets. When you change
spec.template(for example, bumpingimagefrom1.27to1.28), the Deployment creates a new ReplicaSet, shifts replicas from the old ReplicaSet to the new one according tostrategy.rollingUpdate(maxSurge,maxUnavailable), and retains old ReplicaSets at0replicas so you can roll back.
Do it
Generate a minimal Deployment manifest locally with --dry-run=client (verified with kubectl v1.34.1):
kubectl create deployment nginx --image=nginx:1.27 --replicas=3 --dry-run=client -o yamlapiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx
name: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
strategy: {}
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx:1.27
name: nginxApply, roll out an image update, and roll back on a live cluster:
# not run (requires cluster)
kubectl apply -f deployment.yaml
kubectl set image deployment/nginx nginx=nginx:1.28
kubectl rollout status deployment/nginx
kubectl rollout history deployment/nginx
kubectl rollout undo deployment/nginxGotchas
- Mismatched
selector.matchLabelsandtemplate.metadata.labels: The API server rejects a Deployment if its selector does not match the Pod template labels. Never edit selector labels in place on a running Deployment. - Unhealthy Pods marked Ready immediately: Without a
readinessProbe, Kubernetes considers a new Pod ready as soon as its container starts, which can send traffic to a booting app during a rollout. - Zero-history
revisionHistoryLimit: Settingspec.revisionHistoryLimit: 0deletes old ReplicaSets immediately, which disableskubectl rollout undo.
Production checklist
- Run stateless workloads via
Deployment(never barePodor standaloneReplicaSet). - Set
readinessProbeandlivenessProbeso rolling updates wait for new Pods to be ready. - Define
resources.requestsandresources.limitson every container. - Keep
revisionHistoryLimitat a positive number (default10) for fast rollbacks.
Check yourself
Check Yourself
1. What happens when you update the container image in a Kubernetes Deployment?
2. Which resource is directly responsible for ensuring 3 identical Pod replicas stay running?
Sources
Related Notes
What Happens on kubectl apply
Trace how kubectl apply flows through the Kubernetes API Server, etcd, controllers, kube-scheduler, kubelet, and the container runtime.
Kubernetes Probes: Liveness, Readiness, Startup
How livenessProbe, readinessProbe, and startupProbe differ, what each failure triggers, and how failing readiness removes a Pod from EndpointSlices.