Mastering Kubernetes Probes: Liveness and Readiness
In the dynamic world of container orchestration, Kubernetes offers robust mechanisms to ensure the health and stability of your applications. Two key components of this are liveness and readiness probes, which help Kubernetes understand when to restart a Container and when to remove it from service rotations. Let's delve into the intricacies of these probes to make the most of your Kubernetes clusters.
Understanding Kubernetes Probes
Probes in Kubernetes are HTTP or TCP requests that Kubernetes sends to your application to determine its health and readiness. They are configured as part of a Pod's specification and are used by the kubelet to monitor the health of your application. When a probe fails, the kubelet takes specific actions based on the type of probe.
Liveness Probes: Restarting Unresponsive Containers
Liveness probes help Kubernetes determine when to restart a Container. If a liveness probe fails, Kubernetes kills and restarts the Container. This is particularly useful for applications that may occasionally hang or become unresponsive.

- Example of a liveness probe:
livenessProbe: httpGet: path: /healthz port: 8080 initialDelaySeconds: 15 timeoutSeconds: 5
In this example, Kubernetes sends a GET request to the /healthz endpoint on port 8080. If the request fails, Kubernetes restarts the Container. The initialDelaySeconds and timeoutSeconds fields ensure that the probe doesn't interfere with the startup of the application.
Readiness Probes: Removing Unready Containers from Service Rotations
Readiness probes tell Kubernetes when a Container is ready to start receiving traffic. When a pod is not ready, Kubernetes removes it from the service rotation. This is useful for applications that have a startup phase where they are not yet ready to receive traffic.
- Example of a readiness probe:
readinessProbe: httpGet: path: /readyz port: 8080 initialDelaySeconds: 5 periodSeconds: 5
In this example, Kubernetes sends a GET request to the /readyz endpoint on port 8080 every 5 seconds. If the request fails, Kubernetes removes the pod from the service rotation. The initialDelaySeconds field ensures that the probe doesn't interfere with the startup of the application.

Combining Liveness and Readiness Probes
In some cases, you may want to use both liveness and readiness probes. For example, you might have an application that has a startup phase where it's not ready to receive traffic, but once it's ready, it should never hang or become unresponsive. In this case, you would use a readiness probe to remove the pod from the service rotation during startup, and a liveness probe to restart the container if it becomes unresponsive.
Probe Types and Failure Thresholds
Kubernetes supports three types of probes: HTTP, TCP, and exec. HTTP and TCP probes send a request to the application, while exec probes run a command in the container and expect a specific exit code. Each probe type has its own use cases and trade-offs, and the choice of probe type depends on your application's health and readiness indicators.
Kubernetes also allows you to configure the failure threshold for probes. By default, Kubernetes considers a probe to have failed if it fails three times in a row. However, you can configure the number of failures and the period over which they occur to suit your application's needs.

Best Practices for Using Probes
Here are some best practices for using probes in Kubernetes:
- Use liveness probes to restart unresponsive Containers.
- Use readiness probes to remove unready Containers from service rotations.
- Configure initial delay seconds to prevent probes from interfering with the startup of your application.
- Use the appropriate probe type for your application's health and readiness indicators.
- Configure failure thresholds to suit your application's needs.
- Regularly review and update your probes as your application evolves.
By following these best practices, you can ensure that your Kubernetes clusters are robust, resilient, and well-suited to the needs of your applications.






















