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 how to configure them.
A liveness probe detects issues where a container is running but unresponsive, such as deadlocks.
Check Method | Description | Specific Parameter | Common Parameter |
|---|---|---|---|
HTTP (httpGet) | This method applies to containers that expose HTTP or HTTPS services. The cluster periodically sends an HTTP/HTTPS GET request to the target container's endpoint. If the response status code is within the range 200–399, the probe is considered successful. If the response status code is outside this range, the probe fails, and the kubelet stops and restarts the container. |
|
|
TCP (tcpSocket) | This method applies to containers that expose TCP services (such as databases, caches, and custom TCP applications). The cluster periodically attempts to set up a TCP connection with the target container. If the connection is successful, the probe is healthy. Otherwise, the probe fails, and the kubelet stops and restarts the container. | Port: the container port exposed for health checks. Valid range: 1 to 65535. | |
Command (exec) | You need to specify an executable command for the cluster to periodically run inside the container. If the command exits with code 0, the probe is successful. Otherwise, the probe fails, and the kubelet stops and restarts the container. CAUTION: Avoid command-based probes in high-load environments because they consume system resources. If system resources are insufficient, such as high CPU usage or a locked filesystem, probes may time out and fail. If you must use them, follow these guidelines:
| Command: the command executed inside the container to check its status. Enter multiple commands on separate lines. NOTE:
| |
gRPC Check (grpc) | This method applies to gRPC applications. No HTTP ports or external scripts are required. Health checks use standard gRPC APIs. | Port: the container port exposed for health checks. Valid range: 1 to 65535. |
vim health_check.yaml
The following uses an HTTP request as an example. For details about other health check methods, see Configuring Liveness, Readiness and Startup Probes. File content:
apiVersion: apps/v1kind: Deploymentmetadata:name: nginxnamespace: defaultspec:replicas: 1selector:matchLabels:app: nginxversion: v1template:metadata:labels:app: nginxversion: v1spec:containers:- name: container-1image: nginx:latestlivenessProbe : # The liveness probehttpGet: # HTTP requests are used to check container health.path: / # The HTTP health check pathport: 80 # The health check port is 80 .host: '' # The host address, which defaults to the pod IP addressscheme: HTTP # The health check protocolhttpHeaders: # (Optional) The request header name is Custom-Header , and the value is Awesome .- name: Custom-Headervalue: AwesomeinitialDelaySeconds: 3 # The grace period for container startup before health checks begin, in secondstimeoutSeconds: 1 # The probe timeout, in secondsperiodSeconds: 3 # The probe check period, in secondssuccessThreshold: 1 # The minimum consecutive successful probes required to mark a container healthy after a failurefailureThreshold: 3 # The minimum consecutive probe failures before marking a container unhealthyimagePullSecrets:- name: default-secret
kubectl create -f health_check.yaml
If information similar to the following is displayed, the workload is being created:
deployment.apps/nginx created
kubectl get pod
If the pod status is Running, the workload has been created.
NAME READY STATUS RESTARTS AGEnginx-58cdd4f48d-jcsqn 1/1 Running 0 4m19s
A readiness probe determines whether a pod's containers are ready to receive traffic. A pod joins Service endpoints to receive traffic only after all its containers are ready.
Check Method | Description | Specific Parameter | Common Parameter |
|---|---|---|---|
HTTP (httpGet) | This method applies to containers that expose HTTP or HTTPS services. The cluster periodically sends an HTTP/HTTPS GET request to the target container's endpoint. If the response status code is within the range 200–399, the probe is considered successful. If the response status code is outside this range, the probe fails, and the kubelet stops and restarts the container. |
|
|
TCP (tcpSocket) | This method applies to containers that expose TCP services (such as databases, caches, and custom TCP applications). The cluster periodically attempts to set up a TCP connection with the target container. If the connection is successful, the probe is healthy. Otherwise, the probe fails, and the kubelet stops and restarts the container. | Port: the container port exposed for health checks. Valid range: 1 to 65535. | |
Command (exec) | You need to specify an executable command for the cluster to periodically run inside the container. If the command exits with code 0, the probe is successful. Otherwise, the probe fails, and the kubelet stops and restarts the container. CAUTION: Avoid command-based probes in high-load environments because they consume system resources. If system resources are insufficient, such as high CPU usage or a locked filesystem, probes may time out and fail. If you must use them, follow these guidelines:
| Command: the command executed inside the container to check its status. Enter multiple commands on separate lines. NOTE:
| |
gRPC Check (grpc) | This method applies to gRPC applications. No HTTP ports or external scripts are required. Health checks use standard gRPC APIs. | Port: the container port exposed for health checks. Valid range: 1 to 65535. |
vim health_check.yaml
The following uses an HTTP request as an example. For details about other health check methods, see Configuring Liveness, Readiness and Startup Probes. File content:
apiVersion: apps/v1kind: Deploymentmetadata:name: nginxnamespace: defaultspec:replicas: 1selector:matchLabels:app: nginxversion: v1template:metadata:labels:app: nginxversion: v1spec:containers:- name: container-1image: nginx:latestreadinessProbe : # The readiness probehttpGet: # HTTP requests are used to check container health.path: / # The HTTP health check pathport: 80 # The health check port is 80 .host: '' # The host address, which defaults to the pod IP addressscheme: HTTP # The health check protocolhttpHeaders: # (Optional) The request header name is Custom-Header , and the value is Awesome .- name: Custom-Headervalue: AwesomeinitialDelaySeconds: 3 # The grace period for container startup before health checks begin, in secondstimeoutSeconds: 1 # The probe timeout, in secondsperiodSeconds: 3 # The probe check period, in secondssuccessThreshold: 1 # The minimum consecutive successful probes required to mark a container healthy after a failurefailureThreshold: 3 # The minimum consecutive probe failures before marking a container unhealthyimagePullSecrets:- name: default-secret
kubectl create -f health_check.yaml
If information similar to the following is displayed, the workload is being created:
deployment.apps/nginx created
kubectl get pod
If the pod status is Running, the workload has been created.
NAME READY STATUS RESTARTS AGEnginx-58cdd4f48d-jcsqn 1/1 Running 0 4m19s
Startup probes run at container startup to verify initialization. They are used for slow-starting applications.
Check Method | Description | Specific Parameter | Common Parameter |
|---|---|---|---|
HTTP (httpGet) | This method applies to containers that expose HTTP or HTTPS services. The cluster periodically sends an HTTP/HTTPS GET request to the target container's endpoint. If the response status code is within the range 200–399, the probe is considered successful. If the response status code is outside this range, the probe fails, and the kubelet stops and restarts the container. |
|
|
TCP (tcpSocket) | This method applies to containers that expose TCP services (such as databases, caches, and custom TCP applications). The cluster periodically attempts to set up a TCP connection with the target container. If the connection is successful, the probe is healthy. Otherwise, the probe fails, and the kubelet stops and restarts the container. | Port: the container port exposed for health checks. Valid range: 1 to 65535. | |
Command (exec) | You need to specify an executable command for the cluster to periodically run inside the container. If the command exits with code 0, the probe is successful. Otherwise, the probe fails, and the kubelet stops and restarts the container. CAUTION: Avoid command-based probes in high-load environments because they consume system resources. If system resources are insufficient, such as high CPU usage or a locked filesystem, probes may time out and fail. If you must use them, follow these guidelines:
| Command: the command executed inside the container to check its status. Enter multiple commands on separate lines. NOTE:
| |
gRPC Check (grpc) | This method applies to gRPC applications. No HTTP ports or external scripts are required. Health checks use standard gRPC APIs. | Port: the container port exposed for health checks. Valid range: 1 to 65535. |
vim health_check.yaml
The following uses an HTTP request as an example. For details about other health check methods, see Configuring Liveness, Readiness and Startup Probes. File content:
apiVersion: apps/v1kind: Deploymentmetadata:name: nginxnamespace: defaultspec:replicas: 1selector:matchLabels:app: nginxversion: v1template:metadata:labels:app: nginxversion: v1spec:containers:- name: container-1image: nginx:lateststartupProbe : # The startup probehttpGet: # HTTP requests are used to check container health.path: / # The HTTP health check pathport: 80 # The health check port is 80 .host: '' # The host address, which defaults to the pod IP addressscheme: HTTP # The health check protocolhttpHeaders: # (Optional) The request header name is Custom-Header , and the value is Awesome .- name: Custom-Headervalue: AwesomeinitialDelaySeconds: 3 # The grace period for container startup before health checks begin, in secondstimeoutSeconds: 1 # The probe timeout, in secondsperiodSeconds: 3 # The probe check period, in secondssuccessThreshold: 1 # The minimum consecutive successful probes required to mark a container healthy after a failurefailureThreshold: 3 # The minimum consecutive probe failures before marking a container unhealthyimagePullSecrets:- name: default-secret
kubectl create -f health_check.yaml
If information similar to the following is displayed, the workload is being created:
deployment.apps/nginx created
kubectl get pod
If the pod status is Running, the workload has been created.
NAME READY STATUS RESTARTS AGEnginx-58cdd4f48d-jcsqn 1/1 Running 0 4m19s