Deployments & ReplicaSets
Run scalable, self-healing, rolling-updated workloads with Deployments.
A Deployment manages a stateless app declaratively. It creates a ReplicaSet, which keeps the desired number of identical Pods running — recreating any that die. You change the Deployment (image, replicas) and Kubernetes reconciles.
Its headline feature is the rolling update: it gradually replaces old Pods with
new ones, respecting maxSurge/maxUnavailable, so there’s no downtime. If a new
version misbehaves, kubectl rollout undo rolls back to the previous ReplicaSet.
apiVersion: apps/v1
kind: Deployment
metadata: { name: web }
spec:
replicas: 3
selector: { matchLabels: { app: web } }
template:
metadata: { labels: { app: web } }
spec:
containers:
- name: web
image: myapp:1.4.2kubectl apply -f deploy.yaml
kubectl scale deploy/web --replicas=5
kubectl set image deploy/web web=myapp:1.5.0 # triggers a rolling update
kubectl rollout status deploy/web
kubectl rollout undo deploy/web # roll back - Write a Deployment with 3 replicas of an app.
- Scale it to 5 and watch the new Pods appear.
- Update the image and observe the rolling update, then roll it back.
- Explain the relationship between a Deployment and a ReplicaSet.
Cheat Sheet▾
| Task | Command |
|---|---|
| Apply | kubectl apply -f deploy.yaml |
| Scale | kubectl scale deploy/x --replicas=N |
| Update image | kubectl set image deploy/x c=img |
| Rollout status | kubectl rollout status deploy/x |
| Roll back | kubectl rollout undo deploy/x |
| History | kubectl rollout history deploy/x |
Common Interview Questions▾
What does a Deployment do?
It declaratively manages a stateless app via a ReplicaSet — maintaining a desired replica count, self-healing dead Pods, and performing rolling updates and rollbacks.
How does a rolling update work?
Kubernetes incrementally replaces old Pods with new ones, bounded by maxSurge and maxUnavailable, so capacity stays up and there’s no downtime; failures can be rolled back to the prior ReplicaSet.
Deployment vs StatefulSet?
Deployment is for stateless, interchangeable Pods; StatefulSet gives stable network identities and persistent per-Pod storage for stateful apps like databases.