Sumanto Dinar
Sumanto Dinar

Reputation: 107

Kubernetes custom health check

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.

Upvotes: 3

Views: 1306

Answers (1)

Andrew
Andrew

Reputation: 4662

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.

Upvotes: 6

Related Questions