How can I get node's memory with kubernetes-client lib in python
Question:
Could not find how can I obtain information about memory consumption on kubernetes node with kubernetes-client library in Python. I know how to obtain this information with kubectl
command.
Could you provide me with a piece of code how can I do this via the lib?
Thank you.
Answers:
Getting started
kubernetes-client contains pieces of code that you need:
from kubernetes import client, config
def main():
# Configs can be set in Configuration class directly or using helper
# utility. If no argument provided, the config will be loaded from
# default location.
config.load_kube_config()
v1 = client.CoreV1Api()
print("Listing pods with their IPs:")
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
print("%st%st%s" %
(i.status.pod_ip, i.metadata.namespace, i.metadata.name))
if __name__ == '__main__':
main()
Method
For get pod metrics, try this beta class:
V2beta2PodsMetricStatus
Metrics
According to k8s
documentation Your metrics supposed to be:
-
kube_pod_container_resource_limits_memory_bytes
-
kube_pod_container_resource_requests_memory_bytes
After a long investigation and some good advice (use --v=10
flag in kubectl
command) from the kubernetes-client repository on GitHub I managed to find two resources where I can fetch CPU/memory consumption metrics that I was interested in:
https://your.kube-url.com/api/v1/namespaces/kube-system/services/http:heapster:/proxy/apis/metrics/v1alpha1/namespaces/default/pods
https://your.kube-url.com/api/v1/namespaces/kube-system/services/http:heapster:/proxy/apis/metrics/v1alpha1/nodes
I couldn’t find any code examples how to do the same with kubernetes-client
library that is why will build my own wrappers.
Belated answer, but since this frustrated me so much and I wished there had been a proper answer to this question so I didn’t have to go digging for it I’m writing one now to make it easier on the next poor sap.
Here is how you can get memory for nodes:
from kubernetes import client
custom_api = client.CustomObjectsApi(client.ApiClient())
response = custom_api.list_cluster_custom_object("metrics.k8s.io", "v1beta1", "nodes")
for node in response['items'];
print(f"node {node['metadata']['name']} has available memory of {node['usage']['memory']}")
If you get an unauthorized response when you try to run this your need to update your clusterRole and clusterRoleBinding to authorize this. Your need the ‘list’ action on the resource ‘nodes’ for the v1beta1 api
Could not find how can I obtain information about memory consumption on kubernetes node with kubernetes-client library in Python. I know how to obtain this information with kubectl
command.
Could you provide me with a piece of code how can I do this via the lib?
Thank you.
Getting started
kubernetes-client contains pieces of code that you need:
from kubernetes import client, config
def main():
# Configs can be set in Configuration class directly or using helper
# utility. If no argument provided, the config will be loaded from
# default location.
config.load_kube_config()
v1 = client.CoreV1Api()
print("Listing pods with their IPs:")
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
print("%st%st%s" %
(i.status.pod_ip, i.metadata.namespace, i.metadata.name))
if __name__ == '__main__':
main()
Method
For get pod metrics, try this beta class:
V2beta2PodsMetricStatus
Metrics
According to k8s
documentation Your metrics supposed to be:
-
kube_pod_container_resource_limits_memory_bytes
-
kube_pod_container_resource_requests_memory_bytes
After a long investigation and some good advice (use --v=10
flag in kubectl
command) from the kubernetes-client repository on GitHub I managed to find two resources where I can fetch CPU/memory consumption metrics that I was interested in:
https://your.kube-url.com/api/v1/namespaces/kube-system/services/http:heapster:/proxy/apis/metrics/v1alpha1/namespaces/default/pods
https://your.kube-url.com/api/v1/namespaces/kube-system/services/http:heapster:/proxy/apis/metrics/v1alpha1/nodes
I couldn’t find any code examples how to do the same with kubernetes-client
library that is why will build my own wrappers.
Belated answer, but since this frustrated me so much and I wished there had been a proper answer to this question so I didn’t have to go digging for it I’m writing one now to make it easier on the next poor sap.
Here is how you can get memory for nodes:
from kubernetes import client
custom_api = client.CustomObjectsApi(client.ApiClient())
response = custom_api.list_cluster_custom_object("metrics.k8s.io", "v1beta1", "nodes")
for node in response['items'];
print(f"node {node['metadata']['name']} has available memory of {node['usage']['memory']}")
If you get an unauthorized response when you try to run this your need to update your clusterRole and clusterRoleBinding to authorize this. Your need the ‘list’ action on the resource ‘nodes’ for the v1beta1 api