简体   繁体   English

如何从 metric-server 获取 Pod CPU 和 Memory Usage?

[英]How to get Pod CPU and Memory Usage from metric-server?

I currently have metric server installed and running in my K8s cluster.我目前在我的 K8s 集群中安装并运行了度量服务器。

Utilizing the the kubernetes python lib, I am able to make this request to get pod metrics:利用 kubernetes python 库,我可以提出这个请求来获取 pod 指标:

from kubernetes import client

api_client = client.ApiClient()
ret_metrics = api_client.call_api(
            '/apis/metrics.k8s.io/v1beta1/namespaces/' + 'default' + '/pods', 'GET',
            auth_settings=['BearerToken'], response_type='json', _preload_content=False)
response = ret_metrics[0].data.decode('utf-8')
print('RESP', json.loads(response))

In the response, for each pod all containers will be listed with their cpu and memory usage:在响应中,对于每个 pod,所有容器都将列出它们的 cpu 和 memory 使用情况:

'containers': [{'name': 'elasticsearch', 'usage': {'cpu': '14122272n', 'memory': '826100Ki'}}]}

Now my question is how do i get these metrics for the pod itself and not its containers?现在我的问题是如何获取 pod 本身而不是其容器的这些指标? I'd rather not have to sum up the metrics from each container if possible.如果可能的话,我宁愿不必总结每个容器的指标。 Is there any way to do this with metrics-server?有什么办法可以用metrics-server做到这一点吗?

Based on the official repository you can query kubelet stats endpoint:根据官方存储库,您可以查询kubelet stats 端点:

$ curl --insecure https://<node url>:10250/stats/summary

which will return stats of full pods.这将返回完整 pod 的统计信息。 If you want to see metrics for pod/container itself, type:如果您想查看 pod/容器本身的指标,请输入:

$ curl --insecure https://<node url>:10250/{namespace}/{podName}/{uid}/{containerName}

Let's take a look for example:让我们看一个例子:

{ "podRef": 
    { "name": "py588590", 
      "namespace": "myapp", 
      "uid": "e0056f1a" 
    }, 
  "startTime": "2019-10-16T03:57:47Z", 
  "containers": 
    [ 
      { "name": "py588590", 
        "startTime": "2019-10-16T03:57:50Z"
      }
    ]
}

These requests will works:这些请求将起作用:

http://localhost:10255/stats/myapp/py588590/e0056f1a/py588590

You can also look for this article .你也可以找这篇文章 I hope it will helps you.我希望它会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM