Configuring Health Checks for Containers
Scenario
Health check regularly checks the health of containers when they are running. If the health check function is not configured, a pod cannot detect application exceptions or automatically restart the application to restore it. This will result in a situation where the pod status is normal but the application in the pod is abnormal.
Kubernetes provides the following health check probes:
- Liveness probe (livenessProbe): checks whether a container is still alive. It is similar to the ps command that checks whether a process exists. If the liveness check of a container fails, the cluster restarts the container. If the liveness check is successful, no operation is executed.
- Readiness probe (readinessProbe): checks whether a container is ready to process user requests. Once the container is detected unready, service traffic will not be directed to the container. It may take a long time for some applications to start up before they can provide services. This is because that they need to load disk data or rely on startup of an external module. In this case, the application process is running, but the application cannot provide services. To address this issue, this health check probe is used. If the container readiness check fails, the cluster masks all requests sent to the container. If the container readiness check is successful, the container can be accessed.
- Startup probe (startupProbe): checks when a containerized application has started. If such a probe is configured, it disables liveness and readiness checks until it succeeds, ensuring that those probes do not interfere with the application startup. This can be used to adopt liveness checks on slow starting containers, avoiding them getting terminated by the kubelet before they are started.
Procedure
- Log in to the CCE console.
- When creating a workload, select Health Check in Container Information.
- Select and configure an appropriate probe based on service requirements. The parameters of the liveness, readiness, and startup probes are basically the same. A liveness probe is used as an example to describe the parameters. The parameter meanings of other probes are the same. Figure 1 Configuring health checks
Table 1 Probe parameters Parameter
Description
Check Method
There are four options. Select one based on your service scenario. For details about the specific parameters of each check method, see Specific Parameters in this section. For details about common parameters such as the check period and delay, see Table 6.
- HTTP (httpGet): applies to a container that provides services over HTTP/HTTPS. The cluster will periodically initiate an HTTP/HTTPS GET request to the container. If the HTTP/HTTPS response status code is within 200–399, the probe is successful. Otherwise, the probe fails. You must specify the port that the container listens on.
- TCP (tcpSocket): applies to a container that provides services over TCP (such as databases, caches, and custom TCP services). The cluster will periodically establish a TCP connection with the container. If the connection is successful, the probe is successful. Otherwise, the probe fails. You must specify the port that the container listens on.
- Command (exec): You must specify an executable command in a container. The cluster will periodically run the command in the container. If the command output is 0, the health check is successful. Otherwise, the health check fails. CAUTION:
In a high-load environment, do not use command-based health checks because they consume system resources. If system resources are insufficient, such as high CPU usage or a locked filesystem, health checks may time out and are marked as failed. If you must use command-based health checks, follow these guidelines:
- Raise the failure threshold and extend the timeout so transient resource spikes do not time out the health check. However, this will lower service sensitivity, so tune with care.
- Set appropriate CPU limits on service containers and system add-ons. Otherwise, time-slice starvation can keep kernel locks held and block exec probes for every pod on the node.
- gRPC Check (grpc): applies to gRPC applications. You do not need to expose HTTP ports or depend on external executable scripts. The container health checks can be implemented through standard gRPC APIs.
- Configure other parameters and click Create Workload in the lower right corner. If the workload is in the Running state, the startup command is successfully executed.
Specific Parameters
- HTTP (httpGet)
This option applies to a container that provides services over HTTP/HTTPS. The cluster will periodically send an HTTP/HTTPS GET request to the container and determine the probe results based on HTTP status codes. The details are as follows:
- If the response status code is within 200–399, the probe is successful.
- If the response status code is not within 200–399, the probe fails.
The following describes the parameters specific to the HTTP request-based check. The example values in Table 2 indicate that the cluster periodically sends "GET http://172.16.0.186:80/health-check" to the container to check its health.
Figure 2 HTTP request-based health check
Table 2 Parameters specific to an HTTP request-based health check Parameter
Example Value
Description
Path
/health-check
HTTP or HTTPS request path for the health check. Use an absolute path starting with a slash (/).
Port
80
(Mandatory) Container listening port, which is used to locate the container in the pod.
Host Address
172.16.0.186
IP address of the requested host. If this parameter is not set, the IP address of the pod is used by default.
Protocol
HTTP
Protocol used to send requests. The value must match the service type provided by the container.
- TCP (tcpSocket)
This option applies to a container that provides services over TCP (such as databases, caches, and custom TCP services). The cluster will periodically establish a TCP connection to the container to check the container health. If the connection is successful, the probe is successful. Otherwise, the probe fails. You must specify the port that the container listens on.
The following describes the parameters specific to a TCP port–based health check. Assume that the listening port of an Nginx container is 80. If a TCP probe is configured for the container and the probe port is set to 80, the cluster will periodically establish a TCP connection to the port. If the connection is successful, the probe is successful. Otherwise, the probe fails.
Figure 3 TCP port–based health check
Table 3 Parameters specific to a TCP port–based health check Parameter
Example Value
Description
Port
80
(Mandatory) Container listening port, which is used to locate the container in the pod.
- Command (exec)
This option is a flexible health check method that allows you to specify the command in a container. The cluster will periodically run the command in the container to check the container status. If the exit code is 0, the health check is successful. Otherwise, the health check fails.
For HTTP request- and TCP port–based health checks, you can also specify commands to achieve similar effects. For example, the example values in Table 4 can achieve the effect of HTTP request-based checks.
In a high-load environment, do not use command-based health checks because they consume system resources. If system resources are insufficient, such as high CPU usage or a locked filesystem, health checks may time out and are marked as failed. If you must use command-based health checks, follow these guidelines:
- Raise the failure threshold and extend the timeout so transient resource spikes do not time out the health check. However, this will lower service sensitivity, so tune with care.
- Set appropriate CPU limits on service containers and system add-ons. Otherwise, time-slice starvation can keep kernel locks held and block exec probes for every pod on the node.
Figure 4 Command-based health check
Table 4 Parameters specific to a command-based health check Parameter
Example Value
Description
Command
/bin/sh
-c
curl -sf http://172.16.0.186:80/health-check || exit 1
Command executed in the container to check the container status.
NOTE:- Before using this method, you must pack the program or tool into the container image. The cluster will directly execute the command in the container, so the file systems of the host machine or other containers cannot be accessed. If the program or tool (such as curl, nc, or a custom script) that the command depends on is not included in the image, "Command not found" is displayed.
- If the command is a shell script, you must specify the script parser. The cluster does not execute probes in an interactive terminal environment. For this reason, you cannot directly execute the script as a command. You must use the script editor to invoke the script. For example, if the script is located in /data/scripts/health_check.sh, you need to execute sh /data/scripts/health_check.sh.
- gRPC Check (grpc)
This option applies to gRPC applications. You do not need to expose HTTP ports or depend on external executable scripts. The container health check can be implemented through standard gRPC APIs. A gRPC check can be used only if the following conditions are met:
- The CCE cluster version is v1.25 or later.
- To use gRPC for health checks, your application must support gRPC as the health check protocol.
- The port is correct. Similar to HTTP request- and TCP port–based health checks, if the port is incorrect, the application does not support the health check protocol, or there are another configuration error, the probe fails.
Figure 5 gRPC check
Table 5 Parameters specific to a gRPC check Parameter
Example Value
Description
Port
80
(Mandatory) Container listening port, which is used to locate the container in the pod.
Common Parameters
| Parameter | Description | Example YAML |
|---|---|---|
| Period (periodSeconds) | Interval between probe checks, in seconds. For example, if this parameter is set to 30, the health check is performed every 30 seconds. | The following uses the TCP port–based health check of a liveness probe as an example. The configurations of other check methods are similar. ...
livenessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 0
timeoutSeconds: 1
periodSeconds: 10
successThreshold: 1
failureThreshold: 3
... |
| Delay (initialDelaySeconds) | Grace period for container startup before health checks begin, in seconds. For example, if this parameter is set to 30, the health check starts 30 seconds after the container starts. | |
| Timeout (timeoutSeconds) | Maximum time to wait for a probe response, in seconds. If it is set to 0 or left blank, the default value (1) will be used. For example, if this parameter is set to 10, the health check timeout is 10 seconds. The probe fails if the response takes longer than 10 seconds. | |
| Success Threshold (successThreshold) | Minimum consecutive successful probes required to mark a container healthy after a failure. The default value is 1, which is also the minimum value. This parameter must be set to 1 for liveness and startup probes. For example, if this parameter is set to 1, the container recovers after one successful probe following a failure. | |
| Failure Threshold (failureThreshold) | Consecutive probe failures before marking a container unhealthy. The default value is 3, and the minimum value is 1.
|
YAML Example
- Use kubectl to access the cluster. For details, see Accessing a Cluster Using kubectl.
- Create a YAML file for configuring a pod. In this example, the file name is health_check.yaml. You can change it as needed.
vim health_check.yamlThe file content is as follows:
apiVersion: v1 kind: Pod metadata: labels: test: liveness name: liveness-http spec: containers: - name: liveness image: nginx:alpine args: - /server livenessProbe: # Liveness probe httpGet: # HTTP request-based health check path: /healthz # The HTTP check path is /healthz. port: 80 # The health check port is 80. httpHeaders: # (Optional) The request header name is Custom-Header, and the value is Awesome. - name: Custom-Header value: Awesome initialDelaySeconds: 3 periodSeconds: 3 readinessProbe: # Readiness probe exec: # Command-based health check command: # Command to be executed - cat - /tmp/healthy initialDelaySeconds: 5 periodSeconds: 5 startupProbe: # Startup probe httpGet: # HTTP request-based health check path: /healthz # The HTTP check path is /healthz. port: 80 # The health check port is 80. failureThreshold: 30 periodSeconds: 10 - Create a pod.
kubectl create -f health_check.yamlIf information similar to the following is displayed, the pod is being created:
pod/liveness-http created
- Check the pod status.
kubectl get deployment
If the pod status is Running in the command output, the pod has been created.
NAME READY STATUS RESTARTS AGE liveness-http 1/1 Running 1 4m59s
Helpful Links
- To learn more about workload parameters, see Creating a Workload.
- After a workload is created, you can upgrade it, edit its YAML file, and view its logs. For details, see Managing Workloads.
- If the workload fails to be created, rectify the fault by referring to Workload Exception Troubleshooting.
What is your overall rating for this page?
Thank 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