<p>I want to display pod details in the following format using promql/Prometheus.</p>
<p><a href="https://i.stack.imgur.com/AGPBm.png" rel="noreferrer"><img src="https://i.stack.imgur.com/AGPBm.png" alt="Image1" /></a></p>
<p>Furthermore, I want to display CPU and memory utilization of application/component in below format using promql</p>
<p><a href="https://i.stack.imgur.com/SLrzt.png" rel="noreferrer"><img src="https://i.stack.imgur.com/SLrzt.png" alt="Image2" /></a></p>
<p>promql query: sum(container_memory_working_set_bytes) by (pod)</p>
<p>I can get the consumed memory by pod using above query.</p>
<p>How to calculate percentage of memory used ? I am not able to fetch memory limit of stateful pod using promql
Could you please suggest any query/API details ?</p>

Peter
<h3>Per-pod CPU usage in percentage (the query doesn't return CPU usage for pods without CPU limits)</h3>
<pre><code>100 * max(
  rate(container_cpu_usage_seconds_total[5m])
    / on (container, pod)
  kube_pod_container_resource_limits{resource=&quot;cpu&quot;}
) by (pod)
</code></pre>
<p>The <code>kube_pod_container_resource_limits</code> metric can be scraped incorrectly if scrape config for <a href="https://github.com/kubernetes/kube-state-metrics/" rel="noreferrer">kube-state-metrics</a> pod is improperly configured. In this case the original <code>pod</code> label for this metric is moved to the <code>exported_pod</code> label because of <code>honor_labels</code> behavior - see <a href="https://prometheus.io/docs/prometheus/latest/configuration/configuration/#scrape_config" rel="noreferrer">these docs</a> for details. In this case <a href="https://docs.victoriametrics.com/MetricsQL.html#label_replace" rel="noreferrer">label_replace</a> function must be used for moving <code>exported_pod</code> label to <code>pod</code> label:</p>
<pre><code>100 * max(
  rate(container_cpu_usage_seconds_total[5m])
    / on (container, pod)
  label_replace(kube_pod_container_resource_limits{resource=&quot;cpu&quot;}, &quot;pod&quot;, &quot;$1&quot;, &quot;exported_pod&quot;, &quot;(.+)&quot;)
) by (pod)
</code></pre>
<h3>Per-pod memory usage in percentage (the query doesn't return memory usage for pods without memory limits)</h3>
<pre><code>100 * max(
  container_memory_working_set_bytes
    / on (container, pod)
  kube_pod_container_resource_limits{resource=&quot;memory&quot;}
) by (pod)
</code></pre>
<p>If the <code>kube_pod_container_resource_limits</code> metric is scraped incorrectly as mentioned above, then the <a href="https://docs.victoriametrics.com/MetricsQL.html#label_replace" rel="noreferrer">label_replace</a> function must be used for moving <code>exported_pod</code> label value to <code>pod</code>:</p>
<pre><code>100 * max(
  container_memory_working_set_bytes
    / on (container, pod)
  label_replace(kube_pod_container_resource_limits{resource=&quot;memory&quot;}, &quot;pod&quot;, &quot;$1&quot;, &quot;exported_pod&quot;, &quot;(.+)&quot;)
) by (pod)
</code></pre>
