DEV Community

Arsh Sharma
Arsh Sharma

Posted on • Originally published at arshs.tech on

Pod Health with Liveness Probes

Kubernetes relies on Probes to determine the health of a Pod container. A probe can be understood simply as a periodical diagnostic performed by the kubelet on the container.

In this short article, I’m going to show you a liveness probe in action. Liveness probes are used to check if a Pod is healthy (running as expected) or not. It simply acts as a check for Kubernetes to know when it should restart the container.

Without Liveness Probe

Let us create a simple nginx pod and see what happens when we mess with it without using a Liveness probe. Use the following configuration to create your pod:

apiVersion: v1
kind: Pod
metadata:
  name: my-nginx
spec:
  containers:
    - name: my-nginx-container
      image: nginx:alpine

Enter fullscreen mode Exit fullscreen mode

If we port-forward this container we will notice that it is serving the nginx starter page. Ideally, for this case, we would want Kubernetes to restart this pod if we are not able to reach this page. Let us see if that is the case currently or not.

Launch a shell inside the container using kubectl exec:

kubectl exec my-nginx -it sh

Enter fullscreen mode Exit fullscreen mode

Then navigate to the directory which has the HTML file responsible for the nginx starter page:

cd /usr/share/nginx/html

Enter fullscreen mode Exit fullscreen mode

Do a ls here and you’ll see the following output:

50x.html index.html

Enter fullscreen mode Exit fullscreen mode

Now let us delete this index.html file:

rm -rf index.html

Enter fullscreen mode Exit fullscreen mode

Return to your original shell by typing exit in the current one.

Now do kubectl get pods. You will see that even though our pod is now not functioning as expected (because we deleted the index.html file), Kubernetes has still not restarted the pod. And to be honest that shouldn’t be a surprise. Why would Kubernetes know that we care about that particular index.html file unless we explicitly tell it?

Enter the Liveness Probe

This is where the Liveness Probe comes to the rescue. Update your pod configuration with this:

apiVersion: v1
kind: Pod
metadata:
  name: my-nginx
spec:
  containers:
    - name: my-nginx-container
      image: nginx:alpine
      livenessProbe:
        httpGet:
            path: /index.html
            port: 80

Enter fullscreen mode Exit fullscreen mode

You’ll need to delete your previous pod before creating a new one using this config.

Now repeat the same steps and delete the index.html file again. This time after deleting the index.html file don’t immediately type exit to leave the shell, instead, wait about 10 secs (which is the default time gap for regular checks) and you automatically exit the terminal. This is because the container inside which we had opened our terminal got restarted.

You can verify this by running kubectl get pods and you should see something like:

NAME READY STATUS RESTARTS AGE
my-nginx 1/1 Running 1 4m24s

Enter fullscreen mode Exit fullscreen mode

The 1 in the restarts column confirms that the container in our pod got restarted.

So what exactly did the liveness probe do here? The httpGet action sent a GET request to the container. We told the liveness probe to check for index.html on port 80 and if we don’t get a valid status code for our request then restart the pod.

This is useful for checking containers running an API or something like nginx serving static files.

This was it for this article. Hope you liked it and thanks for reading :)

Top comments (0)