Skip to main content

Chapter 3.8 - Health Checks

Learning Objectives

By the end of this chapter, you will be able to:

  • Understand the three types of probes
  • Configure Liveness Probes
  • Configure Readiness Probes
  • Use Startup Probes
  • Choose the right health check strategy

Types of Probes

Kubernetes provides three types of probes to check the health of containers:


1. Liveness Probe

Determines if the container should be restarted.

Example

livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
timeoutSeconds: 5
failureThreshold: 3

Behavior

If the probe fails: The container is restarted.


2. Readiness Probe

Determines if the container is ready to receive traffic.

Example

readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
timeoutSeconds: 3
successThreshold: 1
failureThreshold: 3

Behavior

If the probe fails: The Pod is removed from the Service Endpoints.


3. Startup Probe

For applications that are slow to start.

Example

startupProbe:
httpGet:
path: /startup
port: 8080
failureThreshold: 30 # 30 attempts
periodSeconds: 10 # Every 10 seconds
# Total: up to 5 minutes

Behavior

During startup: Other probes are disabled.


Probe Types

HTTP GET

livenessProbe:
httpGet:
path: /health
port: 8080
httpHeaders:
- name: Custom-Header
value: Awesome

TCP Socket

livenessProbe:
tcpSocket:
port: 8080

Command Exec

livenessProbe:
exec:
command:
- cat
- /tmp/healthy

Complete Configuration

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

Best Practices

When to Use Each Probe

  • Liveness: If the application can become stuck
  • Readiness: If the application needs time to be ready
  • Startup: If the application starts slowly (>30s)
  • initialDelaySeconds: Long enough for startup
  • periodSeconds: 5-10 seconds
  • timeoutSeconds: 1-3 seconds
  • failureThreshold: 3 (default)

Summary

In this chapter, you learned:

Liveness Probe: Restarts the container if it crashes
Readiness Probe: Removes the Pod from the Service if it is not ready
Startup Probe: For applications that are slow to start
Types: HTTP GET, TCP Socket, Command Exec
Configuration: initialDelaySeconds, periodSeconds, failureThreshold


Next Steps

Now that you have mastered Health Checks:

Lab 3.4: Configuring Health Checks
Quiz Module 3: Validate your knowledge


Chapter created: December 2024