Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to find how to get the cpu and memory metrics per pod #1247

Closed
efotopoulou opened this issue Sep 4, 2020 · 11 comments
Closed

Not able to find how to get the cpu and memory metrics per pod #1247

efotopoulou opened this issue Sep 4, 2020 · 11 comments
Assignees
Labels
kind/feature Categorizes issue or PR as related to a new feature. lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed.

Comments

@efotopoulou
Copy link

What is the feature and why do you need it:
Current version of kubernetes client seems not to promote the access at current monitoring metrics from the ApiClient(). For example:

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

Instead has introduced a set of Classes making possible the interaction with k8s cluster.
I can not find how to get the current cpu,memory (or any other custom metric) values via the proposed APIs .

I see it exists the model V2beta2PodsMetricStatus which indicates the current value of a metric describing each pod in the current scale target, but i can not find with Class to use so as to acces it.

Describe the solution you'd like to see:
I would like to be able to acces the current values of all metrics of a pod or all pods.
A usage example would be really helpfull.

Thank you very much for your time and effort!

@efotopoulou efotopoulou added the kind/feature Categorizes issue or PR as related to a new feature. label Sep 4, 2020
@roycaihw
Copy link
Member

/assign @yliaog

@yliaog
Copy link
Contributor

yliaog commented Sep 14, 2020

https://kubernetes.io/docs/tasks/debug-application-cluster/resource-metrics-pipeline/

provides information about how to setup metrics pipeline to collect metrics. do you have it setup properly in you cluster? could you use kubectl to get the metrics?

@yliaog
Copy link
Contributor

yliaog commented Sep 14, 2020

also please try using the dynamic python client, https://github.com/kubernetes-client/python-base/tree/master/dynamic

@efotopoulou
Copy link
Author

Thank you yliaog for your response. i can fetch the metrics using kubectl without problem. The question was if i can get them via the kubernetes-client APIs.
I am not sure which API i should use so as to fetch the V2beta2PodsMetricStatus model.

Just for helping any other developer, this is how i fetch the metrics from kubectl via my python app:

p = subprocess.getoutput('kubectl get --raw /apis/metrics.k8s.io/v1beta1/namespaces/default/pods')
ret_metrics = json.loads(p)
items = ret_metrics['items']

Not sure how to use the dynamic client you propose. The link you send me misses documentation.

@shivakumargn
Copy link

Any updates on this? The official client does not seem to have implementation for any apis and models for the /apis/metrics.k8s.io objects.

@yliaog
Copy link
Contributor

yliaog commented Nov 2, 2020

could you please check out the dynamic client test code: https://github.com/kubernetes-client/python-base/blob/master/dynamic/test_client.py?

it has some examples with custom resources.

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jan 31, 2021
@fejta-bot
Copy link

Stale issues rot after 30d of inactivity.
Mark the issue as fresh with /remove-lifecycle rotten.
Rotten issues close after an additional 30d of inactivity.

If this issue is safe to close now please do so with /close.

Send feedback to sig-contributor-experience at kubernetes/community.
/lifecycle rotten

@k8s-ci-robot k8s-ci-robot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Mar 2, 2021
@fejta-bot
Copy link

Rotten issues close after 30d of inactivity.
Reopen the issue with /reopen.
Mark the issue as fresh with /remove-lifecycle rotten.

Send feedback to sig-contributor-experience at kubernetes/community.
/close

@k8s-ci-robot
Copy link
Contributor

@fejta-bot: Closing this issue.

In response to this:

Rotten issues close after 30d of inactivity.
Reopen the issue with /reopen.
Mark the issue as fresh with /remove-lifecycle rotten.

Send feedback to sig-contributor-experience at kubernetes/community.
/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@kposborne
Copy link

From this stack overflow question, here is an example of using the custom objects api to get the metrics:
https://stackoverflow.com/questions/63295451/i-need-to-get-resource-usage-of-pods-in-a-kubernetes-cluster-with-kubernetes-pyt

>>> from kubernetes import client, config
>>> 
>>> config.load_kube_config()
>>> api = client.CustomObjectsApi()
>>> resource = api.list_namespaced_custom_object(group="metrics.k8s.io",version="v1beta1", namespace="default", plural="pods")
>>> for pod in resource["items"]:
...    print(pod['containers'], "\n")
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature Categorizes issue or PR as related to a new feature. lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed.
Projects
None yet
Development

No branches or pull requests

7 participants