Blue-green deployments in Kubernetes - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using blue-green deployments in Kubernetes, we want to know how the time to switch versions grows as the number of services or pods increases.
We ask: How does deployment time change when we add more pods or services?
Analyze the time complexity of the following Kubernetes deployment switch.
apiVersion: apps/v1
kind: Deployment
metadata:
name: green-deployment
spec:
replicas: 5
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: myapp:green
---
apiVersion: v1
kind: Service
metadata:
name: myapp-service
spec:
selector:
app: myapp
ports:
- protocol: TCP
port: 80
targetPort: 8080
This snippet shows a green deployment with 5 pods and a service routing traffic to pods labeled "myapp".
Look for repeated actions during deployment switch.
- Primary operation: Updating each pod from old version to new version.
- How many times: Once per pod, so number of pods (replicas) times.
As you add more pods, the time to update all pods grows roughly in direct proportion.
| Input Size (n = pods) | Approx. Operations (pod updates) |
|---|---|
| 10 | 10 pod updates |
| 100 | 100 pod updates |
| 1000 | 1000 pod updates |
Pattern observation: Doubling pods doubles the update operations needed.
Time Complexity: O(n)
This means the deployment time grows linearly with the number of pods you have.
[X] Wrong: "Switching traffic between blue and green is instant and does not depend on pod count."
[OK] Correct: While traffic switch via service selector is fast, the actual pod updates and readiness checks take time proportional to how many pods must be updated.
Understanding how deployment time scales helps you design smooth updates and avoid downtime, a key skill in real-world Kubernetes operations.
What if we changed the deployment to update pods in batches instead of all at once? How would the time complexity change?
Practice
Solution
Step 1: Understand blue-green deployment concept
Blue-green deployment runs two versions of an app side by side to avoid downtime.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).Final Answer:
To update applications without downtime by switching traffic between two versions -> Option AQuick Check:
Blue-green deployment = zero downtime updates [OK]
- Confusing blue-green with scaling pods
- Thinking it is for backups
- Mixing it with auto-scaling
Solution
Step 1: Identify traffic routing resource
In Kubernetes, a Service routes traffic to pods based on labels.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.Final Answer:
Service -> Option CQuick Check:
Service routes traffic in blue-green deployments [OK]
- Choosing ConfigMap which stores config data
- Selecting PersistentVolume which manages storage
- Picking Ingress Controller which manages external access
selector: app: myapp version: blue
What happens if you change the selector to
version: green?Solution
Step 1: Understand Service selector role
The Service selector chooses pods matching the labels to send traffic to.Step 2: Effect of changing selector
Changing selector to version: green directs traffic only to pods labeled green, ignoring blue pods.Final Answer:
Traffic will be routed to pods labeled with version green -> Option BQuick Check:
Selector change = traffic to matching pods [OK]
- Assuming traffic splits automatically
- Thinking selector change breaks traffic
- Believing old pods still get traffic
Solution
Step 1: Check traffic routing setup
If users still see blue, traffic is likely still routed to blue pods.Step 2: Identify cause of routing
This happens if the Service selector was not updated to green pods after deploying green version.Final Answer:
The Service selector was not updated to point to green pods -> Option AQuick Check:
Service selector update needed to switch traffic [OK]
- Assuming pods failed without checking
- Thinking Deployment deletion causes this
- Blaming cluster resource issues first
- Blue pods running version 1
- Green pods running version 2
Which sequence of steps ensures a safe switch?
Solution
Step 1: Switch traffic safely
First, update the Service selector to point to green pods so new traffic goes to version 2.Step 2: Confirm green pods are healthy
Check green pods are running well before removing blue pods to avoid downtime.Step 3: Remove old version
After confirmation, delete blue pods to free resources.Final Answer:
Update Service selector to green pods, then delete blue pods after confirming green is healthy -> Option DQuick Check:
Switch traffic first, confirm health, then remove old [OK]
- Deleting blue pods before switching traffic
- Not confirming green pods health
- Updating Deployment without switching Service selector
