【问题标题】:How can I check the time taken by k8s pod to start?如何查看 k8s pod 启动所用的时间?
【发布时间】:2021-01-10 21:36:23
【问题描述】:

我想知道在 Kubernetes 中启动我的 pod 所需的时间。我的 pod 有 3 Init Containers9 actual Containers。 到目前为止,这是我尝试过的:

  • 方法 1: 启动 pod 并监控,直到有 9/9 个容器在运行。 kubectl describe pod <pod_name> 中的 AGE 将给出启动时间的概念。
  • 方法 2: 使用 kubectl eventskubectl describe 命令引用 pod 的事件。问题是并非所有容器都存在事件。此外,没有明确的方法可以了解 pod 处于 READY 状态所花费的时间。

问题: Kubernetes 是否提供任何方法来确定整个 pod 启动时间?

【问题讨论】:

    标签: kubernetes kubernetes-pod


    【解决方案1】:

    随着事情的开始,您的 pod 将通过一组 PodConditions

    你可以使用:

    kubectl get po <pod_name> -o yaml
    

    通过这些不同的条件观察进展:

    ...
      conditions:
      - lastProbeTime: null
        lastTransitionTime: "2020-09-09T08:47:11Z"
        status: "True"
        type: Initialized
      - lastProbeTime: null
        lastTransitionTime: "2020-09-09T08:47:12Z"
        status: "True"
        type: Ready
      - lastProbeTime: null
        lastTransitionTime: "2020-09-09T08:47:12Z"
        status: "True"
        type: ContainersReady
      - lastProbeTime: null
        lastTransitionTime: "2020-09-09T08:47:11Z"
        status: "True"
        type: PodScheduled
    ...
    

    如果你想在 shell 中自动解析所有这些,我推荐yq 用于实际的 YAML 解析。

    【讨论】:

      【解决方案2】:

      虽然 Kubernetes 中没有确切的机制,但您可能需要检查 Kube-state-metrics

      kube-state-metrics 是一个监听 Kubernetes 的简单服务 API 服务器并生成有关对象状态的指标。 (看 下面的 Metrics 部分中的示例。)它不专注于 单个 Kubernetes 组件的健康状况,而是在 内部各种对象的健康状况,例如部署、节点和 豆荚。

      使用它您可以导出对象创建时间。在这种情况下,度量名称将是 kube_&lt;OBJECT&gt;_createdarticle 第 6 节中关于指标的这个article 详细解释了 kube 状态指标和对象创建时间的使用。

      请注意,如果您要测量您的pod start time,则假设运行该 pod 所需的所有图像都已预先拉入机器上。否则,您的测量结果将不正确,因为它包含影响 Kubernetes 性能的变量,例如网络或映像大小。

      【讨论】:

        【解决方案3】:

        如果这可以帮助任何人,这里是一个使用 kubectl、yq 和 bash 的示例

        $ kubectl -n ${NAMESPACE} get pods -o yaml | yq e '.items[].status.conditions[] | select('.type' == "PodScheduled" or '.type' == "Ready") | '.lastTransitionTime'' - | xargs -n 2 bash -c 'echo $(( $(date -d "$0" "+%s") - $(date -d "$1" "+%s") ))'
        67
        70
        70
        72
        ...
        

        【讨论】:

          猜你喜欢
          • 2019-01-27
          • 2018-12-30
          • 1970-01-01
          • 2019-05-08
          • 2020-01-23
          • 1970-01-01
          • 2021-05-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多