Tencent Cloud container clusters are built on the Kubernetes kernel. Kubernetes supports periodic probing of containers, determining their health status based on the probe results, and performing additional actions accordingly.
Health Check Types
Health checks are divided into the following types:
Container Liveness Check: Used to detect whether a container is alive, similar to running the ps command to check if a process exists. If the container's liveness check fails, the cluster will restart the container. If the container's liveness check succeeds, no action will be taken.
Container Readiness Check: Used to determine whether a container is ready to handle user requests. For example, when an application takes a long time to start, it may need to load disk data or depend on an external module to start before it can provide services. In this case, a container readiness check can be used to inspect the application process and confirm whether it has started successfully. If the container's readiness check fails, the cluster will block requests to access the container. If the container's readiness check succeeds, access to the container will be allowed.
Health Check Methods
TCP Port Probe
The principle of TCP port probing is as follows:
For containers providing TCP communication services, the cluster periodically establishes TCP connections to the container. If the connection is successful, the probe is considered successful; otherwise, it fails. When choosing TCP port probing, the listening port of the container must be specified.
For example, a Redis container has a service port of 6379. If we configure TCP port probing for this container and specify the probing port as 6379, the cluster will periodically initiate TCP connections to the container's 6379 port. If the connection is successful, the check is considered successful; otherwise, it fails.
HTTP Request Probe
HTTP request probing is designed for containers providing HTTP/HTTPS services, and the cluster periodically sends HTTP/HTTPS GET requests to the container. If the HTTP/HTTPS response code falls within the range of 200 - 399, the probe is considered successful; otherwise, it fails. When using HTTP request probing, you must specify the container's listening port and the HTTP/HTTPS request path.
For example, for a container providing HTTP services with a server port of 80 and an HTTP check path of
/health-check, the cluster will periodically send GET http://containerIP:80/health-check requests to the container.Run Command Check
Executable command check is a powerful inspection method that requires users to specify an executable command within a container. The cluster will periodically execute this command inside the container. If the command returns a result of 0, the check is successful; otherwise, it fails.
For TCP port probing and HTTP request probing, both can be replaced by using the executable command check method:
For TCP port probing, you can write a program to connect to the container's port. If the connection is successful, the script returns 0; otherwise, it returns -1.
For HTTP request probes, you can create a script to perform a wget on the container and check the response code. For example,
wget http://127.0.0.1:80/health-check. If the response code is within the range of 200 - 399, the script returns 0; otherwise, it returns -1.Supports and Limits
The program to be run must be placed in the image of the container; otherwise, the run will fail as the program cannot be found.
If the command being executed is a shell script, you cannot directly specify the script as the execution command; instead, you need to add the script interpreter. For example, if the script is
/data/scripts/health_check.sh, then when using the execution command check, the specified program should be:sh
/data/scripts/health_check.sh
1.1 On the "Deployment" page of the cluster, click Create.
1.2 Navigate to the "Create Workload" page and select Show Advanced Settings located below the "Container Instance" module.
1.3 In "Container Health Check", using Liveness Check as an example, set the following parameters.
Check Method: Select "Executable Command Check".
Execute Command: Enter the following content.
sh
/data/scripts/health_check.sh
1.4 For other parameter settings, please refer to Deployment Management.
Other Common Parameters
Startup Delay: Measured in seconds. Specifies the duration after the container starts before probing begins. For example, if the startup delay is set to 5, health checks will begin 5 seconds after the container starts.
Interval Time: Measured in seconds. Specifies the frequency of health checks. For example, if the interval time is set to 10, the cluster will perform a check every 10 seconds.
Response Timeout: Unit in seconds. Specifies the timeout duration for health probes. For TCP port probing, HTTP request probing, and command execution checks, this represents the TCP connection timeout, HTTP request response timeout, and command execution timeout, respectively.
Healthy Threshold: Measured in occurrences. Specifies how many consecutive successful health checks are required before a container is considered healthy. For example, if the healthy threshold is set to 3, it means that the container is considered healthy only if it passes 3 consecutive probes successfully.
Note
If the type of health check is a survival check, then the healthy threshold can only be 1, and other values you set will be considered invalid.
Unhealthy Threshold: Measured in occurrences. Specifies how many consecutive health check failures are required before a container is considered unhealthy. For example, if the unhealthy threshold is set to 3, it means that the container is considered unhealthy only after failing the probe 3 consecutive times.