Liveness Probe
Overview
Kubernetes applications have the self-healing capability, that is, when an application container crashes, the container can be detected and restarted automatically. However, this mechanism does not work for deadlocks. Assume that a Java program is having a memory leak. The program is unable to make any progress, while the JVM process is running. To address this issue, Kubernetes introduces liveness probes to check whether containers response normally and determine whether to restart containers. This is a good health check mechanism.
It is advised to define the liveness probe for every pod to gain a better understanding of pods' running statuses.
Supported detection mechanisms are as follows:
- HTTP GET: The kubelet sends an HTTP GET request to the container. Any 2XX or 3XX code indicates success. Any other code returned indicates failure.
- TCP Socket: The kubelet attempts to open a socket to your container on the specified port. If it can establish a connection, the container is considered healthy. If it fails to establish a connection, the container is considered a failure.
- Exec: kubelet executes a command in the target container. If the command succeeds, it returns 0, and kubelet considers the container to be alive and healthy. If the command returns a non-zero value, kubelet kills the container and restarts it.
In addition to liveness probes, readiness probes are also available for you to detect pod status. For details, see Readiness Probe.
HTTP GET
HTTP GET is the most common detection method. An HTTP GET request is sent to a container. Any 2xx or 3xx code returned indicates that the container is healthy. The following example shows how to define such a request:
apiVersion: v1 kind: Pod metadata: name: liveness-http spec: containers: - name: liveness image: nginx:alpine livenessProbe: # liveness probe httpGet: #HTTP GET definition path: / port: 80 imagePullSecrets: - name: default-secret
Create pod liveness-http.
$ kubectl create -f liveness-http.yaml pod/liveness-http created
The probe sends an HTTP Get request to port 80 of the container. If the request fails, Kubernetes restarts the container.
View details of pod liveness-http.
$ kubectl describe po liveness-http Name: liveness-http ...... Containers: liveness: ...... State: Running Started: Mon, 03 Aug 2020 03:08:55 +0000 Ready: True Restart Count: 0 Liveness: http-get http://:80/ delay=0s timeout=1s period=10s #success=1 #failure=3 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-vssmw (ro) ......
The preceding output reports that the pod is Running with Restart Count being 0, which indicates that the container is normal and no restarts have been triggered. If the value of Restart Count is not 0, the container has been restarted.
TCP Socket
TCP Socket: The kubelet attempts to open a socket to your container on the specified port. If it can establish a connection, the container is considered healthy. If it fails to establish a connection, the container is considered a failure. For detailed defining method, see the following example.
apiVersion: v1 kind: Pod metadata: labels: test: liveness name: liveness-tcp spec: containers: - name: liveness image: nginx:alpine livenessProbe: # liveness probe tcpSocket: port: 80 imagePullSecrets: - name: default-secret
Exec
kubelet executes a command in the target container. If the command succeeds, it returns 0, and kubelet considers the container to be alive and healthy. The following example shows how to define the command.
apiVersion: v1 kind: Pod metadata: labels: test: liveness name: liveness-exec spec: containers: - name: liveness image: nginx:alpine args: - /bin/sh - -c - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600 livenessProbe: # liveness probe exec: # Exec definition command: - cat - /tmp/healthy imagePullSecrets: - name: default-secret
In the preceding configuration file, kubelet executes the command cat /tmp/healthy in the container. If the command succeeds and returns 0, the container is considered healthy. For the first 30 seconds, there is a /tmp/healthy file. So during the first 30 seconds, the command cat /tmp/healthy returns a success code. After 30 seconds, the /tmp/healthy file is deleted. The probe will then consider the pod to be unhealthy and restart it.
Advanced Settings of a Liveness Probe
The describe command of liveness-http returns the following information:
Liveness: http-get http://:80/ delay=0s timeout=1s period=10s #success=1 #failure=3
This is the detailed configuration of the liveness probe.
- delay=0s indicates that the probe starts immediately after the container is started.
- timeout=1 indicates that the container must respond within one second. Otherwise, the health check is recorded as failed.
- period=10s indicates that the probe checks containers every 10 seconds.
- #success=1 indicates that the operation is recorded as successful if it is successful for once.
- #failure=3 indicates that a container will be restarted after three consecutive failures.
The preceding liveness probe indicates that the probe checks containers immediately after they are started. If a container does not respond within one second, the check is recorded as failed. The health check is performed every 10 seconds. If the check fails for three consecutive times, the container is restarted.
apiVersion: v1 kind: Pod metadata: name: liveness-http spec: containers: - name: liveness image: nginx:alpine livenessProbe: httpGet: path: / port: 80 initialDelaySeconds: 10 # Liveness probes are initiated after the container has started for 10s. timeoutSeconds: 2 # The container must respond within 2s. Otherwise, it is considered as a failure. periodSeconds: 30 # The probe is performed every 30s. successThreshold: 1 # The container is considered healthy as long as the probe succeeds once. failureThreshold: 3 # The container is considered unhealthy after three consecutive failures.
Normally, the value of initialDelaySeconds must be greater than 0, because it takes a while for the application to be ready. The probe often fails if the probe is initiated before the application is ready.
In addition, you can set the value of failureThreshold to be greater than 1. In this way, the kubelet checks the container for multiple times in one probe rather than performing the probe for multiple times.
Configuring a Liveness Probe
- What to check
An effective liveness probe should check all the key parts of an application and use a dedicated URL, such as /health. When the URL is accessed, the probe is triggered and a result is returned. Note that no authentication should be involved. Otherwise, the probe keeps failing and restarting the container.
In addition, a probe must not check parts that have external dependencies. For example, if a frontend web server cannot connect to a database, the web server should not be considered unhealthy for the connection failure.
- To be lightweight
A liveness probe must not occupy too many resources or certain resources for too long. Otherwise, resource shortage may affect service running. For example, the HTTP GET method is recommended for a Java application. If the Exec method is used, the JVM startup process occupies too many resources.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot