File-Aware Readiness Probe for Container Initialization
Scenerio
Create a single Pod named python-app-pod in namespace python-app of image busybox:stable.
The Pod should have a readiness probe executing:
cat /src/main.py
It should initially wait 15 seconds, periodically wait 15 seconds, and have failureThreshold set to 2.
This will set the container ready only if the file /src/main.py exists.
Hint
Use a container command that creates /src/main.py and keeps the container running (sleep), and an exec readinessProbe (cat /src/main.py) so the Pod becomes Ready only when the file exists.
Test Cases
Kubernetes Readiness Probes
A readiness probe answers a critical question: "Is this container ready to receive traffic?"
Kubernetes runs a health check command inside your container at regular intervals and evaluates the result based on the exit code:
- Exit code 0 → Command succeeded → Container marked as Ready ✓
- Non-zero exit code → Command failed → Container marked as Not Ready ✗
When a container is Not Ready, Kubernetes automatically stops routing traffic to it until it passes the probe again.
Understanding cat /src/main.py as a Readiness Check
The cat command reads a file and outputs its contents. When used as a readiness probe, it serves as a simple file existence check:
- File exists →
catsucceeds → Exit code 0 → Probe passes ✓ - File missing →
catfails → Exit code 1 → Probe fails ✗
This pattern is useful when your application creates a "ready" marker file once initialization completes.
Probe Execution Timeline
Here's what happens when a Pod starts with a readiness probe:
Pod starts
│
│ Container initializing...
│
├─ [Wait 15 seconds] ← initialDelaySeconds
│
├─ First probe: cat /src/main.py
│ │
│ ├─ File missing? → Probe fails (failure count: 1/2)
│ │
│ ├─ [Wait 15 seconds] ← periodSeconds
│ │
│ ├─ Second probe: cat /src/main.py
│ │ │
│ │ ├─ Still missing? → Probe fails (2/2) → Pod marked NOT READY
│ │ │ ⚠️ No traffic routed to this Pod
│ │ │
│ │ └─ File exists? → Probe passes → Pod marked READY
│ │ ✓ Traffic can now reach this Pod
│ │
│ └─ [Repeat every 15 seconds...]
│
└─ Pod continues running regardless of probe status
💡 Important: The Pod keeps running even when probes fail. Kubernetes simply stops sending traffic to it. Once the probe passes, traffic resumes automatically.
Step 1: Generate Base Manifest
kubectl run is a shortcut to create a Pod manifest without actually sending it to the Kubernetes API server.
kubectl run python-app-pod -n python-app \
--image=busybox:stable \
--dry-run=client -o yaml > python-app-pod.yaml
Breaking it down:
kubectl run python-app-pod— generates a Pod manifest with the namepython-app-pod-n python-app— sets the namespace topython-app--image=busybox:stable— sets the container image--dry-run=client— the manifest is built locally on your machine, nothing is sent to the cluster-o yaml— outputs the manifest in YAML format> python-app-pod.yaml— saves the output into a file
This gives you a clean base manifest that you can then edit to add the readiness probe before applying it.
Step 2: Add Command and Readiness Probe
⚠️ Why the command is Required
The busybox:stable image is a minimal Linux environment - it doesn't include /src/main.py or even the /src directory. We must create these through the container's command field in the manifest.
Without the command that creates these:
- The readiness probe runs
cat /src/main.py - The file doesn't exist →
catfails with exit code 1 - Probe fails continuously → Pod stays in
0/1 READYstate forever - No traffic will ever reach the Pod
Solution: Add command: ["sh", "-c", "mkdir -p /src && touch /src/main.py && sleep 3600"] to the container spec to create the file that the readiness probe expects.
Edit the file, add the readiness probe and command, then apply:
kubectl apply -f python-app-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: python-app-pod # Name of the Pod
namespace: python-app # Namespace where the Pod will be created
labels:
app: python-app # Label to identify this Pod
spec:
containers:
- name: python-app # Name of the container
image: busybox:stable # Using busybox as a lightweight container image
command: ["sh", "-c", "mkdir -p /src && touch /src/main.py && sleep 3600"]
# 1. mkdir -p /src → creates the /src directory
# 2. touch /src/main.py → creates the file the probe checks
# 3. sleep 3600 → keeps the container running
readinessProbe:
# exec runs a command inside the container
# The probe passes only if exit code is 0
exec:
command:
- cat # cat reads a file and prints its content
- /src/main.py # Target file to check for existence
# Wait 15 seconds after the container starts
# before running the first probe check.
# This gives the container time to initialize.
initialDelaySeconds: 15
# After each check, wait 15 seconds before
# running the next probe check.
periodSeconds: 15
# If the probe fails 2 times in a row,
# mark the container as Not Ready.
# Traffic will stop flowing to this Pod.
failureThreshold: 2
Verify
Wait for the probe to pass and verify:
kubectl get pod python-app-pod -n python-app -w
Once the initialDelaySeconds (15s) passes and the probe runs cat /src/main.py, the file exists so the Pod becomes 1/1 READY.