Kubernetes:如何在Readiness Probe命令中传递管道字符

时间:2019-11-19 10:45:20

标签: kubernetes openshift

我在准备就绪探测命令中传递竖线字符|时遇到问题。

我想要一个探测命令:

curl --silent http://localhost:8080/actuator/health | grep --quiet -e '^{\"status\"\:\"UP\".*}$'

这是我定义探针的方式:

# kubectl get pod my_pod -o yaml

readinessProbe:
  exec:
    command:
    - curl
    - --silent
    - http://localhost:8080/actuator/health
    - '|'
    - grep
    - --quiet
    - -e
    - '''^{\"status\"\:\"UP\".*}$'''

准备就绪探针失败,并显示一条消息:

  

就绪探针失败:curl:选项--quiet:不明卷曲:尝试使用“ curl --help”或“ curl --manual”以获取更多信息

在不使用管道字符|的情况下执行命令时,可以重现该错误:

curl --silent http://localhost:8080/actuator/health grep --quiet -e '^{\"status\"\:\"UP\".*}$'

由于某种原因,Kubernetes无法解释管道。

能否请您帮我部署管道?

1 个答案:

答案 0 :(得分:2)

Kubernetes不会运行Shell来自行处理命令;它只是直接运行它们。外壳中最接近的等效项是

curl '--silent' 'http://...' '|' 'grep' ...

也就是说,|不会拆分两个单独的命令,因为那是shell语法;没有外壳,它成为curl的另一个参数,后面的所有单词也一样。

您需要自己提供外壳包装:

readinessProbe:
  exec:
    command:
      - sh
      - -c
      - curl --silent http://localhost:8080/actuator/health | grep --quiet -e '^{\"status\"\:\"UP\".*}$'

您可以使用替代的YAML语法来使其更具可读性。 (>表示将以下几行折叠为一个字符串; -表示将前导空格和尾随空格删除。

readinessProbe:
  exec:
    command:
      - sh
      - -c
      - >-
         curl --silent http://localhost:8080/actuator/health |
         grep --quiet -e '^{\"status\"\:\"UP\".*}$'