0
0
Kubernetesdevops~5 mins

Exec probe configuration in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes, you want Kubernetes to check if your app inside a container is working well. Exec probes let Kubernetes run a command inside the container to see if it is healthy. If the command fails, Kubernetes knows the app might have a problem and can restart it.
When you want to check if a web server inside a container is running by running a command that tests its status.
When your app does not listen on a network port but can be checked by running a script inside the container.
When you want Kubernetes to restart a container automatically if a specific command inside it fails.
When you want to make sure a database inside a container is ready by running a command that checks its status.
When you want to avoid restarting a container unnecessarily by running a quick command to confirm health.
Config File - pod.yaml
pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: exec-probe-pod
spec:
  containers:
  - name: my-app
    image: busybox
    command: ["sh", "-c", "sleep 3600"]
    livenessProbe:
      exec:
        command:
        - cat
        - /tmp/healthy
      initialDelaySeconds: 5
      periodSeconds: 5

This YAML file creates a pod with one container running a simple busybox image that sleeps for an hour.

The livenessProbe uses an exec command to check if the file /tmp/healthy exists inside the container.

If the file is missing, the probe fails and Kubernetes restarts the container.

initialDelaySeconds waits 5 seconds before starting the checks, and periodSeconds runs the check every 5 seconds.

Commands
This command creates the pod with the exec liveness probe configured. Kubernetes will start the container and begin health checks after the initial delay.
Terminal
kubectl apply -f pod.yaml
Expected OutputExpected
pod/exec-probe-pod created
This command shows the status of the pod to verify it is running and ready after creation.
Terminal
kubectl get pods
Expected OutputExpected
NAME READY STATUS RESTARTS AGE exec-probe-pod 1/1 Running 0 10s
This command creates the file /tmp/healthy inside the container so the exec probe will succeed and keep the container healthy.
Terminal
kubectl exec exec-probe-pod -- sh -c 'touch /tmp/healthy'
Expected OutputExpected
No output (command runs silently)
This command removes the file /tmp/healthy inside the container to simulate a failure. The exec probe will fail and Kubernetes will restart the container.
Terminal
kubectl exec exec-probe-pod -- rm /tmp/healthy
Expected OutputExpected
No output (command runs silently)
Check the pod status again to see if Kubernetes restarted the container after the exec probe failed.
Terminal
kubectl get pods
Expected OutputExpected
NAME READY STATUS RESTARTS AGE exec-probe-pod 1/1 Running 1 1m
Key Concept

If you remember nothing else from this pattern, remember: exec probes run a command inside the container to check health and trigger restarts if the command fails.

Common Mistakes
Not setting initialDelaySeconds causing the probe to run before the app is ready.
The probe may fail immediately and restart the container unnecessarily.
Set initialDelaySeconds to wait enough time for the app to start before probing.
Using a command that always succeeds or never fails.
The probe will never detect real problems, so unhealthy containers won't restart.
Use a command that accurately reflects the app's health status.
Not testing the exec command manually inside the container before adding it to the probe.
If the command is wrong or missing, the probe will always fail.
Run the command inside the container with kubectl exec to verify it works as expected.
Summary
Create a pod YAML with a livenessProbe using exec to run a command inside the container.
Apply the YAML with kubectl apply and check pod status with kubectl get pods.
Use kubectl exec to create or remove files or run commands inside the container to test the probe.
Kubernetes restarts the container automatically if the exec probe command fails.