Canary deployments in Kubernetes - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using canary deployments in Kubernetes, it's important to understand how the deployment process scales as you increase the number of users or pods.
We want to know how the time to roll out changes as the system grows.
Analyze the time complexity of the following Kubernetes deployment snippet for a canary release.
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app-canary
spec:
replicas: 10
selector:
matchLabels:
app: my-app-canary
template:
metadata:
labels:
app: my-app-canary
spec:
containers:
- name: my-app-container
image: my-app:v2-canary
resources:
limits:
cpu: "500m"
memory: "256Mi"
This snippet creates 10 pods running the canary version of the app alongside existing stable pods.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating and updating each pod in the canary deployment.
- How many times: Once per replica, here 10 times for 10 pods.
As the number of replicas (pods) increases, the deployment controller performs operations for each pod.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 pod creations and updates |
| 100 | 100 pod creations and updates |
| 1000 | 1000 pod creations and updates |
Pattern observation: The work grows directly with the number of pods you deploy.
Time Complexity: O(n)
This means the time to complete the canary deployment grows linearly with the number of pods you create.
[X] Wrong: "Deploying more pods in a canary release takes the same time no matter how many pods there are."
[OK] Correct: Each pod requires separate creation and readiness checks, so more pods mean more work and longer deployment time.
Understanding how deployment time scales helps you design better release strategies and shows you can think about system growth clearly.
"What if we used a rolling update strategy that updates pods in batches instead of all at once? How would the time complexity change?"
Practice
canary deployment in Kubernetes?Solution
Step 1: Understand canary deployment concept
Canary deployments release new software versions to a small subset of users first to test stability and reduce risk.Step 2: Compare options with this concept
Only To release a new version to a small group of users first to reduce risk describes this gradual rollout to a small group to reduce risk.Final Answer:
To release a new version to a small group of users first to reduce risk -> Option AQuick Check:
Canary deployment = gradual rollout [OK]
- Thinking canary deploys to all users at once
- Confusing canary with blue-green deployment
- Assuming canary deletes old versions immediately
Solution
Step 1: Identify resource for managing app versions
Deployments manage application versions and rollout strategies in Kubernetes.Step 2: Match resource to canary deployment
Canary deployments use multiple Deployments with different labels to control traffic.Final Answer:
Deployment -> Option AQuick Check:
Canary uses Deployment resource [OK]
- Choosing ConfigMap which stores config, not versions
- Selecting ServiceAccount which manages permissions
- Picking PersistentVolume which handles storage
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-canary
labels:
version: canary
spec:
replicas: 2
selector:
matchLabels:
app: myapp
version: canary
template:
metadata:
labels:
app: myapp
version: canary
spec:
containers:
- name: myapp
image: myapp:v2
Assuming the stable deployment has 8 replicas with label version: stable and the Service routes traffic evenly by label.Solution
Step 1: Calculate total replicas
Stable has 8 replicas, canary has 2 replicas, total = 8 + 2 = 10 replicas.Step 2: Calculate canary traffic percentage
Traffic is split evenly by label, so canary gets 50% traffic regardless of pod count.Final Answer:
50% -> Option DQuick Check:
Service splits traffic evenly by label = 50% canary [OK]
- Assuming traffic splits by pod count instead of labels
- Ignoring label-based routing in Service
- Miscounting total replicas
Solution
Step 1: Understand how Service routes traffic
Service routes traffic to pods matching its selector labels.Step 2: Identify why canary pods get no traffic
If Service selector misses canary label, canary pods won't receive traffic, so users see only old version.Final Answer:
The Service selector does not include the canary label -> Option CQuick Check:
Service selector missing canary label = no canary traffic [OK]
- Assuming zero replicas without checking
- Blaming image tag without logs
- Ignoring Service selector labels
Solution
Step 1: Understand traffic splitting in Kubernetes Service
Standard Kubernetes Service does not support weighted traffic splitting by itself.Step 2: Identify method to split traffic by percentage
Using two Services and an Ingress or service mesh allows weighted traffic splitting (e.g., 10% to canary, 90% to stable).Step 3: Evaluate options
Create two Services, one for stable and one for canary, and use an Ingress with traffic splitting describes creating two Services and using Ingress for traffic splitting, which is the correct approach.Final Answer:
Create two Services, one for stable and one for canary, and use an Ingress with traffic splitting -> Option BQuick Check:
Weighted traffic split requires Ingress or service mesh [OK]
- Expecting Service selector to do weighted routing
- Scaling pods to control traffic percentage
- Using single Deployment for canary traffic split
