Unit 5.4 · Tutorial 2

CD & Deployment Strategies

Chapter 15 · System Design & Deployment
~2.5 hours Advanced Blue‑Green · Canary · Rolling · Rollback

Overview

Continuous Delivery (CD) automates the deployment of applications to production. This tutorial covers the major deployment strategies: blue‑green, canary, rolling updates, and rollback strategies. You'll learn to choose the right strategy for your application based on risk tolerance and operational requirements.

Why this matters: Choosing the right deployment strategy reduces downtime, minimises risk, and enables faster release cycles. It's essential for modern DevOps practices.

1. Deployment Strategies Overview

// Comparison of deployment strategies | Strategy | Downtime | Risk | Rollback Speed | Cost | |---------------|----------|----------|----------------|--------| | Blue-Green | None | Low | Instant | High | | Canary | None | Medium | Fast | Medium | | Rolling | None | Medium | Medium | Low | | Recreate | Yes | High | Slow | Low |

Key considerations

  • Downtime tolerance: Can your application afford downtime?
  • Risk tolerance: How much risk are you willing to accept?
  • Rollback speed: How quickly can you revert if something goes wrong?
  • Infrastructure cost: Extra resources needed for the deployment.

2. Blue-Green Deployment

Blue‑green deployment maintains two identical environments: blue (current) and green (new). Traffic is switched from blue to green once the new version is verified.

// Blue-Green workflow 1. Deploy new version to green environment 2. Run tests on green environment 3. Switch router/Load Balancer from blue to green 4. Monitor green for issues 5. If issues, switch back to blue (instant rollback) // Infrastructure (simplified) - Load Balancer: routes traffic to active environment - Blue: current production (v1) - Green: new version (v2)
// Kubernetes example with blue-green # Blue deployment (current) apiVersion: v1 kind: Service metadata: name: app-service spec: selector: app: blue # Green deployment (new) apiVersion: apps/v1 kind: Deployment metadata: name: app-green spec: replicas: 3 selector: matchLabels: app: green template: metadata: labels: app: green spec: containers: - name: app image: app:v2
Pros: Zero downtime, instant rollback, easy to test. Cons: Requires double the infrastructure (cost).

3. Canary Deployments

Canary deployments gradually roll out the new version to a small subset of users before a full rollout.

// Canary workflow 1. Deploy new version to a small percentage of users (e.g., 5%) 2. Monitor metrics (error rate, latency, user feedback) 3. If metrics are good, gradually increase percentage (10% → 25% → 50% → 100%) 4. If issues are detected, roll back immediately // Example: Ingress controller weight-based routing apiVersion: networking.k8s.io/v1 kind: Ingress metadata: annotations: nginx.ingress.kubernetes.io/canary: "true" nginx.ingress.kubernetes.io/canary-weight: "10"
// CI/CD pipeline with canary (GitHub Actions + Argo Rollouts) deploy-canary: steps: - name: Deploy canary run: kubectl set image deployment/canary app=${{ env.NEW_IMAGE }} - name: Monitor canary run: | # Check error rate if error_rate > 0.01; then echo "Canary failed, rolling back" kubectl rollout undo deployment/canary fi - name: Promote canary run: kubectl patch deployment/canary -p '{"spec":{"trafficRouting":{"weight":50}}}'
Pros: Risk reduction, real‑world testing, gradual rollout. Cons: Complex to implement, requires monitoring and automation.

4. Rolling Updates

Rolling updates gradually replace old instances with new ones, maintaining availability during the deployment.

// Kubernetes rolling update configuration apiVersion: apps/v1 kind: Deployment spec: replicas: 5 strategy: type: RollingUpdate rollingUpdate: maxSurge: 1 # Number of extra pods allowed maxUnavailable: 0 # Number of unavailable pods allowed template: spec: containers: - name: app image: app:v2
// CI/CD pipeline with rolling update deploy: script: - docker build -t app:$CI_COMMIT_SHA . - docker push app:$CI_COMMIT_SHA - kubectl set image deployment/app app=app:$CI_COMMIT_SHA - kubectl rollout status deployment/app
Pros: No extra infrastructure required, zero downtime. Cons: Slower rollback, more complex to monitor.

5. Rollback Strategies

  • Immediate rollback: Switch back to the previous version instantly (blue‑green).
  • Gradual rollback: Roll back one instance at a time (rolling update).
  • Automated rollback: Triggered by monitoring alerts (canary).
  • Manual rollback: Human intervention required.
# Kubernetes rollback kubectl rollout history deployment/app kubectl rollout undo deployment/app --to-revision=2 # GitHub Actions rollback (manual trigger) name: Rollback on: workflow_dispatch: inputs: revision: description: 'Revision to rollback to' required: true jobs: rollback: runs-on: ubuntu-latest steps: - name: Rollback deployment run: kubectl rollout undo deployment/app --to-revision=${{ github.event.inputs.revision }}
Best practices:
  • Always have a rollback plan.
  • Automate rollback detection when possible.
  • Test rollback procedures regularly.
  • Keep previous versions available for quick rollback.

Quiz

Question 1

Which deployment strategy uses two identical environments and switches traffic between them?

  • Canary
  • Blue-Green
  • Rolling Update
  • Recreate
Show answer
B. Blue-Green.

Question 2

What is the primary advantage of a canary deployment?

  • Zero infrastructure cost
  • Gradual rollout with monitoring, reducing risk
  • Instant rollback
  • Simple implementation
Show answer
B. Gradual rollout with monitoring, reducing risk.

Question 3

Which Kubernetes strategy controls the number of extra pods during a rolling update?

  • maxUnavailable
  • maxSurge
  • replicas
  • strategy
Show answer
B. maxSurge.

Exercises

Exercise 1

Design a blue‑green deployment strategy for a web application. Describe the infrastructure, the switching mechanism, and the rollback plan.

Sample answer
  • Infrastructure: Two environments (blue/green) behind a load balancer.
  • Switching: Update load balancer target group or DNS record.
  • Rollback: Switch load balancer back to blue.
  • Testing: Verify green environment before switching.

Exercise 2

Explain how you would implement a canary deployment with a 10% initial rollout and automated rollback if the error rate exceeds 1%.

Sample answer
  • Deploy: Deploy new version with 10% traffic weight.
  • Monitor: Track error rate, latency, and success rate.
  • Automation: If error_rate > 0.01, trigger rollback.
  • Tool: Use Argo Rollouts with analysis template.

Homework

Homework 1

Choose a deployment strategy for your capstone project. Write a 300‑word justification, considering risk tolerance, downtime requirements, and infrastructure constraints.

Sample outline
  • Strategy: Rolling update (or canary).
  • Justification: Low cost, zero downtime, simple to implement.
  • Infrastructure: Kubernetes with rolling update configuration.
  • Rollback: `kubectl rollout undo`.

Mini‑Project

Deployment Strategy Implementation

Implement a deployment strategy for a microservice application:

  • Choose a strategy (blue‑green, canary, or rolling)
  • Write Kubernetes manifests or Terraform configuration
  • Implement a CI/CD pipeline that uses the strategy
  • Include a rollback mechanism
  • Document the complete setup
Sample outline
  • Strategy: Blue‑green with nginx ingress controller.
  • Kubernetes: Blue deployment, Green deployment, Service pointing to active.
  • Pipeline: GitHub Actions that deploys to green, tests, and switches traffic.
  • Rollback: Manual switch via `kubectl patch service`.

Tutorial Summary

You learned the major deployment strategies: blue‑green, canary, rolling updates, and rollback strategies. Each has different trade‑offs in terms of risk, cost, and complexity. Choosing the right strategy is essential for safe, reliable software delivery.

Key takeaway: Deployment strategy is a business decision as much as a technical one. Balance risk, cost, and speed based on your application's requirements.