0
0
KubernetesHow-ToBeginner · 3 min read

How to Configure Readiness Probe in Kubernetes for Healthy Pods

In Kubernetes, configure a readinessProbe in your pod's container spec to check if the application is ready to accept traffic. This probe can use HTTP, TCP, or command checks and controls when the pod is marked ready by the kubelet.
📐

Syntax

The readinessProbe is defined inside the container spec in a pod manifest. It includes the probe type (httpGet, tcpSocket, or exec) and parameters like initialDelaySeconds, periodSeconds, and timeoutSeconds.

Key parts explained:

  • httpGet: Checks an HTTP endpoint.
  • tcpSocket: Checks if a TCP port is open.
  • exec: Runs a command inside the container.
  • initialDelaySeconds: Wait time before starting probes.
  • periodSeconds: How often to run the probe.
  • timeoutSeconds: Time to wait for probe response.
yaml
readinessProbe:
  httpGet:
    path: /healthz
    port: 8080
  initialDelaySeconds: 5
  periodSeconds: 10
  timeoutSeconds: 2
💻

Example

This example shows a pod with a readiness probe that checks the /ready HTTP endpoint on port 80. The probe starts 10 seconds after the container starts and runs every 5 seconds.

yaml
apiVersion: v1
kind: Pod
metadata:
  name: readiness-example
spec:
  containers:
  - name: webserver
    image: nginx:1.23
    readinessProbe:
      httpGet:
        path: /ready
        port: 80
      initialDelaySeconds: 10
      periodSeconds: 5
      timeoutSeconds: 3
Output
Pod 'readiness-example' will be marked Ready only after the /ready endpoint returns success.
⚠️

Common Pitfalls

Common mistakes when configuring readiness probes include:

  • Setting initialDelaySeconds too low, causing probes to fail before the app is ready.
  • Using the wrong port or path that the app does not serve.
  • Not matching the probe type to the app's readiness check method.
  • Confusing readiness probes with liveness probes; readiness controls traffic routing, liveness controls restarts.
yaml
readinessProbe:
  httpGet:
    path: /wrongpath
    port: 8080
  initialDelaySeconds: 0
  periodSeconds: 5

# Corrected version:
readinessProbe:
  httpGet:
    path: /healthz
    port: 8080
  initialDelaySeconds: 5
  periodSeconds: 10
📊

Quick Reference

FieldDescriptionExample
httpGetHTTP GET request to check readinesspath: /healthz, port: 8080
tcpSocketTCP check on a portport: 3306
execRun command inside containercommand: ["cat", "/tmp/ready"]
initialDelaySecondsWait before first probe10
periodSecondsTime between probes5
timeoutSecondsProbe timeout duration3

Key Takeaways

Configure readinessProbe in the container spec to control pod readiness.
Use httpGet, tcpSocket, or exec probes depending on your app's readiness check.
Set initialDelaySeconds to avoid premature probe failures.
Readiness probes control traffic routing; they do not restart containers.
Test your probe endpoints or commands to ensure they reflect true readiness.