A health check periodically performs diagnostics on a running container using any combination of the readiness, liveness, and startup health checks.
You can include one or more probes in the specification for the pod that contains the container which you want to perform the health checks.
Note
|
If you want to add or edit health checks in an existing pod, you must edit the pod |
- Readiness probe
-
A readiness probe determines if a container is ready to accept service requests. If the readiness probe fails for a container, the kubelet removes the pod from the list of available service endpoints.
After a failure, the probe continues to examine the pod. If the pod becomes available, the kubelet adds the pod to the list of available service endpoints.
- Liveness health check
-
A liveness probe determines if a container is still running. If the liveness probe fails due to a condition such as a deadlock, the kubelet kills the container. The pod then responds based on its restart policy.
For example, a liveness probe on a pod with a
restartPolicy
ofAlways
orOnFailure
kills and restarts the container. - Startup probe
-
A startup probe indicates whether the application within a container is started. All other probes are disabled until the startup succeeds. If the startup probe does not succeed within a specified time period, the kubelet kills the container, and the container is subject to the pod
restartPolicy
.Some applications can require additional startup time on their first initialization. You can use a startup probe with a liveness or readiness probe to delay that probe long enough to handle lengthy start-up time using the
failureThreshold
andperiodSeconds
parameters.For example, you can add a startup probe, with a
failureThreshold
of 30 failures and aperiodSeconds
of 10 seconds (30 * 10s = 300s) for a maximum of 5 minutes, to a liveness probe. After the startup probe succeeds the first time, the liveness probe takes over.
You can configure liveness, readiness, and startup probes with any of the following types of tests:
-
HTTP
GET
: When using an HTTPGET
test, the test determines the healthiness of the container by using a web hook. The test is successful if the HTTP response code is between200
and399
.You can use an HTTP
GET
test with applications that return HTTP status codes when completely initialized. -
Container Command: When using a container command test, the probe executes a command inside the container. The probe is successful if the test exits with a
0
status. -
TCP socket: When using a TCP socket test, the probe attempts to open a socket to the container. The container is only considered healthy if the probe can establish a connection. You can use a TCP socket test with applications that do not start listening until initialization is complete.
You can configure several fields to control the behavior of a probe:
-
initialDelaySeconds
: The time, in seconds, after the container starts before the probe can be scheduled. The default is 0. -
periodSeconds
: The delay, in seconds, between performing probes. The default is10
. This value must be greater thantimeoutSeconds
. -
timeoutSeconds
: The number of seconds of inactivity after which the probe times out and the container is assumed to have failed. The default is1
. This value must be lower thanperiodSeconds
. -
successThreshold
: The number of times that the probe must report success after a failure to reset the container status to successful. The value must be1
for a liveness probe. The default is1
. -
failureThreshold
: The number of times that the probe is allowed to fail. The default is 3. After the specified attempts:-
for a liveness probe, the container is restarted
-
for a readiness probe, the pod is marked
Unready
-
for a startup probe, the container is killed and is subject to the pod’s
restartPolicy
-
The following are samples of different probes as they would appear in an object specification.
apiVersion: v1
kind: Pod
metadata:
labels:
test: health-check
name: my-application
# ...
spec:
containers:
- name: goproxy-app (1)
args:
image: registry.k8s.io/goproxy:0.1 (2)
readinessProbe: (3)
exec: (4)
command: (5)
- cat
- /tmp/healthy
# ...
-
The container name.
-
The container image to deploy.
-
A readiness probe.
-
A container command test.
-
The commands to execute on the container.
apiVersion: v1
kind: Pod
metadata:
labels:
test: health-check
name: my-application
# ...
spec:
containers:
- name: goproxy-app (1)
args:
image: registry.k8s.io/goproxy:0.1 (2)
livenessProbe: (3)
httpGet: (4)
scheme: HTTPS (5)
path: /healthz
port: 8080 (6)
httpHeaders:
- name: X-Custom-Header
value: Awesome
startupProbe: (7)
httpGet: (8)
path: /healthz
port: 8080 (9)
failureThreshold: 30 (10)
periodSeconds: 10 (11)
# ...
-
The container name.
-
Specify the container image to deploy.
-
A liveness probe.
-
An HTTP
GET
test. -
The internet scheme:
HTTP
orHTTPS
. The default value isHTTP
. -
The port on which the container is listening.
-
A startup probe.
-
An HTTP
GET
test. -
The port on which the container is listening.
-
The number of times to try the probe after a failure.
-
The number of seconds to perform the probe.
apiVersion: v1
kind: Pod
metadata:
labels:
test: health-check
name: my-application
# ...
spec:
containers:
- name: goproxy-app (1)
args:
image: registry.k8s.io/goproxy:0.1 (2)
livenessProbe: (3)
exec: (4)
command: (5)
- /bin/bash
- '-c'
- timeout 60 /opt/eap/bin/livenessProbe.sh
periodSeconds: 10 (6)
successThreshold: 1 (7)
failureThreshold: 3 (8)
# ...
-
The container name.
-
Specify the container image to deploy.
-
The liveness probe.
-
The type of probe, here a container command probe.
-
The command line to execute inside the container.
-
How often in seconds to perform the probe.
-
The number of consecutive successes needed to show success after a failure.
-
The number of times to try the probe after a failure.
kind: Deployment
apiVersion: apps/v1
metadata:
labels:
test: health-check
name: my-application
spec:
# ...
template:
spec:
containers:
- resources: {}
readinessProbe: (1)
tcpSocket:
port: 8080
timeoutSeconds: 1
periodSeconds: 10
successThreshold: 1
failureThreshold: 3
terminationMessagePath: /dev/termination-log
name: ruby-ex
livenessProbe: (2)
tcpSocket:
port: 8080
initialDelaySeconds: 15
timeoutSeconds: 1
periodSeconds: 10
successThreshold: 1
failureThreshold: 3
# ...
-
The readiness probe.
-
The liveness probe.