如何从metrics-server获取Pod CPU和内存使用率?

我目前在K8s集群中安装并运行了度量服务器。

利用kubernetes python lib,我可以发出此请求以获取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))

在响应中,将为每个容器列出所有容器及其CPU和内存使用情况:

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

现在我的问题是我如何获得豆荚本身而不是容器的这些指标?如果可能的话,我宁愿不必总结每个容器的指标。有什么办法可以通过metrics-server做到这一点?

1 个答案:
答案 0 :(得分:0)

基于official repository,您可以查询kubelet统计信息终结点:

https://github.com/kubernetes/kubernetes/blob/release-1.14/pkg/kubelet/server/stats/handler.go#L123-L126

https://github.com/kubernetes/community/blob/master/contributors/design-proposals/instrumentation/resource-metrics-api.md
$ curl --insecure https://<node url>:10250/stats/summary

,它将返回完整Pod的统计信息。如果要查看容器/容器本身的指标,请输入:

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

让我们来看一个例子:

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

这些请求将起作用:

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

猜你喜欢

转载自blog.csdn.net/weixin_44716147/article/details/120608681
今日推荐