<p>I want to calculate the actual Container CPU usage by dividing the number of used cores with the CPU limit (number of assignable cores). Accordingly I get two different metrics for 4 pods:</p>

<ul>
<li>Number of used cores for each pod</li>
<li>Number of available cores for each pod</li>
</ul>

<p><strong>My problem:</strong></p>

<p>I'd like to get the CPU usage for each container (number of used cores / number of available cores).</p>

<p><strong>What I tried:</strong></p>

<p>Each of these two queries return exactly what I want:</p>

<ol>
<li>Number of currently used cores for each pod:</li>
</ol>

<p>(I am using label_replace because one metric uses <code>pod_name</code> as metric name and the other uses <code>pod</code>)</p>

<p><code>label_replace(sum(rate(container_cpu_usage_seconds_total{pod_name=~"rewe-bd-palantir-vernemq.*", container_name="vernemq"}[1m])) by (pod_name), "pod", "$1", "pod_name", "(.*)")
</code></p>

<p>Response: <a href="https://monosnap.com/direct/6EPuLF59HBJaYsAmKG6CM0fRPyUXDk" rel="nofollow noreferrer">https://monosnap.com/direct/6EPuLF59HBJaYsAmKG6CM0fRPyUXDk</a></p>

<ol start="2">
<li>Number of available cores for each pod:</li>
</ol>

<p><code>sum(kube_pod_container_resource_limits_cpu_cores{pod=~"rewe-bd-palantir-vernemq.*", container="vernemq", job="kubernetes-pods"}) by (pod)
</code></p>

<p>Response: <a href="https://monosnap.com/direct/dRBfitwcxHIrTRYDmYHwV5YkomYJjH" rel="nofollow noreferrer">https://monosnap.com/direct/dRBfitwcxHIrTRYDmYHwV5YkomYJjH</a></p>

<p>This query didn't work (returned no data points):</p>

<pre><code>label_replace(sum(rate(container_cpu_usage_seconds_total{pod_name=~"rewe-bd-palantir-vernemq.*", container_name="vernemq"}[1m])) by (pod_name), "pod", "$1", "pod_name", "(.*)") / sum(kube_pod_container_resource_limits_cpu_cores{pod=~"rewe-bd-palantir-vernemq.*", container="vernemq", job="kubernetes-pods"}) by (pod)
</code></pre>

<p><strong>My question:</strong></p>

<p>How could I achieve a query which returns the CPU usage (number of used cores / number of available cores) for each pod? </p>

<p>You need to use the <code>on()</code> function as well. So it would be like this.</p>

<pre><code>label_replace(sum(rate(container_cpu_usage_seconds_total{pod_name=~"rewe-bd-palantir-vernemq.*", container_name="vernemq"}[1m])) by (pod_name), "pod", "$1", "pod_name", "(.*)") / on(pod) sum(kube_pod_container_resource_limits_cpu_cores{pod=~"rewe-bd-palantir-vernemq.*", container="vernemq", job="kubernetes-pods"}) by (pod)
</code></pre>
