Pod Disruption Budgets in Kubernetes - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to process Pod Disruption Budgets changes as the number of pods grows.
Specifically, how does Kubernetes handle checking and enforcing these budgets when many pods exist?
Analyze the time complexity of the following Pod Disruption Budget (PDB) enforcement snippet.
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: example-pdb
spec:
minAvailable: 2
selector:
matchLabels:
app: myapp
This PDB ensures at least 2 pods with label app=myapp stay running during disruptions.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Kubernetes lists and counts all pods matching the PDB selector.
- How many times: It checks each pod once per disruption event to verify availability.
As the number of pods matching the selector increases, the time to check them grows linearly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 pod checks |
| 100 | 100 pod checks |
| 1000 | 1000 pod checks |
Pattern observation: The checking time grows directly with the number of pods.
Time Complexity: O(n)
This means the time to enforce the PDB grows in direct proportion to the number of pods it monitors.
[X] Wrong: "Checking a PDB always takes constant time regardless of pod count."
[OK] Correct: Kubernetes must examine each pod to count availability, so more pods mean more work.
Understanding how Kubernetes scales checks like PDB enforcement helps you reason about system behavior under load.
What if the PDB selector matched multiple labels instead of one? How would the time complexity change?
Practice
Pod Disruption Budget in Kubernetes?Solution
Step 1: Understand Pod Disruption Budget purpose
A Pod Disruption Budget (PDB) is designed to keep your app available during planned changes by limiting voluntary disruptions.Step 2: Match purpose to options
To ensure a minimum number of pods stay available during planned disruptions correctly states that PDB ensures a minimum number of pods remain running during disruptions, unlike other options which describe unrelated features.Final Answer:
To ensure a minimum number of pods stay available during planned disruptions -> Option AQuick Check:
Pod Disruption Budget = availability during disruptions [OK]
- Confusing PDB with auto-scaling
- Thinking PDB restarts crashed pods
- Assuming PDB backs up pod data
app=web?Solution
Step 1: Check API version and kind
The correct API version for PDB ispolicy/v1and kind isPodDisruptionBudget.Step 2: Validate spec fields for maxUnavailable and selector
apiVersion: policy/v1 kind: PodDisruptionBudget metadata: name: pdb-web spec: maxUnavailable: 1 selector: matchLabels: app: web correctly usesmaxUnavailable: 1and a selector matching labelapp: web. Other options either use wrong API version, extra unsupported fields, or wrong spec keys.Final Answer:
apiVersion: policy/v1 kind: PodDisruptionBudget metadata: name: pdb-web spec: maxUnavailable: 1 selector: matchLabels: app: web -> Option DQuick Check:
Correct API and maxUnavailable syntax = apiVersion: policy/v1 kind: PodDisruptionBudget metadata: name: pdb-web spec: maxUnavailable: 1 selector: matchLabels: app: web [OK]
- Using wrong API version like v1
- Adding unsupported fields like strategy
- Confusing minAvailable with maxUnavailable
spec:
minAvailable: 3
selector:
matchLabels:
app: backend
If there are 5 pods with label app=backend, how many pods can be disrupted at once without violating the PDB?Solution
Step 1: Understand minAvailable meaning
minAvailable: 3means at least 3 pods must stay running during disruptions.Step 2: Calculate allowed disruptions
With 5 pods total, maximum disruptions allowed = 5 - 3 = 2 pods.Final Answer:
2 pods -> Option BQuick Check:
5 total - 3 minAvailable = 2 allowed disruptions [OK]
- Confusing minAvailable with maxUnavailable
- Assuming all pods can be disrupted
- Ignoring total pod count
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: pdb-db
spec:
maxUnavailable: 2
selector:
matchLabels:
app: database
But Kubernetes reports an error: "spec.maxUnavailable: Invalid value: "2": must be less than the number of pods selected by the label selector". What is the likely cause?Solution
Step 1: Understand maxUnavailable validation
maxUnavailable must be less than the total number of pods matched by the selector.Step 2: Analyze error message meaning
The error says "must be less than the number of pods" meaning the number of pods with labelapp=databaseis less than or equal to 2.Final Answer:
There are fewer than 3 pods with label app=database -> Option AQuick Check:
maxUnavailable < total pods = error if not [OK]
- Using maxUnavailable as percentage incorrectly
- Miswriting selector labels
- Thinking minAvailable is mandatory
app=frontend. You want to ensure that during voluntary disruptions, at least 80% of pods remain available. Which Pod Disruption Budget spec is correct?Solution
Step 1: Understand percentage usage in PDB
maxUnavailable and minAvailable can accept percentages. To keep 80% available, maxUnavailable should be 20%.Step 2: Evaluate options for correctness
spec: minAvailable: 80 selector: matchLabels: app: frontend:minAvailable: 80- absolute value exceeds total pods (10) - invalid.
spec: maxUnavailable: 8 selector: matchLabels: app: frontend:maxUnavailable: 8- allows 8 pods unavailable (only 20% available) - insufficient.
spec: maxUnavailable: 20% selector: matchLabels: app: frontend:maxUnavailable: 20%- allows 20% (~2 pods) unavailable - ensures 80% available - correct.
spec: minAvailable: 20% selector: matchLabels: app: frontend:minAvailable: 20%- ensures only at least 20% available - insufficient.Final Answer:
spec: maxUnavailable: 20% selector: matchLabels: app: frontend -> Option CQuick Check:
20% maxUnavailable = 80% pods available [OK]
- Using minAvailable with percentage incorrectly
- Confusing absolute numbers with percentages
- Not matching selector labels properly
