When you run an application on Kubernetes, you almost never create pods directly. Instead you declare a desired state — “run three copies of my web server” — and Kubernetes works continuously to make reality match. The two controllers behind this behavior are the Kubernetes Deployment and ReplicaSet. Understanding how a Kubernetes deployment and ReplicaSet cooperate is essential, because nearly every workload you ship, from a simple NGINX site to a large API, is managed by them. This article covers Kubernetes Deployments and ReplicaSets Explained for in detail.
This article explains what each object does, how they relate to pods, and walks through creating, updating, scaling, and rolling back a deployment with real YAML you can apply to any cluster.
What Is a Kubernetes ReplicaSet? – Kubernetes Deployments and ReplicaSets Explained for
A ReplicaSet guarantees that a specified number of identical pods are running at all times. If a pod is deleted or its node fails, the ReplicaSet notices the count drop and starts a replacement. Its definition has three key parts:
- replicas — how many copies should exist.
- selector — which pods belong to this set, matched by labels.
- template — the pod specification used when creating new pods.
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: web-rs
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.27
ports:
- containerPort: 80
You can apply this directly, but in practice you rarely do. A ReplicaSet keeps a fixed pod template: to change the image you must edit the set and manually delete old pods. That limitation is exactly why Deployments exist.
What Is a Kubernetes Deployment and ReplicaSet Relationship? – Kubernetes Deployments and ReplicaSets Explained for
A Deployment sits one level above ReplicaSets. You describe the application once, and the Deployment creates and manages ReplicaSets for you. Its superpower is rolling updates: when you change the pod template, the Deployment spins up a new ReplicaSet with the updated template, gradually shifts pods from old to new, and keeps the old ReplicaSet (scaled to zero) so you can roll back instantly.
The relationship is a chain: Deployment → ReplicaSet → Pods. Each rollout creates a new ReplicaSet revision. That is why, after a few updates, kubectl get rs shows several ReplicaSets with 0 desired replicas — they are your rollback history.
Create a Deployment: A Kubernetes Deployment and ReplicaSet Example
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
labels:
app: web
spec:
replicas: 3
selector:
matchLabels:
app: web
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.25
ports:
- containerPort: 80
resources:
requests:
cpu: "100m"
memory: "128Mi"
limits:
cpu: "250m"
memory: "256Mi"
Apply it and watch the objects appear:
kubectl apply -f web-deployment.yaml
kubectl get deployments
kubectl get rs # the ReplicaSet the Deployment created
kubectl get pods # the pods the ReplicaSet created
The strategy block controls the rollout: maxSurge: 1 allows one extra pod during the update, and maxUnavailable: 0 means Kubernetes never drops below three available pods. For background on each command, see our kubectl commands cheat sheet.
Update, Scale, and Roll Back
Rolling update
Change the image in the YAML (for example to nginx:1.27) and apply again, or use a one-liner:
kubectl set image deployment/web nginx=nginx:1.27
kubectl rollout status deployment/web
Kubernetes replaces pods one at a time. If the new version is broken, traffic keeps flowing to the healthy old pods.
Scaling
kubectl scale deployment web --replicas=6
kubectl get pods -w
The Deployment adjusts the active ReplicaSet’s replica count, and new pods are scheduled immediately.
Rollback
kubectl rollout history deployment/web
kubectl rollout undo deployment/web
# or jump to a specific revision:
kubectl rollout undo deployment/web --to-revision=1
Rollback works because the old ReplicaSet was kept. The Deployment simply scales it back up and scales the bad one down.
Deployment vs ReplicaSet vs Pod: When to Use What
- Pod — only for one-off debugging or experiments. A deleted pod is gone forever.
- ReplicaSet — almost never directly. Use it only if you need a fixed set of pods with no update strategy.
- Deployment — the default choice for any stateless application: web servers, APIs, frontends, workers.
For stateful applications (databases, queues) you would use a StatefulSet instead, and for a pod on every node a DaemonSet — both build on the same controller ideas.
Common Problems and Troubleshooting
- Pods never appear: the Deployment’s
selectordoes not match the template labels. The API server rejects a mismatch at apply time, but a selector that matches other pods causes adoption chaos — always use unique labels per app. - Rollout stuck: run
kubectl rollout statusandkubectl describe deployment web. Usually the new pods fail to start (bad image name, missing ConfigMap, insufficient resources). - ImagePullBackOff: the image cannot be downloaded. Check the exact error with
kubectl describe pod. - Old ReplicaSets piling up: by default the last 10 revisions are kept (
revisionHistoryLimit). Lower it if your namespace gets cluttered. - Pods crash after update: read the logs with
kubectl logs— our guide to troubleshooting CrashLoopBackOff covers the workflow.
Conclusion
The Kubernetes Deployment and ReplicaSet pair is the workhorse of the platform: the ReplicaSet keeps the right number of pods alive, and the Deployment adds versioned, reversible updates on top. Declare your application in a Deployment, let it manage ReplicaSets, and you get self-healing, scaling, and safe rollouts for free. For exact field references and advanced strategies such as canary releases, consult the official Kubernetes Deployment documentation.