<p>I am running a load test over a kubernetes pod and i want to sample every 5 minutes the CPU and memory usage of it.
I was currently manually using the linux <code>top</code> command over the kubernetes pod.</p>
<p>Is there any way given a <code>kubernetes pod</code> to fetch the CPU/Memory usage every X minutes and append it to a file ?</p>

<p>Try this one-liner:</p>
<pre class="lang-bash prettyprint-override"><code>while [ true ]; do echo $(date) $(date +%s) $(kubectl top -n your-namespace pod $(kubectl get pods -n your-namespace -l your-label-name=your-label-value -o jsonpath='{..metadata.name}') | tail -n 1) | tee -a /path/to/save/your/logs.txt; done
</code></pre>
<p>Add <code>sleep 300</code> to sample it every 5 minutes instead of continuously.</p>
<p>It will find a pod in namespace <code>your-namespace</code> with label <code>your-label-name</code> that has value <code>your-label-value</code>, take its name, and will take only the last one such pod, if you have multiple pods with the same label (that's what <code>| tail -n 1</code> for). This way you won't have to determine the name of a pod manually. Then it'll print something like this:</p>
<pre><code>Sun, Mar 12, 2023 4:59:05 PM 1678640345 your-pod-name-5c64678fc6-rsldm 47m 657Mi
</code></pre>
<p>Where <code>1678640345</code> is Unix milliseconds timestamp written by <code>$(date +%s)</code>. The output will be printed in console (stdout) and mirrored in <code>/path/to/save/your/logs.txt</code> file.</p>
