<p>Kubernetes top (kubectl top) command shows different memory usage than Linux top command ran inside pod. </p>

<p>I’ve created k8s deployment where YAML contains these memory limits:</p>

<pre><code>resources:
  limits:
    cpu: "1"
    memory: 2500Mi
  requests:
    cpu: 200m
    memory: 2Gi
</code></pre>

<p>Following commands have output as shown: </p>

<pre><code>bash4.4$ kubectl top pod PODNAME 

NAME                     CPU(cores)   MEMORY(bytes)   
openam-d975d46ff-rnp6h   2m           1205Mi
</code></pre>

<p>Run linux top command:</p>

<pre><code>Kubectl exec -it PODNAME top 

Mem: 12507456K used, 4377612K free, 157524K shrd, 
187812K buff, 3487744K cached
</code></pre>

<p>Note ‘free -g’ also shows 11Gb used. </p>

<p>Issue is this contradicts "kubectl top" which shows only 1205 mb used. </p>

Philip Rodriguez
<p>Command <code>kubectl top</code> shows metrics for a given pod. That information is based on reports from <a href="https://github.com/google/cadvisor" rel="noreferrer">cAdvisor</a>, which collects real pods resource usage.</p>

<p>If you run <code>top</code> inside the pod, it will be like you run it on the host system because the pod is using kernel of the host system.
Unix <code>top</code> uses <code>proc</code> virtual filesystem and reads <code>/proc/meminfo</code>file to get an actual information about current memory status. Containers inside pods partially share <code>/proc</code>with the host system include path about a memory and CPU information.</p>

<p>More information you can find in these documents: <a href="https://www.mankier.com/1/kubectl-top-pod" rel="noreferrer">kubectl-top-pod man page</a>, <a href="https://fabiokung.com/2014/03/13/memory-inside-linux-containers/" rel="noreferrer">Memory inside Linux containers</a></p>
