Health check regularly checks the health of containers when the containers are running. If health check is not configured, a pod cannot detect application exceptions or automatically restart the application to recover it. As a result, the pod may be in the Running state, but the application is unavailable or abnormal.
Kubernetes provides three types of health check probes to monitor the applications in containers for system stability and high availability.
For more information, see Liveness, Readiness, and Startup Probes and Configure Liveness, Readiness and Startup Probes.
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 Common Parameters.
|
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:
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.
Parameter | Example Value | Description |
|---|---|---|
Path | /health-check | HTTP or HTTPS request path for 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. |
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 the TCP port-based 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.
Parameter | Example Value | Description |
|---|---|---|
Port | 80 | (Mandatory) Container listening port, which is used to locate the container in the pod. |
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 status is 0, the health check is successful. Otherwise, the health check fails.
For TCP port- and HTTP request-based 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, you are advised not to run commands to check the health status. Running commands consumes system resources. If system resources are not enough (for example, when the CPU load is high or the file system is locked), the health check may fail due to a timeout. If you need to run a command to check the health status, you are advised to increase the number of allowed failures and extend the timeout interval to prevent the health check from failing due to unexpected resource competition.
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:
|
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:
Parameter | Example Value | Description |
|---|---|---|
Port | 80 | (Mandatory) Container listening port, which is used to locate the container in the pod. |
Parameter | Description | Example YAML |
|---|---|---|
Period (periodSeconds) | Probe detection period, in seconds. For example, if this parameter is set to 30, the probe is performed every 30 seconds. | The following uses a TCP port-based check of a liveness probe as an example. The configurations of other check methods are similar.
|
Delay (initialDelaySeconds) | Time reserved for service program startup, in seconds. For example, if this parameter is set to 30, the health check starts 30 seconds after the container starts. | |
Timeout (timeoutSeconds) | The timeout duration of a health check, in seconds. If you set this parameter to 0 or do not specify any value, the default value (1) is used. Example: Value 10 indicates that the health check timeout duration is 10s. If the health check takes longer than this value, the check is considered failed. | |
Success Threshold (successThreshold) | The minimum number of consecutive successes for the container to be considered healthy after a health check failure. The default value is 1, and the minimum value is 1. This parameter must be set to 1 for both the liveness and startup probes. For example, if this parameter is set to 1, the workload will be restored to the normal state if the health check succeeds for one time after a failure. | |
Failure Threshold (failureThreshold) | The number of consecutive failures allowed before the container is considered unhealthy. The default value is 3, and the minimum value is 1.
|
vim health_check.yaml
The file content is as follows:
apiVersion: v1kind: Podmetadata:labels:test: livenessname: liveness-httpspec:containers:- name: livenessimage: <image_address>args:- /serverlivenessProbe: # Liveness probehttpGet: # An HTTP request is used to check the containers.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-Headervalue: AwesomeinitialDelaySeconds: 3periodSeconds: 3readinessProbe: # Readiness probeexec: # A command is used to check the containers.command: # Command to be executed- cat- /tmp/healthyinitialDelaySeconds: 5periodSeconds: 5startupProbe: # Startup probehttpGet: # An HTTP request is used to check the containers.path: /healthz # The HTTP check path is /healthz.port: 80 # The health check port is 80.failureThreshold: 30periodSeconds: 10
kubectl create -f health_check.yaml
If information similar to the following is displayed, the pod is being created:
pod/liveness-http created
kubectl get deployment
If the pod status is Running in the command output, the pod has been created.
NAME READY STATUS RESTARTS AGEliveness-http 1/1 Running 1 4m59s