Introduction

In Kubernetes, keeping your application healthy and available is very important. Kubernetes provides built-in features called liveness probes and readiness probes to monitor the health of your containers.

A liveness probe checks whether your application is still running properly. If it fails, Kubernetes will restart the container automatically.

A readiness probe checks whether your application is ready to receive traffic. If it fails, Kubernetes will temporarily stop sending requests to that container.

In simple terms:

These probes are essential for building reliable, scalable, and production-ready applications in Kubernetes.

Detailed Explanation with Examples

Liveness Probe

A liveness probe ensures your application is not stuck or crashed internally.

Example scenario:

Kubernetes will detect this using a liveness probe and restart the container.

Readiness Probe

A readiness probe ensures your application is fully ready before handling traffic.

Example scenario:

Kubernetes will wait until the readiness probe passes before sending traffic.

Types of Probes in Kubernetes

Kubernetes supports three main types of probes:

HTTP Probe

Checks an HTTP endpoint of your application.

TCP Probe

Checks if a specific port is open.

Command (Exec) Probe

Runs a command inside the container to check health.

Step-by-Step Configuration

Step 1: Create a Deployment YAML

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: my-app-image

Step 2: Add Liveness Probe

livenessProbe:
  httpGet:
    path: /health
    port: 80
  initialDelaySeconds: 10
  periodSeconds: 5

Explanation:

Step 3: Add Readiness Probe

readinessProbe:
  httpGet:
    path: /ready
    port: 80
  initialDelaySeconds: 5
  periodSeconds: 5

Explanation:

Step 4: Full Example

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: my-app-image
        ports:
        - containerPort: 80
        livenessProbe:
          httpGet:
            path: /health
            port: 80
          initialDelaySeconds: 10
          periodSeconds: 5
        readinessProbe:
          httpGet:
            path: /ready
            port: 80
          initialDelaySeconds: 5
          periodSeconds: 5

Real-Life Examples and Scenarios

Scenario 1: Application Crash Recovery

If your application crashes silently:

Scenario 2: Slow Startup Applications

Some applications take time to start.

Scenario 3: Rolling Updates

During deployments:

Real-World Use Cases

Advantages and Disadvantages

Advantages

Disadvantages

Comparison Table

FeatureLiveness ProbeReadiness Probe
PurposeCheck if app is aliveCheck if app is ready
Action on FailureRestart containerStop traffic temporarily
Use CaseCrash or deadlock detectionStartup and traffic control
ImpactRestarts podRemoves pod from service

Summary

Kubernetes liveness and readiness probes are essential tools for managing container health and ensuring high availability in modern cloud-native applications. Liveness probes help automatically recover from failures by restarting unhealthy containers, while readiness probes ensure that only fully prepared applications receive user traffic. By configuring these probes correctly, developers can build reliable, scalable, and production-ready systems with minimal downtime and better user experience.