0
0
KubernetesHow-ToBeginner · 3 min read

How to Use HTTP Probe in Kubernetes for Container Health Checks

In Kubernetes, use the httpGet field inside livenessProbe or readinessProbe to perform HTTP probes. This sends HTTP requests to your container to check if it is alive or ready to serve traffic.
📐

Syntax

The httpGet probe is defined inside livenessProbe or readinessProbe in a Pod spec. It requires specifying the path to request, the port number, and optionally the scheme (HTTP or HTTPS). You can also configure initialDelaySeconds, periodSeconds, and other timing settings.

yaml
livenessProbe:
  httpGet:
    path: /healthz
    port: 8080
    scheme: HTTP
  initialDelaySeconds: 10
  periodSeconds: 5
💻

Example

This example shows a Pod spec with an HTTP liveness probe that checks the /healthz endpoint on port 8080 every 5 seconds, starting 10 seconds after the container starts. If the probe fails, Kubernetes restarts the container.

yaml
apiVersion: v1
kind: Pod
metadata:
  name: http-probe-example
spec:
  containers:
  - name: webserver
    image: nginx
    livenessProbe:
      httpGet:
        path: /healthz
        port: 8080
        scheme: HTTP
      initialDelaySeconds: 10
      periodSeconds: 5
Output
Pod 'http-probe-example' runs with liveness HTTP probe checking /healthz on port 8080 every 5 seconds after 10 seconds delay.
⚠️

Common Pitfalls

  • Not setting initialDelaySeconds can cause probes to fail before the app is ready.
  • Using the wrong path or port causes probe failures and container restarts.
  • For HTTPS endpoints, forgetting to set scheme: HTTPS leads to probe errors.
  • Not exposing the probe port in the container can cause connection refused errors.
yaml
livenessProbe:
  httpGet:
    path: /wrongpath
    port: 80
    scheme: HTTP
  initialDelaySeconds: 5
  periodSeconds: 5

# Corrected version:
livenessProbe:
  httpGet:
    path: /healthz
    port: 8080
    scheme: HTTP
  initialDelaySeconds: 10
  periodSeconds: 5
📊

Quick Reference

FieldDescriptionExample
httpGet.pathURL path to probe inside the container/healthz
httpGet.portPort number or name to connect to8080
httpGet.schemeHTTP or HTTPS protocolHTTP
initialDelaySecondsWait time before first probe10
periodSecondsTime between probes5

Key Takeaways

Use httpGet inside livenessProbe or readinessProbe to check container health via HTTP requests.
Always set initialDelaySeconds to avoid premature probe failures during startup.
Ensure the probe path and port match your container's actual HTTP endpoint.
Specify scheme: HTTPS if your probe uses HTTPS.
Probe failures cause Kubernetes to restart or stop routing traffic to the container.