This content originally appeared on DEV Community and was authored by Jensen Jose
Hello everyone, welcome back to the CK2024 blog series! This is blog number 18 and we’ll dive into health probes in Kubernetes: liveness probes, readiness probes, and startup probes. We’ll explore these concepts in detail with hands-on practice.
What Are Health Probes?
Before we get into the demo, let’s understand what health probes are in Kubernetes. Health probes are mechanisms used to monitor and manage the health of your applications running in Kubernetes. There are three main types of probes:
- Liveness Probes: Ensure your application is running. If the liveness probe fails, Kubernetes will restart the container.
- Readiness Probes: Ensure your application is ready to serve traffic. If the readiness probe fails, Kubernetes will stop sending traffic to the container.
- Startup Probes: Used for slow-starting applications. Ensures that the application has started successfully before running liveness or readiness probes. These probes help in maintaining the health and availability of your applications by automatically recovering from failures.
Liveness Probes
Liveness probes monitor your application and restart the container if it fails. This is useful when your application crashes due to intermittent issues that can be resolved with a restart.
Here's an example of a liveness probe using a command:
apiVersion: v1
kind: Pod
metadata:
name: liveness-exec
spec:
containers:
- name: liveness
image: busybox
args:
- /bin/sh
- -c
- touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5
In this example, the liveness probe runs a command to check if a file exists. If the file doesn’t exist, the probe fails, and Kubernetes restarts the container.
Readiness Probes
Readiness probes ensure your application is ready to serve traffic. If the readiness probe fails, the container is removed from the service’s endpoints, stopping it from receiving traffic until it is ready again.
Here's an example of a readiness probe using an HTTP GET request:
apiVersion: v1
kind: Pod
metadata:
name: readiness-http
spec:
containers:
- name: readiness
image: my-app
readinessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
In this example, the readiness probe sends an HTTP GET request to /healthz. If the response is not successful, the probe fails, and the container stops receiving traffic.
Startup Probes
Startup probes are used for applications that take a long time to start. This probe ensures the application starts successfully before the liveness and readiness probes are activated.
Here’s an example of a startup probe:
apiVersion: v1
kind: Pod
metadata:
name: startup-probe
spec:
containers:
- name: startup
image: my-app
startupProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 10
periodSeconds: 10
In this example, the startup probe sends an HTTP GET request to /healthz. It will wait for the initial delay before performing the first check and continue at specified intervals. If the probe fails, the container will be restarted.
Hands-On Practice
To reinforce your understanding, we’ll do a demo and configure these probes in a Kubernetes cluster. Follow along in the video to see the implementation in action.
Liveness Probe Demo
We’ll create a pod with a liveness probe that checks if a file exists:
- Create a YAML file for the pod configuration:
apiVersion: v1
kind: Pod
metadata:
name: liveness-exec
spec:
containers:
- name: liveness
image: busybox
args:
- /bin/sh
- -c
- touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5
- Apply the configuration:
kubectl apply -f liveness-exec.yaml
- Observe the pod behavior:
kubectl get pods -w
You’ll see that the pod restarts when the file is removed, indicating the liveness probe failure.
Readiness Probe Demo
We’ll create a pod with a readiness probe that checks an HTTP endpoint:
- Create a YAML file for the pod configuration:
apiVersion: v1
kind: Pod
metadata:
name: readiness-http
spec:
containers:
- name: readiness
image: my-app
readinessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
- Apply the configuration:
kubectl apply -f readiness-http.yaml
- Observe the pod behavior:
kubectl get pods -w
The pod will only start receiving traffic once the readiness probe passes.
Startup Probe Demo
We’ll create a pod with a startup probe:
- Create a YAML file for the pod configuration:
apiVersion: v1
kind: Pod
metadata:
name: startup-probe
spec:
containers:
- name: startup
image: my-app
startupProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 10
periodSeconds: 10
- Apply the configuration:
kubectl apply -f startup-probe.yaml
- Observe the pod behavior:
kubectl get pods -w
The startup probe ensures the application is fully started before the liveness and readiness probes are activated.
Conclusion
In this blog, we’ve explored health probes in Kubernetes, including liveness, readiness, and startup probes. These probes help maintain the health and availability of your applications by automatically recovering from failures.
For further reference, check out the detailed YouTube video here:
Happy learning!
This content originally appeared on DEV Community and was authored by Jensen Jose
Jensen Jose | Sciencx (2024-07-16T15:52:35+00:00) Understanding Health Probes in Kubernetes. Retrieved from https://www.scien.cx/2024/07/16/understanding-health-probes-in-kubernetes/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.