<p>I am using Prometheus tool for monitoring my Kubernetes cluster. </p>

<p>I have set a resource limit(memory limit) in my deployments and need to configure a panel for showing the total  memory available. Please let me know the query needed to run in Prometheus for getting the total memory limit available for my deployment.</p>

manu thankachan
<p>It is possible using metrics kube_pod_container_resource_limits_memory_bytes (provided by <a href="https://github.com/kubernetes/kube-state-metrics" rel="nofollow noreferrer">kube-state-metrics</a>) and container_memory_usage_bytes (provided by kubelet/cAdvisor)</p>

<pre><code>label_replace(
  label_replace(
    kube_pod_container_resource_limits_memory_bytes{},
    "pod_name", 
    "$1", 
    "pod", 
    "(.+)"
  ),
  "container_name", 
  "$1", 
  "container", 
  "(.+)"
) 
-
on(pod_name,namespace,container_name) 
avg(
      container_memory_usage_bytes{pod_name=~".+"}
)
by (pod_name,namespace,container_name)
</code></pre>

<p>A little explanation of the query: It is a subtraction of the memory limit and the actual usage. <code>label_replace</code> functions are needed to match the label names of both metrics, as they are obtained from different targets. <code>avg</code> is used to get the average between pod restarts, as every pod restart creates a new metric. <code>{pod_name=~".+"}</code> is used to filter metrics from <code>container_memory_usage_bytes</code> that are not useful for this case</p>
