【问题标题】:How to negate exit code status to be used in Kubernetes livenessProbe?如何否定要在 Kubernetes livenessProbe 中使用的退出代码状态?
【发布时间】:2021-11-16 11:24:35
【问题描述】:

如何否定要在Kubernetes livenessProbe 中使用的退出代码状态?

我将使用grep 命令,我想在下面做检查。

  • 返回退出值 0,如果 grep 没有命中
  • 如果grep 命中则返回退出值 1

由于正常情况下,如果有命中,grep 将返回 0,我可以像下面这样否定这个吗?

!$(cat filet.txt | grep keyword)

【问题讨论】:

    标签: linux kubernetes grep centos livenessprobe


    【解决方案1】:

    是的,你可以试试

    示例:

        livenessProbe:
      exec:
        command:
        - /bin/bash
        - -c
        - cat filet.txt | grep keyword
      initialDelaySeconds: 10
      periodSeconds: 10
    

    如果有帮助,请查看

    -v, --invert-match
              Invert the sense of matching, to select non-matching lines.
    

    你也可以试试-c

    echo 'Test' | grep -c T
    

    1

    echo 'Test' | grep -c N
    

    0

    shell 脚本

    #!/bin/sh
    if [ $(echo 'Test' | grep -c T) ]; then
      exit 1
    else
      exit 0
    fi
    

    最简单的方法是编写 shell 脚本并根据需要管理活动的退出代码 0 或 1,并基于该活动重新启动 pod。

    livenessProbe:
      exec:
        command:
        - /bin/sh
        - -c
        - /home/test/health.sh
    

    【讨论】:

    • 在您的示例中,如果没有命中,吊舱将重新启动?
    • 我认为,但最简单的方法是使用我分享的最后一个 shell 脚本选项,这样可以很好地管理退出状态代码并根据需要管理代码。
    • 我将尝试使用 shell 脚本方法。我认为这是更好的解决方案。你能支持我的问题吗兄弟?我现在不能赞成你的答案,因为我的代表仍然较少:(
    • 它成功了。真的帮了很多忙。
    • 很高兴听到这个消息...!
    猜你喜欢
    • 2021-05-17
    • 2013-06-15
    • 1970-01-01
    • 2018-01-07
    • 2010-11-09
    • 2014-10-21
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    相关资源
    最近更新 更多