What Is a Kubernetes Deployment and Why Do You Need One?
A Deployment is a Kubernetes resource that declaratively manages a set of identical Pods through ReplicaSets. It handles rollouts, rollbacks, and scaling so you never have to manage individual Pods by hand.
Detailed Answer
A Deployment is a first-class Kubernetes resource that provides declarative updates for Pods and ReplicaSets. Instead of manually creating Pods or writing scripts to replace them, you describe the desired state in a Deployment manifest and let Kubernetes do the rest.
The Resource Hierarchy
Understanding where a Deployment sits in the Kubernetes object model is critical:
Deployment
└── ReplicaSet (managed automatically)
└── Pod (managed automatically)
└── Container(s)
You create and modify the Deployment. Kubernetes creates and manages everything below it.
A Minimal Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-frontend
labels:
app: frontend
spec:
replicas: 3
selector:
matchLabels:
app: frontend
template:
metadata:
labels:
app: frontend
spec:
containers:
- name: nginx
image: nginx:1.25
ports:
- containerPort: 80
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 250m
memory: 256Mi
When you apply this manifest, Kubernetes performs the following steps:
- The Deployment controller creates a ReplicaSet with the Pod template you specified.
- The ReplicaSet creates 3 Pods (matching
replicas: 3). - The scheduler assigns each Pod to a node.
- The kubelet on each node pulls the
nginx:1.25image and starts the container.
What a Deployment Gives You
| Capability | Description |
|---|---|
| Self-healing | If a Pod crashes or a node fails, the ReplicaSet creates a replacement Pod automatically. |
| Rolling updates | When you change the image tag or any part of the Pod template, the Deployment orchestrates a zero-downtime rollout. |
| Rollback | Every update creates a new ReplicaSet revision. You can instantly revert to any previous revision. |
| Scaling | Change replicas and Kubernetes adds or removes Pods to match. |
| Pause and resume | You can pause a rollout to make multiple changes, then resume to trigger a single update. |
Creating and Inspecting a Deployment
# Create the Deployment
kubectl apply -f deployment.yaml
# List Deployments
kubectl get deployments
# See detailed status including conditions and ReplicaSet info
kubectl describe deployment web-frontend
# View the Pods it manages
kubectl get pods -l app=frontend
# View the underlying ReplicaSet
kubectl get replicasets -l app=frontend
The Selector Must Match the Template
A common mistake is having a selector.matchLabels that does not match template.metadata.labels. Kubernetes will reject the manifest:
# This will FAIL validation
spec:
selector:
matchLabels:
app: frontend # <-- selector
template:
metadata:
labels:
app: backend # <-- mismatch!
The selector is immutable after creation. If you need to change it, you must delete and recreate the Deployment.
Updating a Deployment
Any change to the Pod template (.spec.template) triggers a new rollout:
# Update the image
kubectl set image deployment/web-frontend nginx=nginx:1.26
# Or edit the manifest and re-apply
kubectl apply -f deployment.yaml
Changes outside the Pod template, such as modifying replicas, do not trigger a rollout. They are applied immediately.
When Not to Use a Deployment
Deployments are designed for stateless workloads. If your application requires:
- Stable network identities or persistent storage per Pod -- use a StatefulSet.
- A Pod on every node (log collectors, monitoring agents) -- use a DaemonSet.
- Run-to-completion tasks -- use a Job or CronJob.
Real-World Best Practices
- Always set resource requests and limits so the scheduler can make informed decisions.
- Use liveness and readiness probes so Kubernetes knows when your Pods are healthy.
- Pin image tags rather than using
latestto ensure reproducible deployments. - Set
revisionHistoryLimitto control how many old ReplicaSets are retained for rollback.
spec:
revisionHistoryLimit: 5
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
This configuration keeps 5 old ReplicaSets for rollback and ensures zero unavailability during updates.
Summary
A Kubernetes Deployment is the go-to resource for running stateless applications. It wraps ReplicaSets and Pods in a declarative layer that handles rollouts, rollbacks, scaling, and self-healing. Understanding Deployments is the foundation for nearly every Kubernetes topic that follows.
Why Interviewers Ask This
This is a foundational question that tells interviewers whether you understand how Kubernetes manages stateless workloads. It separates candidates who have used kubectl from those who understand the resource hierarchy.
Common Follow-Up Questions
Key Takeaways
- Deployments are the standard way to run stateless applications in Kubernetes.
- They create and manage ReplicaSets, which in turn manage Pods.
- Declarative updates let Kubernetes handle the rollout process automatically.