Kubernetes custom health check

4/5/2021

Do you think there is a way to create custom health check in kubernetes?

For example, using http get but if the content contains some string, then it's counted as failure.

-- Sumanto Dinar
kubernetes

1 Answer

4/5/2021

You may use exec probe to create whichever logic you want. If your image contains curl, and your applications listens on 8080 port, you may insert something like

    livenessProbe:
      exec:
        command:
        - bash
        - -c
        - exit "$(curl localhost:8080 | grep -c 'BAD_STRING')"

grep will return 0 if no "bad" strings are found, thus check will pass. Anything non-zero will result in probe failure.

You can use whichever script you find necessary, maybe you can put a healthcheck script inside your container and call it in exec section.

Relevant doc:

kubectl explain deployment.spec.template.spec.containers.readinessProbe.exec
KIND:     Deployment
VERSION:  apps/v1

RESOURCE: exec <Object>

DESCRIPTION:
     One and only one of the following should be specified. Exec specifies the
     action to take.

     ExecAction describes a "run in container" action.

FIELDS:
   command      <[]string>
     Command is the command line to execute inside the container, the working
     directory for the command is root ('/') in the container's filesystem. The
     command is simply exec'd, it is not run inside a shell, so traditional
     shell instructions ('|', etc) won't work. To use a shell, you need to
     explicitly call out to that shell. Exit status of 0 is treated as
     live/healthy and non-zero is unhealthy.
-- Andrew
Source: StackOverflow