Deployment Strategies
Overview
Deployment strategies control how pods are replaced during updates.
Strategy Types
| Strategy | Description | Downtime |
|---|---|---|
| RollingUpdate | Gradual replacement (default) | No |
| Recreate | Kill all, then create new | Yes |
RollingUpdate (Default)
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deploy
spec:
replicas: 4
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1 # Max pods above desired during update
maxUnavailable: 1 # Max pods unavailable during update
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.20RollingUpdate Parameters
| Parameter | Description | Default |
|---|---|---|
maxSurge | Extra pods allowed during update | 25% |
maxUnavailable | Pods that can be unavailable | 25% |
Can be absolute number or percentage:
maxSurge: 2- Allow 2 extra podsmaxSurge: 25%- Allow 25% extra podsmaxUnavailable: 0- No downtime (need maxSurge > 0)
Recreate Strategy
All pods killed before new ones created:
yaml
spec:
strategy:
type: RecreateUse cases:
- Incompatible versions can't run together
- Database migrations requiring exclusive access
- Stateful apps that can't share resources
RollingUpdate Examples
Zero Downtime (Conservative)
yaml
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
# Only 1 new pod created, then 1 old removedFast Update (Aggressive)
yaml
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 50%
maxUnavailable: 50%
# Half the pods updated at onceOne at a Time
yaml
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 1Watch Rolling Update
bash
# Watch pods during update
kubectl get pods -w
# Watch rollout status
kubectl rollout status deployment/nginx-deploy
# Check rollout history
kubectl rollout history deployment/nginx-deployRollback
bash
# Rollback to previous version
kubectl rollout undo deployment/nginx-deploy
# Rollback to specific revision
kubectl rollout undo deployment/nginx-deploy --to-revision=2
# Check revision history
kubectl rollout history deployment/nginx-deploy --revision=2Pause/Resume Rollout
bash
# Pause mid-rollout (canary testing)
kubectl rollout pause deployment/nginx-deploy
# Resume rollout
kubectl rollout resume deployment/nginx-deployUpdate Image
bash
# Trigger rollout by updating image
kubectl set image deployment/nginx-deploy nginx=nginx:1.21
# Or edit directly
kubectl edit deployment nginx-deployRevision History
yaml
spec:
revisionHistoryLimit: 10 # Keep 10 old ReplicaSets for rollbackbash
# Check history
kubectl rollout history deployment/nginx-deploy
# See specific revision
kubectl rollout history deployment/nginx-deploy --revision=3