Kubernetes
k8s
livenessProbe
readinessProbe
container-health-checks

k8s - livenessProbe vs readinessProbe

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Understanding Liveness Probe vs Readiness Probe in Kubernetes

As organizations deploy applications at scale, Kubernetes (K8s) has become a go-to platform for container orchestration due to its robust management capabilities. Two critical features in Kubernetes that help maintain the health of applications are the livenessProbe and readinessProbe. Understanding the distinction between them is essential for ensuring that applications are both resilient and performant in a Kubernetes environment.

Probes Overview

Probes in Kubernetes are diagnostic checks performed on running containers to determine their health and status. These probes are part of a Pod specification and are crucial for maintaining application reliability and availability.

Liveness Probe

The liveness probe is responsible for ensuring that an application is alive. If a liveness probe fails, Kubernetes assumes that the container is unhealthy and restarts it.

  • Purpose: To detect and remedy pod failures that render the application non-functional.
  • Common Use-Cases:
    • Application deadlock detection where the application is running but unable to progress.
    • Critical application faults requiring a restart for recovery.
  • Technical Implementation: A typical liveness probe can be implemented using HTTP, TCP, or as an exec command.
yaml
1  livenessProbe:
2    httpGet:
3      path: /healthz
4      port: 8080
5    initialDelaySeconds: 10
6    periodSeconds: 10

In this example, Kubernetes will check the /healthz endpoint on port 8080 every 10 seconds, starting 10 seconds after the container starts.

Readiness Probe

The readiness probe determines if a container is ready to accept traffic. If the probe fails, Kubernetes will remove the pod from service endpoints.

  • Purpose: To ensure that traffic is only directed to pods capable of serving requests.
  • Common Use-Cases:
    • Applications that have a startup time before becoming fully operational.
    • Services that depend on external systems or configurations.
  • Technical Implementation: Similar to liveness probes, readiness probes can be specified using HTTP requests, TCP sockets, or executed commands.
yaml
1  readinessProbe:
2    tcpSocket:
3      port: 3306
4    initialDelaySeconds: 5
5    periodSeconds: 10

Here, Kubernetes checks if the application is ready to serve on port 3306, with a slight delay after starting the pod.

Comparison Table

AspectLiveness ProbeReadiness Probe
PurposeEnsures container is running and healthyEnsures container is ready to serve traffic
Action on FailureRestarts the containerRemoves pod from the list of service endpoints
Use-CaseRecover non-functioning applicationsAvoid routing to non-ready applications
TriggersIssues like deadlocks, critical faultsApplication initialization, dependency readiness
FrequencyUser-defined; periodic checksUser-defined; periodic checks

Additional Considerations

  • Customization Options: Both probes can be customized with initialDelaySeconds, periodSeconds, timeoutSeconds, successThreshold, and failureThreshold to fine-tune their behavior according to the application's demands.
  • Simultaneous Use: It's possible, and often advantageous, to use both liveness and readiness probes simultaneously in a deployment to cover all bases for application monitoring and management.
  • Impact on Performance: Misconfigured probes can lead to unnecessary restarts or service disruptions. For instance, if a readiness probe is too aggressive, it might not allow sufficient time for an application to become ready, leading to a quasi-DDoS scenario.
  • Debugging Probes: When debugging probe-related issues, leverage Kubernetes events and logs. The kubectl describe command can provide insights into the last probe checks' results.

Conclusion

In summary, Kubernetes' liveness and readiness probes serve distinct but complementary purposes. Correctly configuring and utilizing these probes enhances fault tolerance and ensures that applications can dynamically adapt to changing states without manual intervention. Mastering these probes is a critical skill for any Kubernetes practitioner aiming to deploy resilient and scalable applications.


Course illustration
Course illustration

All Rights Reserved.