Rolling update strategy in Kubernetes - Time & Space Complexity
When Kubernetes updates an application, it replaces old versions with new ones gradually. Understanding how long this process takes helps us plan and manage updates smoothly.
We want to know how the update time grows as the number of pods increases.
Analyze the time complexity of the following Kubernetes rolling update configuration.
apiVersion: apps/v1
kind: Deployment
metadata:
name: example-app
spec:
replicas: 10
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 2
maxSurge: 3
template:
metadata:
labels:
app: example-app
spec:
containers:
- name: app-container
image: example-image:v2
This config updates 10 pods gradually, allowing up to 3 extra pods and 2 pods down at once.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Updating pods in batches during the rolling update.
- How many times: The number of batches depends on total pods and maxUnavailable/maxSurge settings.
As the number of pods (n) grows, the update happens in groups limited by maxUnavailable and maxSurge.
| Input Size (n) | Approx. Number of Update Batches |
|---|---|
| 10 | About 4 batches |
| 100 | About 34 batches |
| 1000 | About 334 batches |
Pattern observation: The number of batches grows roughly linearly with the number of pods.
Time Complexity: O(n)
This means the update time grows roughly in direct proportion to the number of pods being updated.
[X] Wrong: "The rolling update time stays the same no matter how many pods there are."
[OK] Correct: Because pods update in batches limited by maxUnavailable and maxSurge, more pods mean more batches and more time.
Understanding how rolling updates scale helps you manage real applications smoothly and shows you can think about system behavior as it grows.
"What if maxUnavailable was set to 1 instead of 2? How would the time complexity change?"
Practice
Solution
Step 1: Understand rolling update concept
A rolling update replaces pods one by one to avoid downtime.Step 2: Compare options with concept
Only To update an application without downtime by gradually replacing pods describes gradual replacement without downtime.Final Answer:
To update an application without downtime by gradually replacing pods -> Option AQuick Check:
Rolling update = gradual pod replacement [OK]
- Thinking rolling update deletes all pods at once
- Confusing rolling update with backup
- Assuming rolling update scales pods to zero
Solution
Step 1: Identify command to update image
The command to update a deployment's image is 'kubectl set image'.Step 2: Match command with options
kubectl set image deployment/myapp myapp-container=myimage:v2 uses 'kubectl set image' correctly to start rolling update.Final Answer:
kubectl set image deployment/myapp myapp-container=myimage:v2 -> Option AQuick Check:
Change image = kubectl set image [OK]
- Using rollout undo to start update
- Scaling replicas to zero instead of updating image
- Using get pods to update image
kubectl set image deployment/myapp myapp-container=myimage:v2, what does kubectl rollout status deployment/myapp show?Solution
Step 1: Understand purpose of rollout status
The command 'kubectl rollout status' shows progress of deployment updates.Step 2: Compare options with command purpose
The current rollout progress of the deployment update correctly describes rollout progress; others describe different commands.Final Answer:
The current rollout progress of the deployment update -> Option BQuick Check:
Rollout status = update progress [OK]
- Confusing rollout status with pod listing
- Expecting logs from rollout status
- Thinking rollout status shows config details
kubectl set image deployment/myapp myapp-container=myimage:v2 but the new pods crash. Which command helps you revert to the previous version?Solution
Step 1: Identify command to revert deployment
'kubectl rollout undo' reverts deployment to previous version.Step 2: Match command with options
kubectl rollout undo deployment/myapp correctly reverts the deployment; others do not revert updates.Final Answer:
kubectl rollout undo deployment/myapp -> Option CQuick Check:
Undo rollout = kubectl rollout undo [OK]
- Deleting deployment instead of undoing
- Scaling replicas to zero instead of reverting
- Using get pods which only lists pods
Solution
Step 1: Understand maxUnavailable and maxSurge
'maxUnavailable' controls how many pods can be down; 'maxSurge' controls extra pods created during update.Step 2: Ensure at least 2 pods always available
Setting 'maxUnavailable' to 0 means no pods go down; 'maxSurge' 2 allows 2 extra pods to be created for smooth update.Final Answer:
Set 'maxUnavailable' to 0 and 'maxSurge' to 2 in 'rollingUpdate' strategy -> Option DQuick Check:
Zero unavailable + surge 2 = no downtime, 2 pods always up [OK]
- Setting maxUnavailable > 0 causes downtime
- Setting maxSurge to 0 prevents extra pods during update
- Choosing high maxUnavailable and maxSurge causing instability
Ready to prove it?
Pass the test and this pattern moves unknown
