Kubernetes - How to read response body in livenessProbe of a container?

10/14/2021

Below is the current configuration for livenessProbe:

   livenessProbe:
        httpGet:
          path: /heartbeat
          port: 8000
        initialDelaySeconds: 2
        timeoutSeconds: 2
        periodSeconds: 8
        failureThreshold: 2

But response body for URL .well-known/heartbeat shows status: "DOWN" and the http return status as 200

So, Kubelet does not restart the container, due to http response status 200


How to ensure Kubelet reads the response body instead of http return status? using livenessProbe configuration

-- overexchange
kubelet
kubernetes
livenessprobe

2 Answers

10/15/2021

You can interpret the body in your probe using shell command, example:

livenessProbe:
  exec:
    command:
    - sh
    - -c
    - curl -s localhost | grep 'status: "UP"'

grep return non-zero if status: "DOWN" which will direct readinessProbe to fail. You can of course adjust the script according to your actual response body.

-- gohm'c
Source: StackOverflow

10/14/2021

How to ensure Kubelet reads the response body instead of http return status? using livenessProbe configuration

This is not according to the "contract" provided by Kubernetes. You probably need to implement a custom endpoint that follows the contract for HTTP liveness probes as below.

From Define a HTTP liveness probe

If the handler returns a failure code, the kubelet kills the container and restarts it.

Any code greater than or equal to 200 and less than 400 indicates success. Any other code indicates failure.

-- Jonas
Source: StackOverflow