Python: How to get pod monitoring information from SDK?

Created on 7 May 2018  路  18Comments  路  Source: kubernetes-client/python

How to get pod monitoring information from SDK?

lifecyclrotten

Most helpful comment

i do it now this way:

ret_metrics = api_client.call_api('/apis/metrics.k8s.io/v1beta1/namespaces/' + namespace.metadata.name + '/pods', 'GET', auth_settings = ['BearerToken'], response_type='json', _preload_content=False) 
response = ret_metrics[0].data.decode('utf-8')

All 18 comments

What kind of information do you need?

Pod's CPU, memory, hard disk usage and more.

There are not such things in standard API. Please take a look at projects: heapster (deprecated), metric-server, cAdvisor and kube-state-metrics.

Does the Python API support raw queries to the Kubernetes API? If I have the metric-server installed and want to make a raw query to Kubernetes API like: get --raw /apis/metrics.k8s.io/v1beta1/pods/

Issue: metrics-server endpoint #474

@P6rguVyrst yes, it should be possible. Try to use something like this:

api_client = ApiClient()
response = api_client.call_api('/apis/metrics.k8s.io/v1beta1/pods/', 'GET', preaload_content=False)
print(response.text)  # raw HTTPResponse

https://github.com/kubernetes-client/python/blob/master/kubernetes/client/api_client.py#L278

minikube version: v0.28.2

Any chance I'm doing something wrong?

metrics_url = '/apis/metrics.k8s.io/v1beta1/namespaces/kube-system/pods/kube-controller-manager-minikube'
api_client.call_api(metrics_url, 'GET', _preload_content=True)
returns None

(None, 200, HTTPHeaderDict({'Content-Length': '434', 'Content-Type': 'application/json', 'Date': 'Fri, 10 Aug 2018 14:42:47 GMT'}))

when I make the same call via curl, it returns the expected data.
curl --cacert <redacted> --key <redacted> --cert <redacted> https://192.168.99.100:8443/apis/metrics.k8s.io/v1beta1/namespaces/kube-system/pods/kube-controller-manager-minikube

FYI,

I found: .request instead of .call_api
api_client.request('GET', metrics_url, headers=None, _preload_content=True)

This Works!

@mward29 how did you include config parameters here, like the ones from config.load_incluster_config() ?

yes. config.load_kube_config()

FYI. I had to add the full url to make it work. Still unable to get .call_api to work.

metrics_url = 'https://192.168.99.100:8443/apis/metrics.k8s.io/v1beta1/namespaces/kube-system/pods/kube-controller-manager-minikube'

@mward29 i think go get .call_api to work you have to define at least response_type , but i did not find any suitable type in https://github.com/kubernetes-client/python/tree/master/kubernetes/client/models

with

ret= api_client.call_api('/apis/metrics.k8s.io/v1beta1/namespaces/default/pods', 'GET', auth_settings = ['BearerToken'], response_type='json', _preload_content=False)
print(ret)

i get

(<urllib3.response.HTTPResponse object at 0x7f1166713710>, 200, HTTPHeaderDict({'Content-Length': '446', 'Content-Type': 'application/json', 'Date': 'Fri, 10 Aug 2018 06:39:06 GMT'}))

i do it now this way:

ret_metrics = api_client.call_api('/apis/metrics.k8s.io/v1beta1/namespaces/' + namespace.metadata.name + '/pods', 'GET', auth_settings = ['BearerToken'], response_type='json', _preload_content=False) 
response = ret_metrics[0].data.decode('utf-8')

Nice Shinzu

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

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-testing, kubernetes/test-infra and/or fejta.
/lifecycle rotten

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-testing, kubernetes/test-infra and/or fejta.
/close

@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-testing, kubernetes/test-infra and/or fejta.
/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.

Was this page helpful?
0 / 5 - 0 ratings