Kubernetes Rest API node CPU and RAM usage in percentage Kubernetes Rest API node CPU and RAM usage in percentage kubernetes kubernetes

Kubernetes Rest API node CPU and RAM usage in percentage


When you issue kubectl top node, kubectl makes multiple HTTP requests to multiple endpoints. You can see exact endpoints by adding --v=9 flag to kubectl.

For example, in my case

kubectl top node gke-cluster-1-default-pool-99238d56-bv6z --v=9[...]I0726 10:41:18.347144    1986 round_trippers.go:435] curl -k -v -XGET  -H "Accept: application/json, */*" -H "User-Agent: kubectl/v1.21.0 (linux/amd64) kubernetes/cb303e6" 'https://<ip-address>/apis/metrics.k8s.io/v1beta1/nodes/gke-cluster-1-default-pool-99238d56-bv6z'I0726 10:41:18.489068    1986 round_trippers.go:435] curl -k -v -XGET  -H "Accept: application/json, */*" -H "User-Agent: kubectl/v1.21.0 (linux/amd64) kubernetes/cb303e6" 'https://<ip-address>/api/v1/nodes/gke-cluster-1-default-pool-99238d56-bv6z'[...]

(there are a lot more, those two are important to answer your question)

First request return

{    "kind":"NodeMetrics",    "apiVersion":"metrics.k8s.io/v1beta1",    "metadata":{        "name":"gke-cluster-1-default-pool-99238d56-bv6z",        "selfLink":"/apis/metrics.k8s.io/v1beta1/nodes/gke-cluster-1-default-pool-99238d56-bv6z",        "creationTimestamp":"2021-07-26T08:41:19Z"    },    "timestamp":"2021-07-26T08:41:07Z",    "window":"30s",    "usage":{        "cpu":"86855567n",        "memory":"950228Ki"    }}

.usage.cpu and .usage.memory show used CPU and memory respectively.

Second request return (truncated, the response is huge)

{    "status":{        "capacity":{            "attachable-volumes-gce-pd":"15",            "cpu":"2",            "ephemeral-storage":"98868448Ki",            "hugepages-1Gi":"0",            "hugepages-2Mi":"0",            "memory":"4031624Ki",            "pods":"110"        },        "allocatable":{            "attachable-volumes-gce-pd":"15",            "cpu":"940m",            "ephemeral-storage":"47093746742",            "hugepages-1Gi":"0",            "hugepages-2Mi":"0",            "memory":"2885768Ki",            "pods":"110"        }    }}

.status.allocatable.cpu and .status.allocatable.memory show how much CPU and memory can be allocated to running pods.


As you can see, there is no endpoint that return usage in percentage, kubectl does the calculation on the fly, to output the result in human friendly format

$ kubectl top node gke-cluster-1-default-pool-99238d56-bv6zNAME                                       CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%   gke-cluster-1-default-pool-99238d56-bv6z   108m         11%    928Mi           32%


i would recommend you to use official Kubernetes Client library. There you can achieve all features which are available on kubectl.you can achieve kubectl top command using below code snippet

[https://github.com/kubernetes-client/javascript/blob/master/examples/top.js][1]