How can I get node's memory with kubernetes-client lib in python

9/10/2019

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.

-- Stanislav Hordiyenko
kubernetes
python

2 Answers

9/12/2019

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.

-- Stanislav Hordiyenko
Source: StackOverflow

9/10/2019

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("%s\t%s\t%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

-- Yasen
Source: StackOverflow