kubernetes 如何查看k8s pod启动所需的时间?

wyyhbhjk  于 2022-11-21  发布在  Kubernetes
关注(0)|答案(4)|浏览(431)

我想知道在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启动时间?

dsekswqp

dsekswqp1#

随着 设置 的 开始 , 您 的 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 解析 。

1aaf6o9v

1aaf6o9v2#

虽然 在 Kubernetes 中 没有 确切 的 机制 , 但 您 可能 需要 检查 Kube-state - metrics :
kube-state - metrics 是 一 个 简单 的 服务 , 用于 监听 Kubernetes API 服务 器 并 生成 有关 对象 状态 的 指标 。 ( 请 参阅 下面 " 指标 " 部分 中 的 示例 。 ) 它 并 不 关注 单个 Kubernetes 组件 的 运行 状况 , 而是 关注 内部 各种 对象 的 运行 状况 , 如 部署 、 节点 和 pod 。
使用 它 可以 导出 对象 创建 时间 。 在 这种 情况 下 , 度量 名称 将 是 kube_<OBJECT>_created 。 第 6 节 中 的 article 关于 度量 解释 了 更多 关于 kube 状态 度量 和 对象 创建 时间 的 使用 。
请 注意 , 如果 您 要 测量 pod start time , 您 必须 假设 运行 该 单元 所 需 的 所有 映像 都 已 预 拉 到 计算机 上 。 否则 , 您 的 测量 结果 将 不 正确 , 因为 它 将 包含 影响 Kubernetes 性能 的 变量 , 如 网络 或 映像 大小 。

ssm49v7z

ssm49v7z3#

如果这对任何人都有帮助,这里有一个使用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
...
ukxgm1gy

ukxgm1gy4#

基于 Jedi 的 答案 , 我 编写 了 下面 的 工具 , 使用 https://stedolan.github.io/jq/ 来 实现 这 一 点 。 我 这样 运行 它 :x1月 1 日

#!/usr/bin/env bash

pod="$1"

fail() {
    echo "$@" 1>&2
    exit 1
}

# https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-conditions
get_condition_time() {
    condition="$1"

    iso_time=$(kubectl get pod "$pod" -o json | jq ".status.conditions[] | select(.type == \"$condition\" and .status == \"True\") | .lastTransitionTime" | tr -d '"\n')

    test -n "$iso_time" || fail "Pod $pod is not in $condition yet"

    date -d $iso_time +%s
}

initialized_time=$(get_condition_time PodScheduled)
ready_time=$(get_condition_time Ready)
duration_seconds=$(( $ready_time - $initialized_time ))

echo "It took $duration_seconds seconds for $pod to boot up"

中 的 每 一 个

相关问题