如何查看 k8s pod 启动所用的时间?
How can I check the time taken by k8s pod to start?
我想知道在 Kubernetes 中启动我的 pod 所需的时间。我的广告连播有 3 Init Containers
和 9 actual Containers
。
到目前为止,这是我尝试过的:
- 方法一:启动pod并监控直到有9/9个容器运行。
kubectl describe pod <pod_name>
中的 AGE
将给出启动时间的想法。
- 方法 2: 使用
kubectl events
或 kubectl describe
命令引用 pod 的事件。问题是并非所有容器都有事件。此外,没有明确的方法来了解 pod 处于 READY
状态所花费的时间。
问题:Kubernetes有没有提供任何方法来确定整个pod的启动时间?
随着事情的开始设置,您的 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 解析。
虽然在 Kubernetes 中没有确切的机制,但您可能需要检查 Kube-state-metrics:
kube-state-metrics is a simple service that listens to the Kubernetes
API server and generates metrics about the state of the objects. (See
examples in the Metrics section below.) It is not focused on the
health of the individual Kubernetes components, but rather on the
health of the various objects inside, such as deployments, nodes and
pods.
使用它您可以导出对象创建时间。在这种情况下,指标名称将为 kube_<OBJECT>_created
。 article 第 6 节中关于指标的内容详细解释了 kube 状态指标和对象创建时间的使用。
请注意,如果您要测量 pod start time
,您假设 运行 该 pod 所需的所有图像已经 pre-pulled 在机器上。否则您的测量将不正确,因为它将包括影响 Kubernetes 性能的变量,例如网络或图像大小。
如果这可以帮助任何人,这里是一个使用 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 "[=10=]" "+%s") - $(date -d "" "+%s") ))'
67
70
70
72
...
我想知道在 Kubernetes 中启动我的 pod 所需的时间。我的广告连播有 3 Init Containers
和 9 actual Containers
。
到目前为止,这是我尝试过的:
- 方法一:启动pod并监控直到有9/9个容器运行。
kubectl describe pod <pod_name>
中的AGE
将给出启动时间的想法。 - 方法 2: 使用
kubectl events
或kubectl describe
命令引用 pod 的事件。问题是并非所有容器都有事件。此外,没有明确的方法来了解 pod 处于READY
状态所花费的时间。
问题:Kubernetes有没有提供任何方法来确定整个pod的启动时间?
随着事情的开始设置,您的 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 解析。
虽然在 Kubernetes 中没有确切的机制,但您可能需要检查 Kube-state-metrics:
kube-state-metrics is a simple service that listens to the Kubernetes API server and generates metrics about the state of the objects. (See examples in the Metrics section below.) It is not focused on the health of the individual Kubernetes components, but rather on the health of the various objects inside, such as deployments, nodes and pods.
使用它您可以导出对象创建时间。在这种情况下,指标名称将为 kube_<OBJECT>_created
。 article 第 6 节中关于指标的内容详细解释了 kube 状态指标和对象创建时间的使用。
请注意,如果您要测量 pod start time
,您假设 运行 该 pod 所需的所有图像已经 pre-pulled 在机器上。否则您的测量将不正确,因为它将包括影响 Kubernetes 性能的变量,例如网络或图像大小。
如果这可以帮助任何人,这里是一个使用 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 "[=10=]" "+%s") - $(date -d "" "+%s") ))'
67
70
70
72
...