Kubernetes 自定义健康检查

时间:2021-04-05 14:39:55

标签: kubernetes

您认为有没有办法在 Kubernetes 中创建自定义健康检查?

例如,使用http get,但如果内容包含一些字符串,则视为失败。

1 个答案:

答案 0 :(得分:4)

您可以使用 exec 探针来创建您想要的任何逻辑。如果您的图像包含 curl,并且您的应用程序侦听 8080 端口,您可以插入类似

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

如果没有找到“坏”字符串,grep 将返回 0,因此检查将通过。任何非零值都会导致探测失败。

您可以使用任何您认为必要的脚本,也许您可​​以在容器中放置一个健康检查脚本并在 exec 部分调用它。

相关文档:

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.