Health Checks
Learn how Kubernets checks the application health, and how to use liveness probes and readiness probes.
Last updated
Learn how Kubernets checks the application health, and how to use liveness probes and readiness probes.
Last updated
This section continues from the previous section - make sure you do the tutorial in sequence.
Spring Boot Actuator can provide some basic health checking mechanisms via the /actuator/health
endpoint. However, which endpoint you use depends on the Spring Boot version.
Spring Boot 2.3 and above, Spring Boot Actuator has dedicated support for Liveness Probe.
Spring Boot < 2.3 and below, it's best to create a simple endpoint that simply returns HTTP 200
response status instead of using the Spring Boot Actuator's /actuator/health
endpoint. This is because /actuator/health
by default may fail if an external dependency fails.
Use Jib to containerize the application:
Learn different ways to containerize a Java application in the Container Image section.
Kubernetes can automatically detect application issues using a Liveness Probe. When the Liveness Probe check fails, Kubernetes will automatically restart the container, in case restarting your application helps it to recover. If the container continues to fail the Liveness Probe, Kubernetes will go into a Crash Loop and backs off the restart exponentially.
Liveness Probe failure indicates to Kubernetes that the failure can be recovered after a restart.
If your Liveness Probe checks an endpoint that fails due to an external dependency, but the external dependency cannot recover simply because your container restarts, then it's not a good check! This type of checks may cause catastropic cascading failures.
In addition to httpGet
, you can also configure different type of probes such as exec
to execute a command to perform a non-HTTP check, or use tcpSocket
to simply check if a port is listening. See Kubernetes Configure Liveness, Readiness, and Startup Probes documentation for more details.
Notice the additional initialDelaySeconds
configuration. If your application starts slowly (e.g., 1 minute to start), and the livenessProbe
starts the check early (e.g., 10 seconds), then the livenessProbe
might never succeed - causing the application to always getting restarted.
When configuring a livenessProbe
, always consider the initial delay needed for your application.
Even if your application is alive, it doesn't mean that it's ready to receive traffic. For example, during the startup, the application is alive, but it needs to pre-load data, or warmup caches, before it's ready to accept traffic. A Readiness Probe will let Kubernetes know when your application is ready to receive traffic, and only then will the instance be enlisted into the load balancer as a backend to serve requests (i.e., a Service's Endpoint).
You should always configure a readinessProbe
. Even if you don't use Spring Boot Actuator, you can point the probe to /
or some endpoint that indicates the traffic is ready serve.
Spring Boot Version
Liveness Probe
Readiness Probe
>= 2.3
/actuator/health/liveness
/actuator/health/readiness
< 2.3
Any endpoint that simply returns 200
/actuator/health