Kubernetes - Liveness and Readiness
configure liveness, readiness and startup probes for Containers
Health Checks are the easiest ways to let the system know if an instance of your app is working. Defining Health check increase the system’s reliability and uptime.
There are two types of Health Checks
- Liveness
- Readiness
Liveness probes let Kubernetes know if your app is alive or dead. If you app is alive, then Kubernetes leaves it alone. If your app is dead, Kubernetes removes the Pod and starts a new one to replace it.
Readiness probes are designed to let Kubernetes know when your app is ready to serve traffic. Kubernetes makes sure the readiness probe passes before allowing a service to send traffic to the pod. If a readiness probe starts to fail, Kubernetes stops sending traffic to the pod until it passes.
You can have both Liveness and readiness probe for the same container.
There are three types of Probes
- HTTP
- Command
- TCP
Default HealthCheck
A container starts its process usually with Dockerfile’s CMD or ENTRYPOINT. If the process returns non zero code, then Kubernetes determines the container fails. Kubernetes will decide whether to restart the Pod based on restartPolicy
field.
Command Probe
exec-liveness.yaml:
1 | apiVersion: v1 |
This pod creates file /tmp/healthy, sleeps for 30 seconds, then remove the file.
The liveness probe will start after 5 second initial delay. The probe have a period of 5 second. The After 30 seconds, when the file is removed, liveness probe will fail. The pod will become unhealthy and gets restarted.
Http Probe
A more common use case will be probe the http service.
1 | apiVersion: v1 |
Readiness Probe
Readiness probes are configured similarly to liveness probes. The only difference is that you use the readinessProbe field instead of the livenessProbe field.
1 | apiVersion: v1 |
Check the pod status. Readiness probe will fail after 30 seconds. Status will become not ready.
1 | $ kubectl get pods |
readinessProbe and livenessProde can be the same:
1 | readinessProbe: |