All Products
Search
Document Center

Elastic Container Instance:Use probes to perform health checks on containers

Last Updated:Jan 14, 2025

Liveness probes and readiness probes are mechanisms that are used to check the status of containers. Liveness probes are used to check whether containers are running as expected, and readiness probes are used to check whether containers are ready. This topic describes how to configure liveness probes and readiness probes to perform health checks on containers. This way, Kubernetes can better monitor and manage the status of containers to ensure high availability and stability of services.

Feature description

In Kubernetes, the kubelet uses liveness probes and readiness probes to check the status and running of containers on a regular basis.

Probe

Description

Scenario

Liveness probe

Liveness probes are used to check whether a container is working as expected.

  • If the check is successful, the container is working as expected.

  • If the check fails, the system determines whether to restart the container based on the configured container restart policy.

  • By default, if a liveness probe is not configured, the container is considered to be working as expected at all times.

  • When an application is running but no further operations can be performed on the application, liveness probes can capture the deadlock. Then, the system restarts the container to make the application run as expected regardless of whether bugs exist.

  • After applications run for an extended period of time, the applications may eventually transition to the broken state. If you want to restore the applications, you must restart the applications. You can use liveness probes to detect and remedy such situations.

Readiness probe

Readiness probes are used to check whether a container is ready to serve requests.

  • If the check is successful, the container is ready to receive business requests.

  • If the check fails, the container is not ready and the system does not send requests to the container until the recheck is successful.

  • By default, if a readiness probe is not configured, the container is considered to be ready.

An application may be temporarily unable to serve external requests when the application loads a large amount of data or configuration files during startup. In this case, if you do not want to terminate the application or send requests to the application, you can use readiness probes to detect and take action to mitigate such situations.

Configuration examples

You can configure liveness probes and readiness probes by using the livenessProbe and readinessProbe parameters of the container. For more information, see Configure Liveness, Readiness and Startup Probes.

Example 1: Configure a liveness probe

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test
  labels:
    app: test
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      name: nginx-test
      labels:
        app: nginx
        alibabacloud.com/eci: "true" 
    spec:
      containers:
      - name: nginx
        image: registry.cn-shanghai.aliyuncs.com/eci_open/nginx:1.14.2
        ports:
        - containerPort: 80
        args:
        - /bin/sh
        - -c
        - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
       # Configure a liveness probe to check containers by using the CLI mode.
        livenessProbe:    
          exec:           
            command:
            - cat
            - /tmp/healthy
          initialDelaySeconds: 5    # The check starts 5 seconds after the container is started.
          periodSeconds: 5 # The check is performed every 5 seconds. 

Example 2: Configure a readiness probe

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test
  labels:
    app: test
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      name: nginx-test
      labels:
        app: nginx
        alibabacloud.com/eci: "true" 
    spec:
      containers:
      - name: nginx
        image: registry.cn-shanghai.aliyuncs.com/eci_open/nginx:1.14.2
        ports:
        - containerPort: 80
        args:
        - /bin/sh
        - -c
        - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
        # Configure a readiness probe to check containers by using the CLI mode.
        readinessProbe:
          exec:           
            command:
            - cat
            - /tmp/healthy
          initialDelaySeconds: 5    # The check starts 5 seconds after the container is started.
          periodSeconds: 5 # The check is performed every 5 seconds.