Liveness probe concept in Kubernetes - Time & Space Complexity
We want to understand how the time cost of running a liveness probe changes as the number of containers grows.
How does the system handle checking many containers to keep them healthy?
Analyze the time complexity of the following Kubernetes liveness probe configuration.
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: app-container
image: myapp:latest
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
This code sets a liveness probe that checks the /healthz path on port 8080 every 10 seconds after an initial delay.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The kubelet repeatedly sends HTTP GET requests to the container's /healthz endpoint.
- How many times: This happens every 10 seconds indefinitely for each container with a liveness probe.
As the number of containers with liveness probes increases, the kubelet must perform more health checks.
| Input Size (n) | Approx. Operations per period |
|---|---|
| 10 containers | 10 HTTP requests every 10 seconds |
| 100 containers | 100 HTTP requests every 10 seconds |
| 1000 containers | 1000 HTTP requests every 10 seconds |
Pattern observation: The number of health check requests grows directly with the number of containers.
Time Complexity: O(n)
This means the time spent checking container health grows linearly as you add more containers.
[X] Wrong: "The liveness probe checks happen only once or very rarely, so adding containers doesn't affect performance much."
[OK] Correct: The kubelet runs these checks repeatedly at fixed intervals for every container, so more containers mean more checks and more work.
Understanding how liveness probes scale helps you design systems that stay healthy without overloading the cluster.
"What if we changed the liveness probe to run every 5 seconds instead of 10? How would the time complexity change?"
Practice
liveness probe in Kubernetes?Solution
Step 1: Understand the role of liveness probes
Liveness probes check if a container is still running properly. If not, Kubernetes restarts it to fix issues.Step 2: Differentiate from readiness probes
Readiness probes check if a container can receive traffic, but liveness probes focus on container health and restarting.Final Answer:
To check if a container is alive and restart it if it is not -> Option AQuick Check:
Liveness probe = container health check and restart [OK]
- Confusing liveness probe with readiness probe
- Thinking it scales pods instead of restarting
- Assuming it monitors resource usage
Solution
Step 1: Check HTTP GET syntax for livenessProbe
The correct syntax useshttpGetwithpathand numericport. "livenessProbe:\n httpGet:\n path: /healthz\n port: 8080\n initialDelaySeconds: 5" matches this.Step 2: Identify errors in other options
"livenessProbe:\n exec:\n command: [\"curl\", \"/healthz\"]\n initialDelaySeconds: 5" uses exec with curl incorrectly. "livenessProbe:\n tcpSocket:\n path: /healthz\n port: 8080\n initialDelaySeconds: 5" wrongly uses tcpSocket with a path. "livenessProbe:\n httpGet:\n path: healthz\n port: 'eighty'\n initialDelaySeconds: 5" uses a string for port instead of a number.Final Answer:
"livenessProbe:\n httpGet:\n path: /healthz\n port: 8080\n initialDelaySeconds: 5" -> Option AQuick Check:
HTTP GET probe syntax = "livenessProbe:\n httpGet:\n path: /healthz\n port: 8080\n initialDelaySeconds: 5" [OK]
- Using string instead of number for port
- Adding path to tcpSocket probe
- Using exec command incorrectly
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 3
periodSeconds: 3
What happens if the file /tmp/healthy is deleted inside the container?Solution
Step 1: Understand exec liveness probe behavior
The exec probe runs the command. If the command fails (non-zero exit), Kubernetes restarts the container.Step 2: Effect of deleting /tmp/healthy file
Without the file,cat /tmp/healthyfails, so the probe fails and triggers a restart.Final Answer:
The container will be restarted by Kubernetes -> Option BQuick Check:
Exec probe fails = container restart [OK]
- Thinking pod is deleted permanently
- Assuming probe failure is ignored
- Confusing liveness with readiness probe
livenessProbe:
httpGet:
path: /health
port: 80
initialDelaySeconds: 10
periodSeconds: 5
failureThreshold: 3
But your container keeps restarting even though the app is healthy. What is the most likely cause?Solution
Step 1: Analyze probe failure reasons
If the probe path is wrong or the app does not respond at /health, the probe fails and restarts the container.Step 2: Check other parameters
Initial delay of 10 seconds is usually enough. Failure threshold of 3 means 3 failures before restart, which is normal. Port 80 is valid.Final Answer:
The probe path /health is incorrect or not responding -> Option CQuick Check:
Wrong probe path causes restarts [OK]
- Changing initialDelaySeconds unnecessarily
- Misunderstanding failureThreshold effect
- Assuming port 80 is invalid
Solution
Step 1: Identify TCP socket probe syntax
The tcpSocket probe usesportas a number, withinitialDelaySecondsandperiodSecondsset as required. "livenessProbe:\n tcpSocket:\n port: 3306\n initialDelaySeconds: 30\n periodSeconds: 10" matches this.Step 2: Check timing and port correctness
"livenessProbe:\n tcpSocket:\n port: 3306\n initialDelaySeconds: 30\n periodSeconds: 10" sets initial delay to 30 seconds and period to 10 seconds, matching the requirement. Other options have wrong timing or use HTTP or exec probes incorrectly.Final Answer:
"livenessProbe:\n tcpSocket:\n port: 3306\n initialDelaySeconds: 30\n periodSeconds: 10" -> Option DQuick Check:
TCP probe with correct timing = "livenessProbe:\n tcpSocket:\n port: 3306\n initialDelaySeconds: 30\n periodSeconds: 10" [OK]
- Using string instead of number for port
- Mixing HTTP probe for TCP check
- Setting wrong initialDelaySeconds or periodSeconds
Ready to prove it?
Pass the test and this pattern moves unknown
