kentor
kentor

Reputation: 18514

Calculate value in Group By statement

Use case:

I have 10 Kubernetes nodes (consider them as VMs) which have between 7 and 14 allocatable CPU cores which can be requested by Kubernetes pods. Therefore I'd like to show a table which shows the

  1. Allocatable CPU cores
  2. The requested CPU cores
  3. The ratio of requested / allocatable CPU cores

grouped by node.

The problem

Creating the table for the first 2 requirements was easy. I simply created a table in Grafana and added these two metrics:

sum(kube_pod_container_resource_requests_cpu_cores) by (node)
sum(kube_node_status_allocatable_cpu_cores) by (node)

However I was struggling with the third one. I tried this query, but it didn't return any data apparently:

sum(kube_pod_container_resource_requests_cpu_cores / kube_node_status_allocatable_cpu_cores) by (node)

Question

How can I achieve a calculation of two different metrics in a group by statement in my given example?

Upvotes: 5

Views: 7239

Answers (1)

brian-brazil
brian-brazil

Reputation: 34122

The issue here is that the two have different labels, so you need to aggregate away the extras:

  sum by (node)(kube_pod_container_resource_requests_cpu_cores) 
/ 
  sum by (node)(kube_node_status_allocatable_cpu_cores)

Upvotes: 5

Related Questions