Kubernetes
CKAD
Probes

🔍 What Are Probes?

Probes help Kubernetes monitor the health and availability of your containers.

Probe TypePurpose
livenessProbeChecks if the container is alive (running). Restart if it fails.
readinessProbeChecks if the container is ready to serve traffic. Remove from service if it fails.
startupProbeUsed when app takes time to start up. Replaces liveness check during startup phase.

⚙️ How They Work

  • Kubernetes runs the probe periodically.

  • If it fails:

    • Liveness: Container is restarted.
    • Readiness: Removed from Service endpoints.
    • Startup: If it fails during startup, container is killed and restarted.
  • You can probe via:

    • httpGet
    • tcpSocket
    • exec

📦 Liveness, Readiness, Startup Probe - All in One Example

apiVersion: v1
kind: Pod
metadata:
  name: probe-example
spec:
  containers:
  - name: my-app
    image: myapp:1.0
    ports:
    - containerPort: 8080
    livenessProbe:
      httpGet:
        path: /healthz
        port: 8080
      initialDelaySeconds: 10
      periodSeconds: 5
    readinessProbe:
      httpGet:
        path: /ready
        port: 8080
      initialDelaySeconds: 5
      periodSeconds: 3
    startupProbe:
      httpGet:
        path: /startup
        port: 8080
      failureThreshold: 30
      periodSeconds: 5

📘 Explanation

ProbeWhat It ChecksOutcome
LivenessIs the app still running? (e.g., /healthz)Restarts container if it stops responding
ReadinessIs it ready to serve traffic? (e.g., /ready)Traffic is sent only if this passes
StartupIs the app still starting? (e.g., /startup)Waits longer before liveness checks begin

🛠️ Common Real-Life Use Cases

🧠 1. Web App with /health, /ready, /startup Endpoints

livenessProbe:
  httpGet:
    path: /health
    port: 8080
readinessProbe:
  httpGet:
    path: /ready
    port: 8080
startupProbe:
  httpGet:
    path: /startup
    port: 8080
  • Useful for apps like Django, Spring Boot, Laravel, etc.
  • /startup might return 503 until database is connected.
  • /ready only after cache is warmed.

🧠 2. App That Listens on a Port (TCP Probe)

livenessProbe:
  tcpSocket:
    port: 3306
  initialDelaySeconds: 10
  periodSeconds: 10

✅ Ideal for:

  • Databases (MySQL, PostgreSQL)
  • Services with raw socket listening

🧠 3. Use exec to Check Disk or Service

readinessProbe:
  exec:
    command:
    - cat
    - /tmp/ready

✅ Example: Readiness is indicated by the presence of a file /tmp/ready.

Can be used when:

  • No HTTP endpoint is available
  • Internal logic can mark file-based readiness

🚦 Visual Flow: Probe Behavior

                      +-------------+
                      |  Container  |
                      +-------------+
                            |
              +-------------+--------------+
              |                            |
        +-----v-----+              +-------v-------+
        | Liveness  |              |  Readiness    |
        +-----+-----+              +-------+-------+
              |                            |
      Restart if fails         Remove from Service if fails

                     +------------------+
                     |   Startup Probe  |
                     +--------+---------+
                              |
              (If defined, delays liveness check)

⏱️ Common Configuration Parameters

ParameterMeaning
initialDelaySecondsTime to wait before first probe (useful for cold start apps)
periodSecondsHow often to run the probe
timeoutSecondsHow long to wait for the probe response
failureThresholdNumber of failures before marking container as failed
successThresholdNumber of successes to mark container as healthy (readiness only)

✅ Best Practices

PracticeWhy
Use startupProbe for slow-starting appsPrevent premature liveness kill
Use HTTP probes with framework health endpointsIntegrates well with /health and /ready
Keep timeoutSeconds low for quick responsePrevent resource hang
Don’t use livenessProbe for debuggingIt restarts your container
Use readinessProbe to control load balancer trafficEnsures app is ready

💬 Summary Table

Probe TypeUse When…Action Taken
LivenessApp might hang or crashKills & restarts the pod
ReadinessApp needs time to warm up or become healthyTemporarily removes from service
StartupApp has slow init (e.g., database, cache)Waits longer before applying liveness

🧙 AI Wizard - Instant Page Insights

Click the button below to analyze this page.
Get an AI-generated summary and key insights in seconds.
Powered by Perplexity AI!