Working with Deployments
1. Creating Deployment
Example: Deployment with rolling update
apiVersion: apps/v1
kind: Deployment
metadata: { name: web }
spec:
replicas: 3
revisionHistoryLimit: 5
strategy:
type: RollingUpdate
rollingUpdate: { maxUnavailable: 25%, maxSurge: 25% }
selector:
matchLabels: { app: web }
template:
metadata: { labels: { app: web } }
spec:
containers:
- name: app
image: myorg/web:1.4.2
ports: [{ containerPort: 8080 }]
readinessProbe:
httpGet: { path: /ready, port: 8080 }
2. Applying Deployment YAML
| Command | Use |
|---|---|
kubectl apply -f deploy.yaml | Create/update |
kubectl apply -f deploy.yaml --server-side | Server-side apply (field ownership) |
kubectl diff -f deploy.yaml | Show pending changes |
3. Scaling Deployment
kubectl scale deploy web --replicas=5
kubectl scale --replicas=5 -f deploy.yaml
kubectl scale deploy web --current-replicas=3 --replicas=5
| Flag | Use |
|---|---|
--current-replicas=N | Conditional scale (safer) |
4. Updating Deployment Image
kubectl set image deploy/web app=myorg/web:1.5.0 --record
kubectl set env deploy/web LOG_LEVEL=debug
kubectl set resources deploy/web -c=app --limits=cpu=1,memory=1Gi
| Command | Updates |
|---|---|
kubectl set image | Container image(s) |
kubectl set env | Env vars |
kubectl set resources | Requests/limits |
kubectl edit deploy/web | Open in $EDITOR |
5. Viewing Rollout Status
kubectl rollout status deploy/web
kubectl rollout status deploy/web --timeout=5m
| State | Meaning |
|---|---|
| Progressing | Replicas updating |
| Complete | All replicas updated & ready |
| Failed | progressDeadlineSeconds exceeded |
6. Viewing Rollout History
kubectl rollout history deploy/web
kubectl rollout history deploy/web --revision=3
| Column | Source |
|---|---|
| REVISION | Auto-incremented per template change |
| CHANGE-CAUSE | From kubernetes.io/change-cause annotation |
7. Rolling Back Deployment
kubectl rollout undo deploy/web
kubectl rollout undo deploy/web --to-revision=3
| Command | Effect |
|---|---|
rollout undo | Revert to previous revision |
--to-revision=N | Revert to specific revision |
8. Pausing Rollouts
kubectl rollout pause deploy/web
kubectl set image deploy/web app=myorg/web:1.6.0 # changes batched
kubectl set env deploy/web FEATURE=on
kubectl rollout resume deploy/web # rolls out once
| Use Case | Why |
|---|---|
| Batch updates | Avoid multiple rollouts |
| Canary verification | Pause after first batch |
9. Resuming Rollouts
| Command | Effect |
|---|---|
kubectl rollout resume deploy/web | Apply queued changes |
10. Configuring Update Strategy
| Strategy | Behavior |
|---|---|
| RollingUpdate | Default; gradually replace pods |
| Recreate | Delete all old pods, then create new (downtime) |
11. Setting Max Unavailable
| Form | Meaning |
|---|---|
| 25% (default) | Percent of desired replicas |
| Integer | Absolute pod count |
| 0 | No downtime (requires maxSurge ≥1) |
12. Setting Max Surge
| Form | Meaning |
|---|---|
| 25% (default) | Extra pods above replica count |
| 0 | No surge; slower rollout |
| 100% | Doubles capacity during rollout |
13. Setting Revision History Limit
spec:
revisionHistoryLimit: 10
progressDeadlineSeconds: 600
| Field | Default |
|---|---|
| revisionHistoryLimit | 10 (older ReplicaSets pruned) |
| progressDeadlineSeconds | 600 (mark Failed after) |