Configure Liveness, Readiness and Startup Probes [page]deterministic
This page shows how to configure liveness, readiness and startup probes for containers.
For more information about probes, see [Liveness, Readiness and Startup Probes](/docs/concepts/workloads/pods/probes).
##
## Define a liveness command
Many applications running for long periods of time eventually transition to broken states, and cannot recover except by being restarted. Kubernetes provides liveness probes to detect and remedy such situations.
In this exercise, you create a Pod that runs a container based on the `registry.k8s.io/busybox:1.27.2` image. Here is the configuration file for the Pod:
In the configuration file, you can see that the Pod has a single `Container`. The `periodSeconds` field specifies that the kubelet should perform a liveness probe every 5 seconds. The `initialDelaySeconds` field tells the kubelet that it should wait 5 seconds before performing the first probe. To perform a probe, the kubelet executes the command `cat /tmp/healthy` in the target container. If the command succeeds, it returns 0, and the kubelet considers the container to be alive and healthy. If the command returns a non-zero value, the kubelet kills the container and restarts it.
When the container starts, it executes this command:
```shell /bin/sh -c "touch /tmp/healthy; sleep 30; rm -f /tmp/healthy; sleep 600" ```
For the first 30 seconds of the container's life, there is a `/tmp/healthy` file. So during the first 30 seconds, the command `cat /tmp/healthy` returns a success code. After 30 seconds, `cat /tmp/healthy` returns a failure code.
Create the Pod:
```shell kubectl apply -f https://k8s.io/examples/pods/probe/exec-liveness.yaml ```
Within 30 seconds, view the Pod events:
```shell kubectl describe pod liveness-exec ```
The output indicates that no liveness probes have failed yet:
```none Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 11s default-scheduler Successfully assigned default/liveness-exec to node01 Normal Pulling 9s kubelet, node01 Pulling image "registry.k8s.io/busybox:1.27.2" Normal Pulled 7s kubelet, node01 Successfully pulled image "registry.k8s.io/busybox:1.27.2" Normal Created 7s kubelet, node01 Created container liveness Normal Started 7s kubelet, node01 Started container liveness ```
After 35 seconds, view the Pod events again:
```shell kubectl describe pod liveness-exec ```
At the bottom of the output, there are messages indicating that the liveness probes have failed, and the failed containers have been killed and recreated.
```none Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 57s default-scheduler Successfully assigned default/liveness-exec to node01 Normal Pulling 55s kubelet, node01 Pulling image "registry.k8s.io/busybox:1.27.2" Normal Pulled 53s kubelet, node01 Successfully pulled image "registry.k8s.io/busybox:1.27.2" Normal Created 53s kubelet, node01 Created container liveness Normal Started 53s kubelet, node01 Started container liveness Warning Unhealthy 10s (x3 over 20s) kubelet, node01 Liveness probe failed: cat: can't open '/tmp/healthy': No such file or directory Normal Killing 10s kubelet, node01 Container liveness failed liveness probe, will be restarted ```
Wait another 30 seconds, and verify that the container has been restarted:
```shell kubectl get pod liveness-exec ```
The output shows that `RESTARTS` has been incremented. Note that the `RESTARTS` counter increments as soon as a failed container comes back to the running state:
```none NAME READY STATUS RESTARTS AGE liveness-exec 1/1 Running 1 1m ```
## Define a liveness HTTP request
Another kind of liveness pr …(trimmed)