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: 5Example
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: 5Output
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
initialDelaySecondscan cause probes to fail before the app is ready. - Using the wrong
pathorportcauses probe failures and container restarts. - For HTTPS endpoints, forgetting to set
scheme: HTTPSleads 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: 5Quick Reference
| Field | Description | Example |
|---|---|---|
| httpGet.path | URL path to probe inside the container | /healthz |
| httpGet.port | Port number or name to connect to | 8080 |
| httpGet.scheme | HTTP or HTTPS protocol | HTTP |
| initialDelaySeconds | Wait time before first probe | 10 |
| periodSeconds | Time between probes | 5 |
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.