I want to get the cpu and memory usage of a particular pod inside my python program. I tried heapster, but since it is in deprecating state I was forced to use metrics-server. metrics-server returns cpu and memory usage of that command executed time. Heapster was returning list of metrics in each minute from the pod created time to command executed time and also within a start time and end time. How will we get the same using metrics-server?like memory and cpu usage within a start and end time?
Is there any flags to get that?
I tried this - using metrics-server
kubectl 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..
I expect the metrics within a start time and end time. But this gives metrics usage at the current time.
I used kuberenetes client package in python to execute kubectl commands inside a pod and using ‘threding’, collected maximum of metrics within a period.
It's not possible via metrics-server, you can only get currrently used resources (see docs).
For actual monitoring check out Prometheus or similar services.