Try Sailor Cloud - 25% off!

Claim Now
Back to all posts

Kubernetes Deployments: Rolling Updates and Rollbacks

Kubernetes Deployments: Rolling Updates and Rollbacks

Deployments are the standard way to manage stateless applications in Kubernetes. They provide declarative updates, rollbacks, and scaling capabilities.

Creating Deployments

# Imperative command
kubectl create deployment nginx --image=nginx:1.19 --replicas=3

# Generate YAML
kubectl create deployment nginx --image=nginx --dry-run=client -o yaml > deploy.yaml

YAML Definition

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.19
        ports:
        - containerPort: 80

Scaling

# Scale deployment
kubectl scale deployment nginx --replicas=5

# Autoscale based on CPU
kubectl autoscale deployment nginx --min=3 --max=10 --cpu-percent=80

Rolling Updates

Update Strategy

spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1        # Max pods over desired
      maxUnavailable: 0  # Max pods unavailable

Performing Updates

# Update image
kubectl set image deployment/nginx nginx=nginx:1.20

# Edit deployment directly
kubectl edit deployment nginx

# Watch rollout status
kubectl rollout status deployment/nginx

Rollbacks

# Check rollout history
kubectl rollout history deployment/nginx

# View specific revision
kubectl rollout history deployment/nginx --revision=2

# Rollback to previous version
kubectl rollout undo deployment/nginx

# Rollback to specific revision
kubectl rollout undo deployment/nginx --to-revision=1

Pausing and Resuming

# Pause rollout (for multiple changes)
kubectl rollout pause deployment/nginx

# Make changes
kubectl set image deployment/nginx nginx=nginx:1.21
kubectl set resources deployment/nginx -c=nginx --limits=memory=256Mi

# Resume rollout
kubectl rollout resume deployment/nginx

Deployment Strategies

Rolling Update (Default)

Gradually replaces old pods with new ones.

Recreate

All old pods deleted before new ones created. Use for breaking changes.

spec:
  strategy:
    type: Recreate

Common Exam Tasks

  1. Create deployment with specific replicas
  2. Update deployment image
  3. Rollback to previous version
  4. Scale deployment
  5. View rollout history

Practice Deployments

Deployment management is heavily tested in CKA and CKAD. Practice with Sailor.sh.

Start Free Practice