Configuring Container Health Check

Scenario

Health check regularly checks the health status of containers during container 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:

Check Method

Common Parameters

Table 1 Common parameter description

Parameter

Description

Period (periodSeconds)

Indicates the probe detection period, in seconds.

For example, if this parameter is set to 30, the detection is performed every 30 seconds.

Delay (initialDelaySeconds)

Check delay time in seconds. Set this parameter according to the normal startup time of services.

For example, if this parameter is set to 30, the health check will be started 30 seconds after the container is started. The time is reserved for containerized services to start.

Timeout (timeoutSeconds)

Number of seconds after which the probe times out. Unit: second.

For example, if this parameter is set to 10, the timeout wait time for performing a health check is 10s. If the wait time elapses, the health check is regarded as a failure. If the parameter is left blank or set to 0, the default timeout time is 1s.

Success Threshold (successThreshold)

Minimum consecutive successes for the probe to be considered successful after having failed. For example, if this parameter is set to 1, the workload status is normal only when the health check is successful for one consecutive time after the health check fails.

The default value is 1, which is also the minimum value.

The value of this parameter is fixed to 1 in Liveness Probe and Startup Probe.

Failure Threshold (failureThreshold)

Number of retry times when the detection fails.

Giving up in case of liveness probe means to restart the container. In case of readiness probe the pod will be marked Unready.

The default value is 3, and the minimum value is 1.

YAML Example

apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: liveness-http
spec:
  containers:
  - name: liveness
    image: <image_address>
    args:
    - /server
    livenessProbe:                 # Liveness probe
      httpGet:                     # Checking an HTTP request is used as an example.
        path: /healthz             # The HTTP check path is /healthz.
        port: 80                   # The check port number 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:                        # Checking an execution command is used as an example.
        command:                   # Command to be executed
          - cat
          - /tmp/healthy
      initialDelaySeconds: 5
      periodSeconds: 5
    startupProbe:                  # Startup probe
      httpGet:                     # Checking an HTTP request is used as an example.
        path: /healthz             # The HTTP check path is /healthz.
        port: 80                   # The check port number is 80.
      failureThreshold: 30
      periodSeconds: 10