Kubernetes Command Based Liveness Probes

7/19/2021

I have a file in /tmp/healthy as mentioned in official documentation and YAML is available

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

But in my case file always be here and it will contain message like Success or Failure

It is possible with livenessProbe:command to behave on these messages?

-- Umair
kubernetes
livenessprobe

2 Answers

7/19/2021

yes, you can use the grep command to search for Success string, if it is found it would return 0(passing the probe) , however if Success string is not found by grep it would return non-zero rc(failing the probe)

livenessProbe:
  exec:
    command:
    - grep
    - Success 
    - /tmp/healthy

As per documentation

If the command succeeds, it returns 0, and the kubelet considers the container to be alive and healthy. If the command returns a non-zero value, the kubelet kills the container and restarts it.

This means, exec type depends on the return code of the command. as long as grep is returning success , probe will pass. In shell return code of any command can be manually checked via $? variable. Eg: :

grep Success /tmp/healthy; echo $?
-- P....
Source: StackOverflow

7/19/2021

The point of a liveness probe is usually just to check if a pod is up and reachable. cat is decent command to use for liveness probes because it always returns success as long as the file is there. It is just checking if Kubelet can reach the pod.

If I'm understanding what you mean though, it is possible to have the result of the liveness probe depend on the contents of the file. You can execute any bash and exit with a 1 or 0 (fail or suceed) to control the result you want.

For example, if you want the liveness probe to fail if your file contains any failure message:

    livenessProbe:
      exec:
        command:
        - /bin/bash
        - '-c'
        - grep -zqv "Failure" file.txt 
-- Evan S.
Source: StackOverflow