K8S – python client获取POD资源利用率(CPU/内存)
获取与展示POD级甚至Container级的资源利用率是很常见的发布系统需求,然而网上并没有什么资料告诉大家怎么做,本文将告诉大家原理以及python代码实践。
命令行获取
其实kubectl是可以获取到node、pod、container三个级别的资源利用率情况的,只不过大家可能不了解。
1 2 3 4 5 |
[root@10-42-74-90 ~]# kubectl top nodes NAME CPU(cores) CPU% MEMORY(bytes) MEMORY% 10.42.187.205 1691m 5% 14876Mi 24% 10.42.37.63 513m 6% 12994Mi 92% 10.42.8.102 231m 2% 8124Mi 57% |
利用top获取了节点级别的资源利用率。
1 2 3 |
[root@10-42-74-90 ~]# kubectl top pods redis-master-fsx46 NAME CPU(cores) MEMORY(bytes) redis-master-fsx46 1m 14Mi |
获取default命名空间下,redis-master-fsx46这个POD的资源利用率。
1 2 3 |
[root@10-42-74-90 ~]# kubectl top pods redis-master-fsx46 --containers POD NAME CPU(cores) MEMORY(bytes) redis-master-fsx46 master 1m 14Mi |
甚至打印出具体每个container的资源利用情况(这个POD只有1个container叫做master)。
1 2 3 |
[root@10-42-74-90 ~]# kubectl top pods -l name=redis-master NAME CPU(cores) MEMORY(bytes) redis-master-fsx46 2m 14Mi |
还能按标签筛选pods。
kubectl可以做到,python一样可以做到。
原理说明
其实目前新版的K8S在监控这块的架构已经非常明确了,只不过国内很少有文章解释这一块,其官方架构说明见:https://github.com/kubernetes/community/blob/master/contributors/design-proposals/instrumentation/monitoring_architecture.md。
其架构分位2个部分:
- 内置于K8S的核心指标采集,安装K8S就自带了(下图黑色部分)。
- 第三方的监控采集方案,需要大家自己选型,比如prometheus等(下图蓝色部分)。
像kubectl top获取的cpu/mem使用情况,就属于K8S内置的核心指标采集而来,完全不需要第三方的支持。
大家可能听过下面几个监控相关的东西:
- cadvisor
- kube-state-metrics
- metrics-server
- heapster
- prometheus
不知道到底它们是什么关系,到底要用哪个。
我简单给它们分分类,帮助大家清楚它们的定位:
- 非K8S内置(第三方):
- cadvisor:每个node部署1个,独立程序,用于采集dockerd容器信息,暴露prometheus格式接口供采集。
- kube-state-metrics:集群级,监听K8S资源变化,暴露prometheus格式接口供采集。
- prometheus:采集器,其数据源可以是cadvisor/kube-state-metrics或者k8s内置的数据源。
- K8S内置(官方方案):
- kubelet:每个node部署1个,其内置了部分cadvisor功能,因此它也知道所有容器监控信息,也对外提供prometheus格式的采集接口。
- metrics-server:集群级,它采集kubelet最新资源利用率数据到内存,不保存历史,并且通过API Server开放对外Restful资源接口获取JSON格式的实时metrics数据,可以按node/pod/container级别查看资源利用率信息。
- heapster:其实就是metrics-server的前身,作用基本类似,不过已经被metrics-server彻底取代,其官网有如下说明,也就是说基本的CPU/memory监控已经被metrics-server取代:
- RETIRED: Heapster is now retired. See the deprecation timeline for more information on support. We will not be making changes to Heapster.
- The following are potential migration paths for Heapster functionality:
- For basic CPU/memory HPA metrics: Use metrics-server.
如果大家理解上述内容的话就会知道,我们只需要确保metrics-server运行在K8S集群中,那么就可以通过API server得到所有资源利用率情况,这也是kubectl top的工作原理。
与metrics-server通讯
那么当前,metrics-server在API SERVER注册的GROUP叫做metrics.k8s.io,VERSION是v1beta1,所以其对应的Restful资源URI就是以:/apis/metrics.k8s.io/v1beta1/为前缀的。
metrics-server官方已经说明了其资源URI的几种形式:https://github.com/kubernetes/community/blob/master/contributors/design-proposals/instrumentation/resource-metrics-api.md。
The list of supported endpoints:
/nodes
– all node metrics; type[]NodeMetrics
/nodes/{node}
– metrics for a specified node; typeNodeMetrics
/namespaces/{namespace}/pods
– all pod metrics within namespace with support forall-namespaces
; type[]PodMetrics
/namespaces/{namespace}/pods/{pod}
– metrics for a specified pod; typePodMetrics
The following query parameters are supported:
labelSelector
– restrict the list of returned objects by labels (list endpoints only)
所以,为了获取某个namespace下面的pods的资源利用率,我们可以有2种方式:
- 按标签筛选:/apis/metrics.k8s.io/v1beta1/namespaces/{namespace}/pods?labelSelector=xxxx
- 按pod名字获取:/apis/metrics.k8s.io/v1beta1/namespaces/{namespace}/pods/{pod}
下面先教大家如何在命令行下按HTTP调用URL的获取资源信息。
首先启动一个proxy,它会帮我们解决和API SERVER之间的认证问题,我们只需要关注于接口参数即可:
kubectl proxy –port 8888 &
然后我们请求localhost:8888就可以免认证的调用到API SERVER了:
curl localhost:8888/apis/metrics.k8s.io/v1beta1/nodes
会得到nodes级的资源利用率:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
{ "kind": "NodeMetricsList", "apiVersion": "metrics.k8s.io/v1beta1", "metadata": { "selfLink": "/apis/metrics.k8s.io/v1beta1/nodes" }, "items": [ { "metadata": { "name": "10.42.8.102", "selfLink": "/apis/metrics.k8s.io/v1beta1/nodes/10.42.8.102", "creationTimestamp": "2019-11-26T06:23:17Z" }, "timestamp": "2019-11-26T06:22:21Z", "window": "30s", "usage": { "cpu": "250264852n", "memory": "8318172Ki" } }, { "metadata": { "name": "10.42.37.63", "selfLink": "/apis/metrics.k8s.io/v1beta1/nodes/10.42.37.63", "creationTimestamp": "2019-11-26T06:23:17Z" }, "timestamp": "2019-11-26T06:22:27Z", "window": "30s", "usage": { "cpu": "551516196n", "memory": "13280692Ki" } }, { "metadata": { "name": "10.42.187.205", "selfLink": "/apis/metrics.k8s.io/v1beta1/nodes/10.42.187.205", "creationTimestamp": "2019-11-26T06:23:17Z" }, "timestamp": "2019-11-26T06:22:20Z", "window": "30s", "usage": { "cpu": "1630534153n", "memory": "15209140Ki" } } ] } |
获取POD级只需要调整URI:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
[root@10-42-74-90 ~]# curl localhost:8888/apis/metrics.k8s.io/v1beta1/namespaces/default/pods/redis-master-fsx46 { "kind": "PodMetrics", "apiVersion": "metrics.k8s.io/v1beta1", "metadata": { "name": "redis-master-fsx46", "namespace": "default", "selfLink": "/apis/metrics.k8s.io/v1beta1/namespaces/default/pods/redis-master-fsx46", "creationTimestamp": "2019-11-26T06:24:40Z" }, "timestamp": "2019-11-26T06:24:16Z", "window": "30s", "containers": [ { "name": "master", "usage": { "cpu": "969654n", "memory": "15012Ki" } } ] } |
是不是很简单?但是cpu里面为什么有个字母n?memory里面为什么有一个Ki?到底是啥意思?
好了,关于更多细节以及如何用python client调用,请付费加入我的知识星球获取。
阅读后续内容?
你必须付费加入我的知识星球,为有效知识付费是对作者最好的回报。
二维码见下方 或者 右侧边栏。

1
1