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.
|
|
Readiness probe | Readiness probes are used to check whether a container is ready to serve requests.
| 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.