Kubernetes Observability: Readiness and Liveness Probes & Container Logging

🗼Introduction

Kubernetes is a powerful container orchestration platform that enables the deployment and management of containerized applications. To ensure the reliability and high availability of these applications, Kubernetes provides two essential mechanisms known as Readiness and Liveness Probes. These probes play a crucial role in determining the health and operational status of pods running within a cluster. In this blog, we will delve into the concepts of Readiness and Liveness Probes, explore their significance, and provide practical examples to demonstrate their usage effectively.

🗼Pod Status and Conditions:

Before deep dive into observability first we need to understand few diffenect stages in the lifecycle of the Pod.
Kubernetes maintains the status of each pod, indicating whether it is running, completed, or has encountered an error. Additionally, Kubernetes defines specific pod conditions, which include:

  • PodScheduled: The pod is scheduled to run on a node.

  • Initialized: All the init containers have been successfully initialized.

  • Ready: The pod has passed the Readiness Probe and is ready to serve requests.

  • ContainersReady: All containers in the pod have passed their readiness checks.

  • PodInitialized: The pod has been initialized successfully.

🗼Probes in Kubernetes

Kubernetes probes are tools used to assess the health of containers running within a pod. There are two types of probes:

  1. Readiness Probes: Determine if a container is ready to start accepting traffic.

  2. Liveness Probes: Check if a container is still running and functioning correctly.

🎗️Readiness Probes

Readiness probes indicate whether a container is ready to serve requests. They are particularly useful during the startup phase of an application or during updates and scaling events. Kubernetes uses the result of the readiness probe to decide if a container should be included in the load balancer's pool. If the readiness probe fails, Kubernetes will stop sending traffic to that container until it passes.

For example:

readinessProbe:
   failureThreshold: 3
   httpGet:
     path: /health
     port: 8080
     scheme: HTTP
   initialDelaySeconds: 60
   periodSeconds: 10
   successThreshold: 1
   timeoutSeconds: 2

In the above example:

  • httpGet: Specifies that an HTTP GET request will be made to the /health endpoint on port 8080.

  • initialDelaySeconds: Specifies the initial delay before the probe is initiated.

  • periodSeconds: Specifies the frequency at which the probe is performed.

🎗️Liveness Probes

Liveness probes ensure that a container is still running. If a liveness probe fails, Kubernetes will kill the container and start a new one. This helps in recovering from application crashes, deadlocks, or other failures that render a container unusable. Kubernetes continuously checks the liveness probe. If the probe fails, the container is considered unhealthy and is terminated and restarted.

For example:

 livenessProbe:
   failureThreshold: 3
   httpGet:
     path: /health
     port: 8080
     scheme: HTTP
   initialDelaySeconds: 60
   periodSeconds: 10
   successThreshold: 1
   timeoutSeconds: 2
  • httpGet: Specifies an HTTP GET request to the /health endpoint on port 5000.

  • initialDelaySeconds: The initial delay before the probe is initiated (10 seconds).

  • periodSeconds: How often the probe is performed (every 5 seconds).

  • timeoutSeconds: The time to wait for a probe response (2 seconds).

  • failureThreshold: The number of consecutive failures needed before the pod is marked as not ready (3 times).

Here is the complete YAML configuration for the Kubernetes deployment, including both readiness and liveness probes:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp-deployment
  labels:
    app: webapp
spec:
  replicas: 3
  selector:
    matchLabels:
      app: webapp
  template:
    metadata:
      labels:
        app: webapp
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
        readinessProbe:
          httpGet:
            path: /
            port: 80
            scheme: HTTP
          initialDelaySeconds: 60
          periodSeconds: 10
          timeoutSeconds: 2
          failureThreshold: 3
          successThreshold: 1
        livenessProbe:
          httpGet:
            path: /
            port: 80
            scheme: HTTP
          initialDelaySeconds: 60
          periodSeconds: 10
          timeoutSeconds: 2
          failureThreshold: 3
          successThreshold: 1

🗼Conclusion

Readiness and liveness probes are powerful features in Kubernetes that help ensure the reliability and availability of your applications. By carefully implementing and tuning these probes, you can significantly reduce downtime, improve user experience, and maintain the overall health of your Kubernetes environment. Remember to keep your probes simple, adjust parameters to fit your application's needs, and continuously monitor their performance. With these practices, you'll be well on your way to building robust and resilient applications on Kubernetes.

Did you find this article valuable?

Support Ashutosh Mahajan by becoming a sponsor. Any amount is appreciated!