Bird
Raised Fist0
Kubernetesdevops~30 mins

Blue-green deployments in Kubernetes - Mini Project: Build & Apply

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Blue-green deployments
📖 Scenario: You are managing a web application running on Kubernetes. You want to deploy a new version of your app without downtime by using blue-green deployment strategy.
🎯 Goal: Build a simple blue-green deployment setup in Kubernetes using two deployments and a service that switches traffic between them.
📋 What You'll Learn
Create two deployments named blue-deployment and green-deployment with different app versions
Create a service named app-service that routes traffic to the active deployment
Switch the service selector from blue to green to simulate deployment switch
💡 Why This Matters
🌍 Real World
Blue-green deployments are used in production environments to update applications without downtime by running two identical environments and switching traffic.
💼 Career
Understanding blue-green deployments is essential for DevOps engineers and site reliability engineers to manage safe application rollouts.
Progress0 / 4 steps
1
Create the blue deployment
Create a Kubernetes deployment named blue-deployment with label app: blue and container image nginx:1.19.
Kubernetes
Hint

Use kind: Deployment and set metadata.name to blue-deployment. Use label app: blue in spec.template.metadata.labels. Use container image nginx:1.19.

2
Create the green deployment
Create a Kubernetes deployment named green-deployment with label app: green and container image nginx:1.20.
Kubernetes
Hint

Similar to blue deployment, but name it green-deployment, label app: green, and use image nginx:1.20.

3
Create the service routing to blue deployment
Create a Kubernetes service named app-service that routes traffic to pods with label app: blue on port 80.
Kubernetes
Hint

Create a Service with metadata.name as app-service. Set spec.selector.app to blue and expose port 80.

4
Switch service to green deployment
Change the app-service selector from app: blue to app: green to route traffic to the green deployment. Then print the selector line to confirm.
Kubernetes
Hint

Update the spec.selector.app in the service to green. Then print a confirmation message.

Practice

(1/5)
1. What is the main purpose of a blue-green deployment in Kubernetes?
easy
A. To update applications without downtime by switching traffic between two versions
B. To create multiple replicas of a pod for load balancing
C. To automatically scale pods based on CPU usage
D. To backup data from one cluster to another

Solution

  1. Step 1: Understand blue-green deployment concept

    Blue-green deployment runs two versions of an app side by side to avoid downtime.
  2. Step 2: Identify the main goal

    The main goal is to switch user traffic smoothly from the old version (blue) to the new version (green).
  3. Final Answer:

    To update applications without downtime by switching traffic between two versions -> Option A
  4. Quick Check:

    Blue-green deployment = zero downtime updates [OK]
Hint: Blue-green means two versions, switch traffic smoothly [OK]
Common Mistakes:
  • Confusing blue-green with scaling pods
  • Thinking it is for backups
  • Mixing it with auto-scaling
2. Which Kubernetes resource is typically used to switch traffic between blue and green deployments?
easy
A. ConfigMap
B. PersistentVolume
C. Service
D. Ingress Controller

Solution

  1. Step 1: Identify traffic routing resource

    In Kubernetes, a Service routes traffic to pods based on labels.
  2. Step 2: Understand blue-green switching

    Switching traffic between blue and green versions is done by changing the Service selector to point to the desired pods.
  3. Final Answer:

    Service -> Option C
  4. Quick Check:

    Service routes traffic in blue-green deployments [OK]
Hint: Service controls traffic routing between versions [OK]
Common Mistakes:
  • Choosing ConfigMap which stores config data
  • Selecting PersistentVolume which manages storage
  • Picking Ingress Controller which manages external access
3. Given the following Kubernetes Service selector for blue deployment:
selector:
  app: myapp
  version: blue

What happens if you change the selector to version: green?
medium
A. Traffic will be split evenly between blue and green pods
B. Traffic will be routed to pods labeled with version green
C. Traffic will stop because selector is invalid
D. Pods with version blue will receive traffic

Solution

  1. Step 1: Understand Service selector role

    The Service selector chooses pods matching the labels to send traffic to.
  2. Step 2: Effect of changing selector

    Changing selector to version: green directs traffic only to pods labeled green, ignoring blue pods.
  3. Final Answer:

    Traffic will be routed to pods labeled with version green -> Option B
  4. Quick Check:

    Selector change = traffic to matching pods [OK]
Hint: Service selector controls which pods get traffic [OK]
Common Mistakes:
  • Assuming traffic splits automatically
  • Thinking selector change breaks traffic
  • Believing old pods still get traffic
4. You deployed a green version but users still see the blue version. What is the most likely cause?
medium
A. The Service selector was not updated to point to green pods
B. The green pods failed to start due to image pull error
C. The Deployment was deleted accidentally
D. The cluster is out of CPU resources

Solution

  1. Step 1: Check traffic routing setup

    If users still see blue, traffic is likely still routed to blue pods.
  2. Step 2: Identify cause of routing

    This happens if the Service selector was not updated to green pods after deploying green version.
  3. Final Answer:

    The Service selector was not updated to point to green pods -> Option A
  4. Quick Check:

    Service selector update needed to switch traffic [OK]
Hint: Update Service selector to switch traffic [OK]
Common Mistakes:
  • Assuming pods failed without checking
  • Thinking Deployment deletion causes this
  • Blaming cluster resource issues first
5. You want to perform a blue-green deployment with zero downtime. You have:
  • Blue pods running version 1
  • Green pods running version 2

Which sequence of steps ensures a safe switch?
hard
A. Update Deployment to green version, wait for rollout, then update Service selector
B. Delete blue pods first, then update Service selector to green pods
C. Scale down blue pods to zero, then create green pods and update Service selector
D. Update Service selector to green pods, then delete blue pods after confirming green is healthy

Solution

  1. Step 1: Switch traffic safely

    First, update the Service selector to point to green pods so new traffic goes to version 2.
  2. Step 2: Confirm green pods are healthy

    Check green pods are running well before removing blue pods to avoid downtime.
  3. Step 3: Remove old version

    After confirmation, delete blue pods to free resources.
  4. Final Answer:

    Update Service selector to green pods, then delete blue pods after confirming green is healthy -> Option D
  5. Quick Check:

    Switch traffic first, confirm health, then remove old [OK]
Hint: Switch traffic first, confirm green healthy, then remove blue [OK]
Common Mistakes:
  • Deleting blue pods before switching traffic
  • Not confirming green pods health
  • Updating Deployment without switching Service selector