Skip to content

Deployment Strategies

Overview

Deployment strategies control how pods are replaced during updates.


Strategy Types

StrategyDescriptionDowntime
RollingUpdateGradual replacement (default)No
RecreateKill all, then create newYes

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.20

RollingUpdate Parameters

ParameterDescriptionDefault
maxSurgeExtra pods allowed during update25%
maxUnavailablePods that can be unavailable25%

Can be absolute number or percentage:

  • maxSurge: 2 - Allow 2 extra pods
  • maxSurge: 25% - Allow 25% extra pods
  • maxUnavailable: 0 - No downtime (need maxSurge > 0)

Recreate Strategy

All pods killed before new ones created:

yaml
spec:
  strategy:
    type: Recreate

Use 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 removed

Fast Update (Aggressive)

yaml
strategy:
  type: RollingUpdate
  rollingUpdate:
    maxSurge: 50%
    maxUnavailable: 50%
# Half the pods updated at once

One at a Time

yaml
strategy:
  type: RollingUpdate
  rollingUpdate:
    maxSurge: 1
    maxUnavailable: 1

Watch 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-deploy

Rollback

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=2

Pause/Resume Rollout

bash
# Pause mid-rollout (canary testing)
kubectl rollout pause deployment/nginx-deploy

# Resume rollout
kubectl rollout resume deployment/nginx-deploy

Update Image

bash
# Trigger rollout by updating image
kubectl set image deployment/nginx-deploy nginx=nginx:1.21

# Or edit directly
kubectl edit deployment nginx-deploy

Revision History

yaml
spec:
  revisionHistoryLimit: 10  # Keep 10 old ReplicaSets for rollback
bash
# Check history
kubectl rollout history deployment/nginx-deploy

# See specific revision
kubectl rollout history deployment/nginx-deploy --revision=3

Released under the MIT License.