Health Probing in K8s

Introduction

Probing in general means inspecting or monitoring. Health probing in Kubernetes is nothing but monitoring the health of Kubernetes pods and containers.

Health probing can be done in three ways.

  1. Executing commands inside containers periodically

  2. Via HTTP Get request

  3. Or by checking TCP Socket

and health probes are of 3 types,

  1. Startup Probe

  2. Readiness Probe

  3. Liveliness probe

Kubernetes continuously checks application health using health probes, which are configured at the container level inside a Pod. These probes help Kubernetes decide:

  1. When to restart a container

  2. When to send or stop traffic

  3. Whether an application has started successfully

Ways to Perform Health Probing in Kubernetes

Kubernetes supports three mechanisms to perform health checks:

1. Executing Commands Inside the Container

Kubernetes periodically runs a command inside the container. If the command exits with code 0 β†’ probe succeeds else Any non-zero exit code means probe fails

livenessProbe:
  exec:
    command:
      - sh
      - -c
      - cat /tmp/healthy
  initialDelaySeconds: 20
  periodSeconds: 10

Here the probe checks whether the file /tmp/healthy exists. If not, the probe fails.

2. HTTP GET Request

Kubernetes sends an HTTP request to a specified endpoint.

Status codes 200–399 means success In case of Web applications exposing /health, /ready, or /status endpoints.

readinessProbe:
  httpGet:
    path: /health
    port: 8080
  initialDelaySeconds: 10
  periodSeconds: 5

The container is marked Ready only when /health responds successfully.

3. TCP Socket Check

Kubernetes checks whether a TCP port is open.

Successful TCP connection means probe succeeds and Connection failure refers to probe fails Using this in Databases or services that don’t expose HTTP endpoints.

readinessProbe:
  tcpSocket:
    port: 3306
  initialDelaySeconds: 15
  periodSeconds: 10

Kubernetes verifies whether the application is listening on port 3306.

Types of Health Probes in Kubernetes

Kubernetes provides three types of health probes, each serving a different purpose.

1. Startup Probe

A Startup Probe checks whether the application has started successfully. Some applications take a long time to start. Without a startup probe, the liveness probe might kill the container too early. It Runs only during startup If it fails β†’ container is restarted Once it succeeds β†’ liveness and readiness probes begin

startupProbe:
  httpGet:
    path: /startup
    port: 8080
  failureThreshold: 30
  periodSeconds: 10

Kubernetes allows up to 5 minutes (30 Γ— 10s) for the app to start.

2. Readiness Probe

A Readiness Probe checks whether the container is ready to accept traffic. If it fails then Pod is marked NotReady and Traffic is stopped, but Container is NOT restarted. Using in Application warm-up, Dependency failures (DB, cache, APIs) or Controlling rolling deployments.

readinessProbe:
  httpGet:
    path: /ready
    port: 8080
  periodSeconds: 5

Traffic is routed to the Pod only when /ready responds successfully.

3. Liveness Probe

A Liveness Probe checks whether the application is still running correctly. If it fails then Kubernetes restarts the container

livenessProbe:
  httpGet:
    path: /live
    port: 8080
  initialDelaySeconds: 30
  periodSeconds: 10

If /live fails repeatedly, Kubernetes restarts the container.