Create a Health Check for Pods in Kubernetes

This blog post will show you how to create a built-in health check inside a pod that is running on a Kubernetes cluster.

Health Check

By default, any Kubernetes cluster maintains a keep-alive process health check that makes sure the pod is running; however, this health check can be deceiving and result in the application not functioning by the pod does.

LivenessProbe

For that reason, the Kubernetes API comes with a spec that allows us to monitor application inside a running pod.

In the following code, I have a deployment for a Pod that runs a web server, using a liveness check to monitor the health of the webserver.

In code below, I monitor the response from the main web server, and I am checking the status every 5 seconds. If the webserver stops responding and the timeout of 3 seconds is reached, the pod will restart.

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: mypod 
    image: nginx:latest
    livenessProbe:
      httpGet:
        path: /
        port: 80 
      periodSeconds: 5
      timeoutSeconds: 3
    ports:
     - containerPort: 80
       protocol: TCP

If I debug the pod using the log command I wil see the following output.

[13/Feb/2021:09:15:55 +0000] "GET / HTTP/1.1" 200 612 "-" "kube-probe/1.19" "-"
 10. - - [13/Feb/2021:09:16:00 +0000] "GET / HTTP/1.1" 200 612 "-" "kube-probe/1.19" "-"
 10. - - [13/Feb/2021:09:16:05 +0000] "GET / HTTP/1.1" 200 612 "-" "kube-probe/1.19" "-"

Processing…
Success! You're on the list.

Posted

in

by