Configure Liveness Probe in Kubernetes

Prathap
3 min readJun 3, 2022

Kubelet uses liveness probe to know when to restart a container.

For instance, a liveness probe could notice a deadlock where an application is running, unable to make any progress. Restarting a container in such a state can help to make the application more available despite bugs.

Many applications running for a long time someday transition into a broken state and can not recover except by being restarted. Kubernetes helps to detect and fix such problems using a liveness probe.

Define a liveness command:

liveness command

In the above configuration, the Pod has single Container. The periodSeconds field specifies that kubelet should perform the liveness probe every 5 seconds. The initialDelaySeconds specifies that kubelet that it should wait 5 seconds before performing the first probe.

To perform probe the kubelet runs the command “cat /tmp/healthyin the target container. If the command succeeds, it returns 0 and kubelet considers the container to be alive and healthy. If the command returns non-zero value, the kubelet kills the container and restarts it.

Define a liveness HTTP request:

Another kind of liveness probe uses an HTTP GET request.

liveness HTTP request

As shown in the above configuration, Pod has a single Container. Kubelet performs the liveness probe every 3 seconds. Kubelet waits for 3 seconds before performing the first liveness probe.

To perform the probe, the kubelet sends an HTTP request to the server that is running in the container and the listening port on 8080. If the HTTP request (/healthz) returns a success code, the kubelet considers the Container to be alive and healthy. If the HTTP request returns a failure code, kubelet kills the Container and restarts it.

Define a TCP liveness probe:

The TCP liveness probe is similar to the HTTP request liveness probe and uses a TCP socket for the liveness probe.

TCP liveness

In this configuration, kubelet will attempt to open a socket to the container on the specified port. If kubelet can establish a connection, the container is considered healthy. If kubelet fails to open a connection, kubelet kills the container and restarts it.

--

--