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

CommandUse
kubectl apply -f deploy.yamlCreate/update
kubectl apply -f deploy.yaml --server-sideServer-side apply (field ownership)
kubectl diff -f deploy.yamlShow 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
FlagUse
--current-replicas=NConditional 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
CommandUpdates
kubectl set imageContainer image(s)
kubectl set envEnv vars
kubectl set resourcesRequests/limits
kubectl edit deploy/webOpen in $EDITOR

5. Viewing Rollout Status

kubectl rollout status deploy/web
kubectl rollout status deploy/web --timeout=5m
StateMeaning
ProgressingReplicas updating
CompleteAll replicas updated & ready
FailedprogressDeadlineSeconds exceeded

6. Viewing Rollout History

kubectl rollout history deploy/web
kubectl rollout history deploy/web --revision=3
ColumnSource
REVISIONAuto-incremented per template change
CHANGE-CAUSEFrom kubernetes.io/change-cause annotation

7. Rolling Back Deployment

kubectl rollout undo deploy/web
kubectl rollout undo deploy/web --to-revision=3
CommandEffect
rollout undoRevert to previous revision
--to-revision=NRevert 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 CaseWhy
Batch updatesAvoid multiple rollouts
Canary verificationPause after first batch

9. Resuming Rollouts

CommandEffect
kubectl rollout resume deploy/webApply queued changes

10. Configuring Update Strategy

StrategyBehavior
RollingUpdateDefault; gradually replace pods
RecreateDelete all old pods, then create new (downtime)

11. Setting Max Unavailable

FormMeaning
25% (default)Percent of desired replicas
IntegerAbsolute pod count
0No downtime (requires maxSurge ≥1)

12. Setting Max Surge

FormMeaning
25% (default)Extra pods above replica count
0No surge; slower rollout
100%Doubles capacity during rollout

13. Setting Revision History Limit

spec:
  revisionHistoryLimit: 10
  progressDeadlineSeconds: 600
FieldDefault
revisionHistoryLimit10 (older ReplicaSets pruned)
progressDeadlineSeconds600 (mark Failed after)