簡體   English   中英

如何從 metric-server 獲取 Pod CPU 和 Memory Usage?

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

我目前在我的 K8s 集群中安裝並運行了度量服務器。

利用 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))

在響應中,對於每個 pod,所有容器都將列出它們的 cpu 和 memory 使用情況:

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

現在我的問題是如何獲取 pod 本身而不是其容器的這些指標? 如果可能的話,我寧願不必總結每個容器的指標。 有什么辦法可以用metrics-server做到這一點嗎?

根據官方存儲庫,您可以查詢kubelet stats 端點:

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

這將返回完整 pod 的統計信息。 如果您想查看 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

你也可以找這篇文章 我希望它會幫助你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM