Implementing Health Checks
1. Configuring Liveness Probes
| Field | Description | Values | Notes |
|---|---|---|---|
| livenessProbe | Defines how Kubernetes determines if a container is alive | object |
Optional field in Pod spec |
| livenessProbe.httpGet | HTTP GET request to check container health | object |
Used for web services or APIs |
| livenessProbe.tcpSocket | TCP socket connection to check container health | object |
Useful for non-HTTP services like databases |
| livenessProbe.exec | Executes a command inside the container to check health | object |
Useful for checking internal state or running diagnostics |
| livenessProbe.initialDelaySeconds | Number of seconds to wait before starting the probe | integer |
Default: 0 |
| livenessProbe.periodSeconds | How often to perform the probe (in seconds) | integer |
Default: 10 |
| livenessProbe.timeoutSeconds | Number of seconds to wait for the probe to complete | integer |
Default: 1 |
| livenessProbe.failureThreshold | Number of consecutive failures to consider the container dead | integer |
Default: 3 |
| livenessProbe.successThreshold | Minimum consecutive successes for the probe to be considered successful | integer |
Default: 1 |
Example: HTTP GET Liveness Probe
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: web-container
image: nginx
livenessProbe:
httpGet:
path: /healthz
port: 80
initialDelaySeconds: 30
periodSeconds: 10
Example: TCP Socket Liveness Probe
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: db-container
image: postgres
livenessProbe:
tcpSocket:
port: 5432
initialDelaySeconds: 60
periodSeconds: 15
Example: Exec Liveness Probe
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: app-container
image: myapp
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 10
periodSeconds: 5
2. Configuring Readiness Probes
| Field | Description | Example |
|---|---|---|
| readinessProbe | Defines how to check if a pod is ready to serve traffic | |
Example: HTTP Readiness Probe
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "example-pod"
},
"spec": {
"containers": [
{
"name": "example-container",
"image": "nginx:latest",
"readinessProbe": {
"httpGet": {
"path": "/healthz",
"port": 80
},
"initialDelaySeconds": 5,
"periodSeconds": 10,
"timeoutSeconds": 3,
"failureThreshold": 3
}
}
]
}
}
Note: The
readinessProbe ensures that traffic is not sent to a pod until it's ready. It's particularly useful during deployment or restart scenarios.
3. Configuring Startup Probes
| Field | Description | Values |
|---|---|---|
| startupProbe | Defines the startup probe configuration | object |
| startupProbe.httpGet | HTTP GET request to check the application startup | object |
| startupProbe.httpGet.path | Path to access for the HTTP GET request | /healthz |
| startupProbe.httpGet.port | Port to access for the HTTP GET request | 8080 |
| startupProbe.httpGet.scheme | Scheme to use for the HTTP GET request (HTTP or HTTPS) | HTTP |
| startupProbe.initialDelaySeconds | Number of seconds to wait before starting the probe | 30 |
| startupProbe.periodSeconds | How often to execute the probe (in seconds) | 10 |
| startupProbe.timeoutSeconds | Number of seconds to wait for a response before timing out | 5 |
| startupProbe.failureThreshold | Number of consecutive failures before considering the probe failed | 3 |
| startupProbe.successThreshold | Number of consecutive successes to consider the probe successful | 1 |
Example: Startup Probe Configuration
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: example-container
image: nginx
startupProbe:
httpGet:
path: /healthz
port: 8080
scheme: HTTP
initialDelaySeconds: 30
periodSeconds: 10
timeoutSeconds: 5
failureThreshold: 3
successThreshold: 1
4. Using HTTP Probes
| Field | Description | Example |
|---|---|---|
| httpGet.path | Path to access on the host for health check | /healthz |
| httpGet.port | Port number to access on the pod | 8080 |
| httpGet.host | Host header to send with the request | localhost |
| httpGet.scheme | HTTP or HTTPS scheme to use | HTTP |
| httpGet.httpHeaders | List of custom headers to include in the request | [{name: "Authorization", value: "Bearer token"}] |
Example: HTTP Probe Configuration
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: example-container
image: nginx
livenessProbe:
httpGet:
path: /healthz
port: 8080
scheme: HTTP
initialDelaySeconds: 30
periodSeconds: 10
Note: The
httpGet probe is ideal for applications that expose a health endpoint over HTTP. It's commonly used in microservices to indicate whether the service is ready to handle traffic.
5. Using TCP Probes
| Field | Description | Values |
|---|---|---|
| tcpSocket.port | Port number to connect to | 8080 |
| tcpSocket.host | Host to connect to (optional) | localhost |
Example: TCP Probe Configuration
livenessProbe:
tcpSocket:
port: 8080
host: localhost
initialDelaySeconds: 10
periodSeconds: 5
Note: TCP probes are useful for services that do not support HTTP endpoints but can accept TCP connections.
6. Using Exec Probes
| Property | Description | Values |
|---|---|---|
| exec | Command to execute inside the container | command: ["/bin/sh", "-c", "curl -f http://localhost:8080/health || exit 1"] |
| initialDelaySeconds | Number of seconds after container starts before probe is initiated | 5 |
| periodSeconds | How often (in seconds) to execute the probe | 10 |
| timeoutSeconds | Number of seconds after which the probe times out | 1 |
| successThreshold | Minimum consecutive successes for the probe to be considered successful | 1 |
| failureThreshold | Minimum consecutive failures for the probe to be considered failed | 3 |
Example: Exec Probe Configuration
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: example-container
image: nginx
livenessProbe:
exec:
command:
- /bin/sh
- -c
- curl -f http://localhost:8080/health || exit 1
initialDelaySeconds: 5
periodSeconds: 10
timeoutSeconds: 1
failureThreshold: 3
Note: The command runs in the container's environment. Ensure that the necessary binaries (e.g.,
curl) are available in the container image.7. Using gRPC Probes
| Field | Description | Values |
|---|---|---|
| grpc | gRPC probe configuration | { port: 50051, service: 'myapp.MyService' } |
| port | Port number to connect to | 50051 |
| service | gRPC service name to check | myapp.MyService |
Example: gRPC Probe Configuration
apiVersion: v1
kind: Pod
metadata:
name: grpc-pod
spec:
containers:
- name: grpc-container
image: myapp:latest
ports:
- containerPort: 50051
livenessProbe:
grpc:
port: 50051
service: myapp.MyService
initialDelaySeconds: 10
periodSeconds: 5
Note: gRPC probes require the container to support gRPC health checking. The
service field must match the gRPC service name defined in the server's health check implementation.8. Setting Initial Delay
| Field | Description | Default Value | Notes |
|---|---|---|---|
| initialDelaySeconds | Number of seconds after the container starts before liveness or readiness probes are executed. | 0 | Must be >= 0. Used to prevent premature probe execution before the application is ready. |
Example: Configuring initialDelaySeconds in a readiness probe
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: app-container
image: nginx
readinessProbe:
httpGet:
path: /healthz
port: 80
initialDelaySeconds: 10
periodSeconds: 5
9. Configuring Period Seconds
| Field | Description | Default Value | Notes |
|---|---|---|---|
| periodSeconds | How often (in seconds) to perform the probe | 10 | Must be >= initialDelaySeconds + timeoutSeconds |
Example: Configuring periodSeconds in a Probe
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
spec:
containers:
- name: nginx
image: nginx
livenessProbe:
httpGet:
path: /healthz
port: 80
periodSeconds: 15
timeoutSeconds: 5
10. Setting Failure Threshold
| Field | Description | Default Value | Notes |
|---|---|---|---|
| failureThreshold | Number of consecutive failures after which the probe is considered failed | 3 |
Minimum value is 1. If set to 0, the probe will never fail |
Example: Configuring failureThreshold in a readiness probe
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
spec:
containers:
- name: nginx
image: nginx:latest
readinessProbe:
httpGet:
path: /healthz
port: 80
failureThreshold: 5
periodSeconds: 10
11. Setting Success Threshold
| Field | Description | Default Value | Notes |
|---|---|---|---|
| successThreshold | Minimum consecutive successes for the probe to be considered successful after having failed | 1 |
Must be 1 for liveness and readiness probes; for startup probes, it can be greater than 1 |
Example: Configuring successThreshold in a readiness probe
readinessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
successThreshold: 2
failureThreshold: 3
Note: The
successThreshold is particularly useful for applications that may take some time to become fully ready after starting up, such as those initializing databases or loading large configuration files.12. Configuring Timeout Seconds
| Field | Description | Default Value | Notes |
|---|---|---|---|
| timeoutSeconds | Number of seconds after which the probe times out | 1 | Must be > 0 and <= periodSeconds |