<p>I would like to develop a simple webservice, which returns kubernetes metrics like node / pod CPU and RAM usage in percentage.
In CLI there are commands like <code>kubectl top node</code> or <code>kubectl describe node</code> with all the metrics i need. However i am unable find such metrics in kubernetes REST api endpoints. The closest one i got is <code>/apis/metrics.k8s.io/v1beta1/nodes</code> endpoint, which shows CPU and RAM usage but no in percentage.</p>
<p>Is there such an endpoint which show the same information as in <code>top</code> or <code>describe</code> commands or, perhaps, there are better way than <code>metrics.k8s.io</code> addon.</p>

<p>When you issue <code>kubectl top node</code>, kubectl makes multiple HTTP requests to multiple endpoints. You can see exact endpoints by adding <code>--v=9</code> flag to <code>kubectl</code>.</p>
<p>For example, in my case</p>
<pre class="lang-text prettyprint-override"><code>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 &quot;Accept: application/json, */*&quot; -H &quot;User-Agent: kubectl/v1.21.0 (linux/amd64) kubernetes/cb303e6&quot; 'https://&lt;ip-address&gt;/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 &quot;Accept: application/json, */*&quot; -H &quot;User-Agent: kubectl/v1.21.0 (linux/amd64) kubernetes/cb303e6&quot; 'https://&lt;ip-address&gt;/api/v1/nodes/gke-cluster-1-default-pool-99238d56-bv6z'
[...]
</code></pre>
<p><em>(there are a lot more, those two are important to answer your question)</em></p>
<p>First request return</p>
<pre class="lang-json prettyprint-override"><code>{
    &quot;kind&quot;:&quot;NodeMetrics&quot;,
    &quot;apiVersion&quot;:&quot;metrics.k8s.io/v1beta1&quot;,
    &quot;metadata&quot;:{
        &quot;name&quot;:&quot;gke-cluster-1-default-pool-99238d56-bv6z&quot;,
        &quot;selfLink&quot;:&quot;/apis/metrics.k8s.io/v1beta1/nodes/gke-cluster-1-default-pool-99238d56-bv6z&quot;,
        &quot;creationTimestamp&quot;:&quot;2021-07-26T08:41:19Z&quot;
    },
    &quot;timestamp&quot;:&quot;2021-07-26T08:41:07Z&quot;,
    &quot;window&quot;:&quot;30s&quot;,
    &quot;usage&quot;:{
        &quot;cpu&quot;:&quot;86855567n&quot;,
        &quot;memory&quot;:&quot;950228Ki&quot;
    }
}
</code></pre>
<p><code>.usage.cpu</code> and <code>.usage.memory</code> show used CPU and memory respectively.</p>
<p>Second request return (truncated, the response is huge)</p>
<pre class="lang-json prettyprint-override"><code>{
    &quot;status&quot;:{
        &quot;capacity&quot;:{
            &quot;attachable-volumes-gce-pd&quot;:&quot;15&quot;,
            &quot;cpu&quot;:&quot;2&quot;,
            &quot;ephemeral-storage&quot;:&quot;98868448Ki&quot;,
            &quot;hugepages-1Gi&quot;:&quot;0&quot;,
            &quot;hugepages-2Mi&quot;:&quot;0&quot;,
            &quot;memory&quot;:&quot;4031624Ki&quot;,
            &quot;pods&quot;:&quot;110&quot;
        },
        &quot;allocatable&quot;:{
            &quot;attachable-volumes-gce-pd&quot;:&quot;15&quot;,
            &quot;cpu&quot;:&quot;940m&quot;,
            &quot;ephemeral-storage&quot;:&quot;47093746742&quot;,
            &quot;hugepages-1Gi&quot;:&quot;0&quot;,
            &quot;hugepages-2Mi&quot;:&quot;0&quot;,
            &quot;memory&quot;:&quot;2885768Ki&quot;,
            &quot;pods&quot;:&quot;110&quot;
        }
    }
}
</code></pre>
<p><code>.status.allocatable.cpu</code> and <code>.status.allocatable.memory</code> show how much CPU and memory can be allocated to running pods.</p>
<hr />
<p>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</p>
<pre class="lang-text prettyprint-override"><code>$ kubectl top node gke-cluster-1-default-pool-99238d56-bv6z
NAME                                       CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%   
gke-cluster-1-default-pool-99238d56-bv6z   108m         11%    928Mi           32%
</code></pre>
