DEV Community

Cover image for Understanding Health Probes in Kubernetes
Jensen Jose
Jensen Jose

Posted on

Understanding Health Probes in Kubernetes

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:

  1. Liveness Probes: Ensure your application is running. If the liveness probe fails, Kubernetes will restart the container.
  2. Readiness Probes: Ensure your application is ready to serve traffic. If the readiness probe fails, Kubernetes will stop sending traffic to the container.
  3. 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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:

  1. 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
Enter fullscreen mode Exit fullscreen mode
  1. Apply the configuration:
kubectl apply -f liveness-exec.yaml
Enter fullscreen mode Exit fullscreen mode
  1. Observe the pod behavior:
kubectl get pods -w
Enter fullscreen mode Exit fullscreen mode

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:

  1. 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
Enter fullscreen mode Exit fullscreen mode
  1. Apply the configuration:
kubectl apply -f readiness-http.yaml
Enter fullscreen mode Exit fullscreen mode
  1. Observe the pod behavior:
kubectl get pods -w
Enter fullscreen mode Exit fullscreen mode

The pod will only start receiving traffic once the readiness probe passes.

Startup Probe Demo

We’ll create a pod with a startup probe:

  1. 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
Enter fullscreen mode Exit fullscreen mode
  1. Apply the configuration:
kubectl apply -f startup-probe.yaml
Enter fullscreen mode Exit fullscreen mode
  1. Observe the pod behavior:
kubectl get pods -w
Enter fullscreen mode Exit fullscreen mode

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!

Top comments (0)