if 语句中的 kubernetes livenessprobe exec 命令环境变量不起作用

dav*_*amp 5 kubernetes livenessprobe

我在获取 kubernetes livenessProbe exec 命令来处理环境变量时遇到困难。\n我的目标是让 liveness 探针监视 pod 上的内存使用情况并执行 httpGet 运行状况检查。

\n

“如果容器内存使用量超过资源限制的 90% 或 http 响应代码/health失败,则探测应该失败。”

\n

活性探针配置如下:

\n
\nlivenessProbe:\n  exec:\n    command:\n    - sh\n    - -c\n    - |-\n      "used=$(awk '{ print int($1/1.049e+6) }' /sys/fs/cgroup/memory/memory.usage_in_bytes);\n      thresh=$(awk '{ print int( $1 / 1.049e+6 * 0.9 ) }' /sys/fs/cgroup/memory/memory.limit_in_bytes);\n      health=$(curl -s -o /dev/null --write-out "%{http_code}" http://localhost:8080/health);\n      if [[ ${used} -gt ${thresh} || ${health} -ne 200 ]]; then exit 1; fi"\n  initialDelaySeconds: 240\n  periodSeconds: 60\n  failureThreshold: 3\n  timeoutSeconds: 10\n\n
Run Code Online (Sandbox Code Playgroud)\n

如果我执行到(ubuntu)pod并运行这些命令,它们都可以正常工作并完成工作。

\n

但当部署为 livenessProbe 时,pod 不断失败并出现以下警告:

\n
Events:                                                                                                                                                                                                               \xe2\x94\x82\n\xe2\x94\x82   Type     Reason     Age                  From     Message                                                                                                                                                           \xe2\x94\x82\n\xe2\x94\x82   ----     ------     ----                 ----     -------                                                                                                                                                           \xe2\x94\x82\n\xe2\x94\x82   Warning  Unhealthy  14m (x60 over 159m)  kubelet  (combined from similar events): Liveness probe failed: sh: 4: used=1608;                                                                                          \xe2\x94\x82\n\xe2\x94\x82 thresh=2249;                                                                                                                                                                                                          \xe2\x94\x82\n\xe2\x94\x82 health=200;                                                                                                                                                                                                           \xe2\x94\x82\n\xe2\x94\x82 if [[  -gt  ||  -ne 200 ]]; then exit 1; fi: not found\n
Run Code Online (Sandbox Code Playgroud)\n

看起来探测内存和卷曲运行状况检查端点的初始命令都已工作并填充了环境变量,但这些变量替换随后并未填充在 if 语句中,因此探测永远不会通过。

\n

知道为什么吗?或者如何配置才能正常工作?\n我知道这有点复杂。提前致谢。

\n

And*_*ess 7

看起来 shell 将您的整个命令视为要执行的文件名。

我会删除外部引号

livenessProbe:
  exec:
    command:
    - sh
    - -c
    - |-
      used=$(awk '{ print int($1/1.049e+6) }' /sys/fs/cgroup/memory/memory.usage_in_bytes);
      thresh=$(awk '{ print int( $1 / 1.049e+6 * 0.9 ) }' /sys/fs/cgroup/memory/memory.limit_in_bytes);
      health=$(curl -s -o /dev/null --write-out "%{http_code}" http://localhost:8080/health);
      if [[ ${used} -gt ${thresh} || ${health} -ne 200 ]]; then exit 1; fi
  initialDelaySeconds: 240
  periodSeconds: 60
  failureThreshold: 3
  timeoutSeconds: 10
Run Code Online (Sandbox Code Playgroud)

您已经告诉 YAML 解析器它是一个多行字符串


dav*_*amp 4

事实证明,@Andrew McGuinness 和 @OreOP 的答案对于我最终正常工作的解决方案至关重要:

  livenessProbe:
    exec:
      command:
      - /bin/bash
      - -c
      - |-
        used=$(awk '{ print int($1/1.049e+6) }' /sys/fs/cgroup/memory/memory.usage_in_bytes);
        thresh=$(awk '{ print int( $1 / 1.049e+6 * 0.9 ) }' /sys/fs/cgroup/memory/memory.limit_in_bytes);
        health=$(curl -s -o /dev/null --write-out "%{http_code}" http://localhost:8080/health);
        if [[ ${used} -gt ${thresh} || ${health} -ne 200 ]]; then exit 1; fi
    initialDelaySeconds: 240
    periodSeconds: 60
    failureThreshold: 3
    timeoutSeconds: 10
Run Code Online (Sandbox Code Playgroud)

我非常需要 Andrews 关于删除引号的建议,因为我已经指示 yaml 解析器这是一个多行字符串。我想这实际上就是我想问的。但@OreOP 对于我对 bash 和 sh 之间的混淆以及哪一个会接受双括号[[ conditional ]]声明的理解是绝对正确的。

顺便说一句,我完全同意这两种观点,即这并不是解决当前更深层次问题的最终正确解决方案,但出于各种其他原因,我的团队要求将此补丁作为临时措施。我的脚本中memory.limit_in_bytes的实际上引用了我的 k8s 部署 yaml 中设置的资源限制。