通过 C# 代码给出错误的 Liveness Probe

Liveness Probe through C# code giving error

我正在尝试通过 C# 代码(.Net 核心框架)实现 liveliness probe。我只是想 运行 在容器 like this 中执行 curl 命令。下面是代码片段:

IList<string> command = new List<string>();
V1Probe livnessconfig = null;
command.Add("curl http://localhost:5001/checkhealth/");
V1ExecAction execommand = new V1ExecAction(command);
livnessconfig = new V1Probe { Exec = execommand, InitialDelaySeconds = 10, PeriodSeconds = 10, TimeoutSeconds = 5, FailureThreshold = 3 };

但是在 pod 描述中出现这个错误:

Liveness probe errored: rpc error: code = Unknown desc = failed to exec in container: failed to start exec "a80d33b5b2046b8e606ed622da7085013a725": OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: "curl http://localhost:5001/checkhealth/": stat curl http://localhost:5001/checkhealth/

谁能告诉我这是否是向 V1ExecAction 提供命令的正确方法。它在 K8s.Models 中的元数据实现显示 V1ExecAction 在 List:

中执行命令
    #region Assembly KubernetesClient, Version=3.0.0.0, Culture=neutral, PublicKeyToken=a0f90e8c9af122d

using Newtonsoft.Json;
using System.Collections.Generic;

namespace k8s.Models
{
    //
    // Summary:
    //     ExecAction describes a "run in container" action.
    public class V1ExecAction
    {
        //
        // Summary:
        //     Initializes a new instance of the V1ExecAction class.
        public V1ExecAction();
        //
        // Summary:
        //     Initializes a new instance of the V1ExecAction class.
        //
        // Parameters:
        //   command:
        //     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.
        public V1ExecAction(IList<string> command = null);

        //
        // Summary:
        //     Gets or sets 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.
         [JsonProperty(PropertyName = "command")]
      public IList<string> Command { get; set; }
  } }

您混淆了 Exec 表单和 Shell 表单;您可以更改 command 以显式使用 shell,或者修复调用以与 exec 兼容。这就是 stat 响应试图告诉您的内容:没有这样的 file 命名为 curl http...

使用 sh

command.Add("sh");
command.Add("-c");
command.Add("curl http://localhost:5001/checkhealth/");

使用本机执行程序

command.Add("curl");
command.Add("http://localhost:5001/checkhealth/");

虽然这不是您要问的问题,但您将遇到的下一个问题是 curl 仅根据它是否可以连接来改变其退出状态,而不是 200 与非 200 HTTP 状态代码。您将希望 --fail 参数让 curl 根据 HTTP 状态

改变其 return 代码

您可能还想包含 --silent,因为该健康检查命令输出显示在 kubelet 日志和 kubectl describe pod