如何定期获取kubernetes pod指标并将其附加到文件
我正在通过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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试此单线:
添加
睡眠300
每5分钟而不是连续采样。它将在命名空间
您的namespace
中找到一个带有标签>您的标签名称的豆荚,并且只会仅采用最后一个这样的吊舱,如果您有多个带有相同标签的豆荚(这就是| tail -n 1
for)。这样,您就不必手动确定吊舱的名称。然后,它将打印出类似的内容:其中
1678640345
是Unix毫秒的时间戳,由$(date +%s)
编写。输出将以控制台(STDOUT)打印,并在/path/to/save/your/logs.txt
文件中镜像。Try this one-liner:
Add
sleep 300
to sample it every 5 minutes instead of continuously.It will find a pod in namespace
your-namespace
with labelyour-label-name
that has valueyour-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: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.您可以安装 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.