Deployments
Deployments are the most common way to manage stateless applications in Kubernetes. They provide declarative updates, rolling deployments, rollback capabilities, and automatic pod replacement. Think of a Deployment as a manager that ensures your application always has the right number of pods running with the correct configuration.
What Are Deployments?
A Deployment manages a ReplicaSet, which in turn manages a set of pods. When you update a Deployment—changing the container image, environment variables, or resource limits—the Deployment automatically performs a rolling update, gradually replacing old pods with new ones without downtime.
Why Use Deployments?
Deployments provide several key benefits:
✅ Declarative updates - Describe desired state, Kubernetes makes it happen
✅ Rolling updates - Update pods gradually without downtime
✅ Automatic rollback - Revert to previous version if something goes wrong
✅ Self-healing - Automatically replaces failed pods
✅ Scaling - Easily scale up or down by changing replica count
✅ Status tracking - Monitor rollout progress and health
How Deployments Work
Deployments use ReplicaSets to manage pod replicas. When you create a Deployment, it creates a ReplicaSet that ensures the desired number of pods are running. When you update the Deployment, it creates a new ReplicaSet and performs a rolling update.
Basic Deployment Example
Here’s a simple Deployment that runs an nginx web server:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.21
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
Key fields:
replicas- Number of pod replicas to maintainselector- Labels that identify which pods this Deployment managestemplate- Pod template used to create new podstemplate.metadata.labels- Must match the selector labels
Deployment Lifecycle
A Deployment goes through several states during its lifecycle:
You can check the status with:
kubectl get deployment nginx-deployment
kubectl describe deployment nginx-deployment
kubectl rollout status deployment nginx-deployment
Updating Deployments
Deployments support two update strategies:
RollingUpdate (Default)
Gradually replaces old pods with new ones. You can control the update process:
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1 # Can have 1 extra pod during update
maxUnavailable: 0 # All pods must be available
replicas: 3
During a rolling update:
- New pods are created gradually
- Old pods are terminated after new ones are ready
- Service remains available throughout
Recreate
Terminates all old pods before creating new ones. Causes brief downtime:
spec:
strategy:
type: Recreate
Use Recreate when:
- Your application doesn’t support running multiple versions
- You need to ensure only one version runs at a time
- Brief downtime is acceptable
Rolling Updates in Action
Rollback
If an update causes problems, you can rollback to a previous version:
# View rollout history
kubectl rollout history deployment nginx-deployment
# Rollback to previous version
kubectl rollout undo deployment nginx-deployment
# Rollback to specific revision
kubectl rollout undo deployment nginx-deployment --to-revision=2
# View rollback status
kubectl rollout status deployment nginx-deployment
Scaling Deployments
Scale a Deployment by changing the replica count:
# Scale to 5 replicas
kubectl scale deployment nginx-deployment --replicas=5
# Or edit the Deployment
kubectl edit deployment nginx-deployment
You can also enable autoscaling:
kubectl autoscale deployment nginx-deployment --min=3 --max=10 --cpu-percent=80
Deployment vs ReplicaSet
While ReplicaSets manage pod replicas, Deployments add:
- Rolling updates - Gradual pod replacement
- Rollback - Revert to previous versions
- History - Track changes and revisions
- Pause/Resume - Control rollout process
You typically use Deployments rather than ReplicaSets directly, as Deployments provide a higher-level abstraction that includes update management.
When to Use Deployments
Use Deployments for:
✅ Stateless applications - Web servers, APIs, microservices
✅ Applications needing updates - Rolling updates without downtime
✅ Standard workloads - Most containerized applications
✅ Applications that scale horizontally - Multiple identical pods
✅ Applications without stable identity - Pods are interchangeable
Don’t use Deployments for:
❌ Stateful applications - Use StatefulSets instead
❌ One-time tasks - Use Jobs
❌ Scheduled tasks - Use CronJobs
❌ One pod per node - Use DaemonSets
Best Practices
Always specify resource requests and limits - Helps scheduler make better placement decisions
Use health probes - Liveness and readiness probes ensure pods are healthy
containers:
- name: nginx
image: nginx:1.21
livenessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 5
periodSeconds: 5
Match labels correctly - Ensure
selector.matchLabelsmatchestemplate.metadata.labelsUse appropriate update strategy - RollingUpdate for zero-downtime, Recreate when necessary
Set maxUnavailable carefully - Ensure service availability during updates
Version your images - Use specific tags, not
latest, for predictable deploymentsMonitor rollouts - Use
kubectl rollout statusto monitor update progressTest rollbacks - Verify you can rollback before deploying to production
Use namespaces - Organize Deployments with namespaces
Document changes - Use annotations to document why changes were made
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
description: "Initial deployment"
Common Operations
View Deployment Status
kubectl get deployments
kubectl describe deployment nginx-deployment
kubectl get rs -l app=nginx # View ReplicaSets
kubectl get pods -l app=nginx # View Pods
Pause and Resume
# Pause a rollout
kubectl rollout pause deployment nginx-deployment
# Make multiple changes while paused
kubectl set image deployment nginx-deployment nginx=nginx:1.22
kubectl set resources deployment nginx-deployment -c nginx --limits=cpu=1000m
# Resume the rollout
kubectl rollout resume deployment nginx-deployment
Delete Deployment
# Delete Deployment (pods are also deleted)
kubectl delete deployment nginx-deployment
# Or delete with cascading
kubectl delete deployment nginx-deployment --cascade=foreground
Topics
- Rolling Updates & Rollbacks - Detailed guide to deployment updates and rollbacks
- Strategies - Deployment update strategies and configurations
See Also
- ReplicaSets - Understanding the ReplicaSet mechanism
- Services - Exposing Deployments via Services
- Autoscaling - Automatically scaling Deployments
- Resilience Patterns - Health checks and probes for Deployments