How to Use Exec Probe in Kubernetes for Container Health Checks
In Kubernetes, use the
exec probe to run a command inside a container to check its health. Define the exec probe in the pod's livenessProbe or readinessProbe section with the command to execute. Kubernetes runs this command periodically and uses its success or failure to manage container lifecycle.Syntax
The exec probe runs a command inside the container to check its health. It is defined under livenessProbe or readinessProbe in the pod spec.
Key parts:
- exec: The command to run as an array of strings.
- initialDelaySeconds: Time to wait before starting probes.
- periodSeconds: How often to run the probe.
- timeoutSeconds: Time to wait for the command to finish.
yaml
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5
timeoutSeconds: 1Example
This example shows a pod with an exec liveness probe that checks if the file /tmp/healthy exists inside the container. If the file is missing, Kubernetes restarts the container.
yaml
apiVersion: v1
kind: Pod
metadata:
name: exec-probe-example
spec:
containers:
- name: busybox
image: busybox
command: ["sh", "-c", "touch /tmp/healthy && sleep 3600"]
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5
timeoutSeconds: 1Output
kubectl get pod exec-probe-example
NAME READY STATUS RESTARTS AGE
exec-probe-example 1/1 Running 0 10s
Common Pitfalls
Common mistakes when using exec probes include:
- Using commands that never exit or take too long, causing probe timeouts.
- Not setting
initialDelaySeconds, so probes start before the container is ready. - Using commands that require environment setup not present in the container.
- Confusing
execprobe with HTTP or TCP probes.
Always test your command inside the container manually before using it in a probe.
yaml
Wrong:
livenessProbe:
exec:
command:
- tail
- -f
- /dev/null
initialDelaySeconds: 5
periodSeconds: 5
Right:
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5Quick Reference
| Field | Description | Example |
|---|---|---|
| exec.command | Command array to run inside container | ["cat", "/tmp/healthy"] |
| initialDelaySeconds | Wait time before first probe | 5 |
| periodSeconds | Interval between probes | 5 |
| timeoutSeconds | Time to wait for command to finish | 1 |
| failureThreshold | Failures before marking container unhealthy | 3 |
Key Takeaways
Use the exec probe to run a command inside the container for health checks.
Set initialDelaySeconds to avoid probing before the container is ready.
Ensure the exec command exits quickly and correctly reflects container health.
Test your exec command inside the container before using it in probes.
Exec probes help Kubernetes decide when to restart or stop routing traffic to containers.