<p>I am using the nodejs kubernetes client <a href="https://www.npmjs.com/package/@kubernetes/client-node" rel="nofollow noreferrer">@kubernetes/client-node</a> in a current project and I want to collect metrics (memory, network, cpu) from pods</p>
<p>now with the cli I'd just use</p>
<pre class="lang-sh prettyprint-override"><code>kubectl top pods
</code></pre>
<p>or exec /sys/fs/cgroups/cpu/...</p>
<p>However I was unable to find the correct API or call in the npm package. Unfortunately the documentation only points to the official API Reference which unfortunately doesn't help me in this case.</p>
<p>So to boil the question down it would be.</p>
<p><strong>How can I get metrics from a pod using the node.js @kubernetes/client-node module?</strong></p>

relief.melone
<p>The <a href="https://github.com/kubernetes-client/javascript" rel="nofollow noreferrer">GitHub repository</a> contains the following two examples:</p>
<ul>
<li><a href="https://github.com/kubernetes-client/javascript/blob/32c11630a323b8a98bb936af3163fe28818c1e85/examples/top.js" rel="nofollow noreferrer">top.js</a></li>
<li><a href="https://github.com/kubernetes-client/javascript/blob/32c11630a323b8a98bb936af3163fe28818c1e85/examples/top_pods.js" rel="nofollow noreferrer">top_pods.js</a></li>
</ul>
<p>The essential parts:</p>
<pre><code>const kc = new k8s.KubeConfig();
kc.loadFromDefault();
const k8sApi = kc.makeApiClient(k8s.CoreV1Api);

// node
k8s.topNodes(k8sApi).then(...);

// pods
const metricsClient = new k8s.Metrics(kc);
k8s.topPods(k8sApi, metricsClient).then(...);
k8s.topPods(k8sApi, metricsClient, &quot;namespace&quot;).then(...);
</code></pre>
