不使用第三方工具检查 kubernetes pod CPU 和内存

Checking kubernetes pod CPU and memory without using third party tool

我遵循了本页中第一个可能的解决方案:Checking kubernetes pod CPU and memory

我试过命令:

kubectl exec pod_name -- /bin/bash

但它没有用,所以我尝试了命令:

kubectl exec -n [namespace] [pod_name] -- cat test.log

我知道这个是因为当我 运行 命令时:

kubectl get pods --all-namespaces | grep [pod_name]

这是我看到的:

POD_NAME

但我收到此错误消息:

OCI runtime exec failed: exec failed: container_linux.go:349: starting container process caused "exec: \"cat\": executable file not found in $PATH": unknown
command terminated with exit code 126

你能告诉我如何解决这个问题吗?

##更新 我试了k9s工具也看不到CPU, finished pods的MEM,看不到CPU, finished pods的MEM是正常的吗?

k9s

查看 pod 的 cpu 和内存使用情况的最直接方法是安装 metrics server,然后使用 kubectl top podskubectl top pod <pod-name>

指标服务器对集群的影响很小,它将帮助您监控集群。

你链接的 SO post 中的答案对我来说似乎是一个 hack,绝对不是监控 pod 资源使用情况的常用方法。

您似乎想要检查内存和 CPU Pods 的使用情况,即 Completed

I tried the k9s tool and I also cannot see CPU, MEM of finished pods, is it normal that we cannot see CPU, MEM of the finished pods ?

I notice it's already running but with this command kubectl top pods -n %namespace% I do see the CPU, MEM of the Running pod but not the one that was already completed.

标记为 Completed 的 Pod 不再 运行(已终止),我们无法使用 kubectl exec 命令连接到它:

$ kubectl exec -it -n cronjob hello-1618235100-xwxkc -- bash
error: cannot exec into a container in a completed pod; current phase is Succeeded 

我们可以使用 kubectl get -ojson 命令查看 Pod phase

$ kubectl get pod hello-1618235100-xwxkc -n cronjob
NAME                     READY   STATUS      RESTARTS   AGE
hello-1618235100-xwxkc   0/1     Completed   0          6m11s

$ kubectl get pod hello-1618235100-xwxkc -n cronjob -ojson | grep -i phase
        "phase": "Succeeded",
   

Pod phase 文档中所示:

Succeeded - All containers in the Pod have terminated in success, and will not be restarted.

无法使用 kubectl top 命令显示 Pods,因为指标服务器不存储指标历史记录(请参阅:Metrics Server documentation 文档):

Only the most recent value of each metric will be remembered. If a user needs an access to historical data they should either use 3rd party monitoring solution or archive the metrics on their own

例如,我使用 Prometheus + Grafana 并可以访问我的 Pods 的历史数据: