Checking kubernetes pod CPU and memory
https://kubernetes.io/docs/tasks/debug-application-cluster/resource-usage-monitoring/
pod
kubectl top
https://gitee.com/ploynomail/python/blob/master/kubernetes/README.md#documentation-for-api-endpoints
gitee 国内镜像 api 接口
github 国外 api 接口
https://github.com/kubernetes-client/python/blob/master/kubernetes/README.md
kubectl top pod -n <namespace> --no-headers
kubectl exec <podname> -n <namespace> -- cat /sys/fs/cgroup/memory/memory.usage_in_bytes
注意以下Deployment配置清单和kubectl top指令查看pod资源使用率中,都有cpu和内存的两个数量单位(m和Mi),这里把这两个单位解释做个记录:
- cpu单位m:代表 “千分之一核心”,譬如50m的含义是指50/1000核心,即5%
- 内存单位Mi:1Mi = 1024乘1024,而平时使用的单为M是1M = 1000乘1000
[root@k8s-master ~]# echo $((`kubectl exec le-bc5200ee-fcc1-4168-b447-fc28b5aaf9bb-bvrpt -n zj31 -- cat /sys/fs/cgroup/memory/memory.usage_in_bytes`/1000/1000))
633
[root@k8s-master ~]# kubectl top pod le-bc5200ee-fcc1-4168-b447-fc28b5aaf9bb-bvrpt -n zj31
NAME CPU(cores) MEMORY(bytes)
le-bc5200ee-fcc1-4168-b447-fc28b5aaf9bb-bvrpt 5m 600Mi
[root@k8s-master ~]# kubectl top pod -n zj31 --no-headers
le-bc5200ee-fcc1-4168-b447-fc28b5aaf9bb-bvrpt 6m 601Mi
[root@k8s-master ~]# kubectl get all -n zj31
NAME READY STATUS RESTARTS AGE
pod/le-bc5200ee-fcc1-4168-b447-fc28b5aaf9bb-bvrpt 1/1 Running 0 2d2h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/le-bc5200ee-fcc1-4168-b447-fc28b5aaf9bb ClusterIP 10.1.62.2 <none> 80/TCP 2d2h
NAME COMPLETIONS DURATION AGE
job.batch/le-bc5200ee-fcc1-4168-b447-fc28b5aaf9bb 0/1 2d2h 2d2h
api
https://stackoverflow.com/questions/58403810/how-to-get-pod-cpu-and-memory-usage-from-metrics-server
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))
https://stackoverflow.com/questions/57238390/getting-metrics-via-metrics-server-kubernetes
I tried this - using metrics-serverkubectl get --raw /apis/metrics.k8s.io/v1beta1/namespace/<namespace>/pods/<podname>
The above command returns the cpu and memory usage of a specified pod at that time.
Sample output :slight_smile:
{“kind”:“PodMetrics”,“apiVersion”:“metrics.k8s.io/v1beta1”,
“metadata”:{“name”:“podname”,“namespace”:“default”,“selfLink”:"/apis/metrics.k8s.io/v1beta1/namespaces/default/pods/podname",
“creationTimestamp”:“2019-06-26T03:41:25Z”},“timestamp”:“2019-06-26T03:41:00Z”,“window”:“1m0s”,
“containers”:[{“name”:“cassandra”,“usage”:{“cpu”:“4m”,“memory”:“816388Ki”}}]}
I tried the same with curl command also.curl http://127.0.0.1:8001/apis/metrics.k8s.io/v1beta1/namespace/<namespace-name>/pods/<pod-name> -k
This also returns the same..
[root@k8s-master ~]# kubectl top pod le-bc5200ee-fcc1-4168-b447-fc28b5aaf9bb-bvrpt -n zj31
NAME CPU(cores) MEMORY(bytes)
le-bc5200ee-fcc1-4168-b447-fc28b5aaf9bb-bvrpt 5m 602Mi
>>> from kubernetes import client
>>> api_client = client.ApiClient()
>>>
>>> namesapce = 'zj31'
>>> url = '/apis/metrics.k8s.io/v1beta1/namespaces/' + namesapce + '/pods'
>>> ret_metrics = api_client.call_api(url, 'GET', auth_settings=['BearerToken'], response_type='json', _preload_content=False)
>>> response = ret_metrics[0].data.decode('utf-8')
>>> res = json.loads(response)
>>> for i in res['items']:
... for j in i['containers']:
... print('## memory: ',j['usage']['memory'])
...
## memory: 617224Ki
>>> namesapce = 'zj31'
>>> podname = 'le-bc5200ee-fcc1-4168-b447-fc28b5aaf9bb-bvrpt'
>>> url_pod = '/apis/metrics.k8s.io/v1beta1/namespaces/' + namesapce + '/pods/' + podname
>>> ret_metric = api_client.call_api(url_pod, 'GET', auth_settings=['BearerToken'], response_type='json', _preload_content=False)
>>> response = ret_metric[0].data.decode('utf-8')
>>> res = json.loads(response)
>>> for j in res['containers']:
... print('## memory: ',j['usage']['memory'])
...
## memory: 617224Ki
node
[https://dzone.com/articles/kubernetes-resource-usage-how-do-you-manage-and-mo]
To see the how much of its quota each node is using we can use this command, with example output for a 3 node cluster:
kubectl top node
kubectl get nodes --no-headers | awk '{print $1}' | xargs -I {} sh -c 'echo {}; kubectl describe node {} | grep Allocated -A 5 | grep -ve Event -ve Allocated -ve percent -ve -- ; echo'
kubectl get nodes --no-headers | awk '{print $1}' | xargs -I {} sh -c 'echo {}; kubectl describe node {} | grep Allocated -A 5 | grep -ve Event -ve Allocated -ve percent -ve -- ; echo'
gke-rel3170-default-pool-3459fe6a-n03g
CPU Requests CPU Limits Memory Requests Memory Limits
358m (38%) 138m (14%) 516896Ki (19%) 609056Ki (22%)
gke-rel3170-default-pool-3459fe6a-t3b3
CPU Requests CPU Limits Memory Requests Memory Limits
460m (48%) 0 (0%) 310Mi (11%) 470Mi (17%)
gke-rel3170-default-pool-3459fe6a-vczz
CPU Requests CPU Limits Memory Requests Memory Limits
570m (60%) 110m (11%) 430Mi (16%) 790Mi (29%)
[root@k8s-master ~]# kubectl top node
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
k8s-master 406m 0% 5678Mi 2%
k8s-node1 2643m 4% 41689Mi 10%
k8s-node2 261m 0% 26782Mi 5%