<p>I have a play framework based java application deployed in kubernetes. One of the pods died due to out of memory/memory leak. In local , can use some utilities and monitor jvm heap usage.  I am new to kubernetes.</p>
<p>Appreciate if you tell how to check for heap usage history of my application in a Kubernetes pod which got killed. kubectl get events on this killed pod will give events history but I want to check object wise heap usage history on that dead pod. Thanks much</p>

ApprenticeWST
<p>You can install addons or external tools like <a href="https://prometheus.io/" rel="nofollow noreferrer">Prometheus</a> or <a href="https://github.com/kubernetes-sigs/metrics-server" rel="nofollow noreferrer">metrics-server</a>.</p>
<p><a href="https://github.com/prometheus" rel="nofollow noreferrer"><strong>Prometheus</strong></a> is an open-source systems monitoring and alerting toolkit originally built at <a href="https://soundcloud.com/" rel="nofollow noreferrer">SoundCloud</a>.</p>
<p>You can define queries:
For CPU percentage</p>
<pre><code>avg((sum (rate (container_cpu_usage_seconds_total {container_name!=&quot;&quot; ,pod=&quot;&lt;Pod name&gt;&quot; } [5m])) by (namespace , pod, container ) / on (container , pod , namespace) ((kube_pod_container_resource_limits_cpu_cores &gt;0)*300))*100)
</code></pre>
<p>For Memory percentage</p>
<pre><code>avg((avg (container_memory_working_set_bytes{pod=&quot;&lt;pod name&gt;&quot;}) by (container_name , pod ))/ on (container_name , pod)(avg (container_spec_memory_limit_bytes&gt;0 ) by (container_name, pod))*100)
</code></pre>
<p>Take a look: <a href="https://stackoverflow.com/questions/55143656/prometheus-queries-to-get-cpu-and-memory-usage-in-kubernetes-pods">prometheus-pod-memory-usage</a>.</p>
<p>You can visualize such metrics using <a href="https://grafana.com/" rel="nofollow noreferrer">Grafana</a> - take a look  how to set it up with Prometheus - <a href="https://medium.com/@pramodshehan/containers-metrics-in-prometheus-and-grafana-389555499eb8" rel="nofollow noreferrer">grafana-prometheus-setup</a>.</p>
<p><strong>Metrics-server</strong> is a scalable, efficient source of container resource metrics for Kubernetes built-in autoscaling pipelines.</p>
<p>Metrics Server collects resource metrics from Kubelets and exposes them in Kubernetes apiserver through  <a href="https://github.com/kubernetes/metrics" rel="nofollow noreferrer">Metrics API</a>  for use by  <a href="https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/" rel="nofollow noreferrer">Horizontal Pod Autoscaler</a>  and  <a href="https://github.com/kubernetes/autoscaler/tree/master/vertical-pod-autoscaler" rel="nofollow noreferrer">Vertical Pod Autoscaler</a>. Metrics API can also be accessed by  <code>kubectl top</code>, making it easier to debug autoscaling pipelines.</p>
<p>You can execute:</p>
<pre><code>$ kubectl top pod &lt;your-pod-name&gt; --namespace=your-namespace --containers
</code></pre>
<p>The following command will give you both the CPU usage as well as the memory usage for a given pod and its containers.</p>
<p>See how to firstly install metrics-server: <a href="https://github.com/kubernetes-sigs/metrics-server" rel="nofollow noreferrer">metrics-server-installtion</a>.</p>
<p>Otherwise if you want to check cpu/memory usage without installing any third party tool then you can get memory and cpu usage of pod from cgroup.</p>
<ol>
<li>Go to shell of running container  <code>kubectl exec pod_name -- /bin/bash</code></li>
<li>Go to  <code>cd /sys/fs/cgroup/cpu</code>  for cpu usage run  <code>cat cpuacct.usage</code></li>
<li>Go to  <code>cd /sys/fs/cgroup/memory</code>  for memory usage run  <code>cat memory.usage_in_bytes</code></li>
</ol>
<p>Remember that memory usage is in bytes.</p>
<p>Take a look: <a href="https://stackoverflow.com/questions/54531646/checking-kubernetes-pod-cpu-and-memory">memory-usage-kubernetes</a>.</p>
