如何定期获取kubernetes pod指标并将其附加到文件

发布于 2025-01-26 11:29:05 字数 177 浏览 3 评论 0原文

我正在通过Kubernetes Pod进行负载测试,我想每5分钟采样一次CPU和内存使用情况。 我目前正在手动使用Kubernetes Pod上的Linux top命令。

有什么方法给定kubernetes pod每x分钟获取CPU/内存使用情况并将其附加到文件中?

I am running a load test over a kubernetes pod and i want to sample every 5 minutes the CPU and memory usage of it.
I was currently manually using the linux top command over the kubernetes pod.

Is there any way given a kubernetes pod to fetch the CPU/Memory usage every X minutes and append it to a file ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心清如水 2025-02-02 11:29:05

尝试此单线:

while [ true ]; do echo $(date) $(date +%s) $(kubectl top -n your-namespace pod $(kubectl get pods -n your-namespace -l your-label-name=your-label-value -o jsonpath='{..metadata.name}') | tail -n 1) | tee -a /path/to/save/your/logs.txt; done

添加睡眠300每5分钟而不是连续采样。

它将在命名空间您的namespace中找到一个带有标签>您的标签名称的豆荚,并且只会仅采用最后一个这样的吊舱,如果您有多个带有相同标签的豆荚(这就是| tail -n 1 for)。这样,您就不必手动确定吊舱的名称。然后,它将打印出类似的内容:

Sun, Mar 12, 2023 4:59:05 PM 1678640345 your-pod-name-5c64678fc6-rsldm 47m 657Mi

其中1678640345是Unix毫秒的时间戳,由$(date +%s)编写。输出将以控制台(STDOUT)打印,并在/path/to/save/your/logs.txt文件中镜像。

Try this one-liner:

while [ true ]; do echo $(date) $(date +%s) $(kubectl top -n your-namespace pod $(kubectl get pods -n your-namespace -l your-label-name=your-label-value -o jsonpath='{..metadata.name}') | tail -n 1) | tee -a /path/to/save/your/logs.txt; done

Add sleep 300 to sample it every 5 minutes instead of continuously.

It will find a pod in namespace your-namespace with label your-label-name that has value your-label-value, take its name, and will take only the last one such pod, if you have multiple pods with the same label (that's what | tail -n 1 for). This way you won't have to determine the name of a pod manually. Then it'll print something like this:

Sun, Mar 12, 2023 4:59:05 PM 1678640345 your-pod-name-5c64678fc6-rsldm 47m 657Mi

Where 1678640345 is Unix milliseconds timestamp written by $(date +%s). The output will be printed in console (stdout) and mirrored in /path/to/save/your/logs.txt file.

时光礼记 2025-02-02 11:29:05

您可以安装 Metrics Server ,然后在循环中编写一个小型bash脚本调用kubectl顶部Pods的某些组合 - all-namespaces,然后输出到文件。另一个选择,如果您想要更多的肉,是运行Prometheus和/或 kube-state-Metrics 并将其设置为系统中所有POD的刮擦度量。

You can install the metrics server and then write a small bash script that in a loop that calls some combo of kubectl top pods --all-namespaces and outputs to a file. Another option if you want something with some more meat is to run Prometheus and/or kube-state-metrics and set it up to scrape metrics from all pods in the system.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文