Deployments & ReplicaSets
Deployments declare the desired state of a replicated workload and manage ReplicaSets, which own the pods. Master rolling updates, scaling, rollouts, rollbacks and the self-healing loop.
What is this?
A Deployment declares the desired state of a replicated workload: how many replicas, which image and version, and the update strategy. It creates and manages ReplicaSets, and each ReplicaSet owns and reconciles a set of pods. This gives you self-healing, declarative scaling and safe rolling updates and rollbacks out of the box.
Why do you need it?
- Self-healing: if a pod dies or a node fails, the ReplicaSet creates a replacement immediately.
- Rolling updates and rollbacks are declarative โ kubectl rollout undo reverses a bad release.
- Scaling is a one-liner and every change is recorded as an auditable revision.
- Deployments are the standard controller for stateless workloads and the foundation for autoscaling.
Mental model
A project manager and team leads
Deployment, ReplicaSet and Pods
The Deployment (PM) sets the goal: three developers on the project. The ReplicaSet (team lead) hires and replaces developers to keep the team at three. Pods (developers) do the actual work.
How it works
- The Deployment creates a ReplicaSet whose only job is to keep the declared number of healthy pods running.
- The ReplicaSet owns pods through the pod template labels, which are immutable once the Deployment exists.
- A rollout creates a new ReplicaSet and scales it up while scaling the old one down, honoring maxSurge and maxUnavailable.
- The deployment controller watches pod status and immediately replaces failed pods.
- A rollback scales the previous revision ReplicaSet back up and the current one down.
The Deployment to ReplicaSet to Pod hierarchy during a rollout.
Key commands
kubectl create deployment nginx --image=nginx:1.25Create a Deployment with one replica from the nginx image.
kubectl get deployments -o wideShow desired, ready and available replica counts plus the image.
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR nginx 3/3 3 3 4m nginx nginx:1.25 app=nginx
kubectl scale deployment nginx --replicas=5Declaratively change the replica count.
kubectl set image deployment/nginx nginx=nginx:1.26Trigger a rolling update by changing the container image.
kubectl rollout status deployment/nginxBlock until the current rollout completes or fails.
kubectl rollout undo deployment/nginxRoll back to the previous revision.
kubectl rollout history deployment/nginxList the deployment revision history.
Examples
A Deployment with a rolling update strategy
apiVersion: apps/v1kind: Deploymentmetadata: name: nginxspec: replicas: 3 selector: matchLabels: app: nginx strategy: type: RollingUpdate rollingUpdate: maxSurge: 1 maxUnavailable: 0 template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.25 ports: - containerPort: 80The Deployment wants 3 nginx pods at all times. maxUnavailable: 0 and maxSurge: 1 mean a rollout adds one new pod before removing any old pod, so there is no capacity dip. The selector matches the template labels, which is what ties the Deployment to its ReplicaSets.
In production
- GitOps applies the same deployment manifest everywhere; version bumps are just manifest updates.
- Tuning maxUnavailable and maxSurge controls how fast a release rolls out and how much capacity dips.
- The HorizontalPodAutoscaler scales deployment replicas based on CPU or custom metrics.
- Rollout history makes every image change auditable and reversible with one command.
Best practices
- Use kubectl apply with versioned manifests from git; never hand-edit live objects.
- Pin image versions and avoid :latest so rollouts are reproducible.
- Set readiness probes so new pods only receive traffic when actually ready.
- Keep revisionHistoryLimit modest (for example 10) to bound etcd usage.
- Use one deployment per workload per environment with clean label selectors.
Common mistakes
- Changing the pod template selector after creation โ it is immutable and the Deployment will error.
- Reusing the same labels for template and selector so ReplicaSets overlap and fight.
- Rolling out with broken readiness probes, so maxUnavailable waits forever.
- Forgetting that plain kubectl run creates a Deployment, surprising users who wanted a pod.
Common issues
Pods stuck in Pending, 0/N nodes available: Insufficient cpuThe cluster has no node with enough allocatable CPU for the replica requests.
Scale down, raise node capacity, or lower the CPU requests on the container.
Invalid value: ... spec.selector does not match template labelsThe selector and template labels diverged, and selectors are immutable.
The selector cannot be edited in place; recreate the Deployment with matching labels.
Rollout stuck: progress deadline exceededNew pods never become ready, often due to image pull failures or failing readiness probes.
Check kubectl get pods and kubectl describe pod for ImagePullBackOff or probe failures, fix the cause, then kubectl rollout status again.
Debugging tips
- kubectl describe deployment <name> shows ReplicaSet events and rollout errors.
- kubectl rollout status deployment/<name> reports rollout progress and failures.
- kubectl get rs shows the old and new ReplicaSets side by side during a rollout.
- kubectl rollout undo deployment/<name> is the fastest first move after a bad release.
Interview checkpoint
Hands-on lab
- Create and scale a Deployment
- Verify self-healing behavior
- Trigger and watch a rolling update
- Roll back to a previous image revision
Scaling, Rolling Updates and Rollbacks
35 min ยท intermediate
Quick quiz
What does a Deployment use to manage its pod replicas?
Deleting a pod owned by a Deployment results in a replacement pod being created.
Which command rolls a Deployment back to its previous revision?
Cheat sheet
kubectl create deployment app --image=imgCreate a Deploymentkubectl get deployment -o wideStatus and imagekubectl scale deployment app --replicas=5Change replica countkubectl set image deployment/app c=img:v2Update container imagekubectl rollout status deployment/appWait for rolloutkubectl rollout undo deployment/appRoll back a releasekubectl rollout history deployment/appList revisions