Tối đang có 2 metrics:
metrics_server_pod_cpu_usage{app_kubernetes_io_instance="metrics-server-exporter-nim-staging", app_kubernetes_io_managed_by="Helm", app_kubernetes_io_name="metrics-server-exporter", app_kubernetes_io_version="v2.0.1", cluster_name="staging-nim-engines", container="file-service", helm_sh_chart="metrics-server-exporter-1.2.1", instance="10.195.222.52:1994", job="kubernetes-service-endpoints", kind_owner="Deployment", name_owner="file-service", namespace="nim-engines-staging", node="ip-10-195-222-225.us-west-2.compute.internal", os="linux", pod="file-service-59fbb8bdf-2qbzd", service="metrics-server-exporter-nim-staging"}
metrics_server_pod_cpu_usage{app_kubernetes_io_instance="metrics-server-exporter-nim-staging",app_kubernetes_io_managed_by="Helm",app_kubernetes_io_name="metrics-server-exporter",app_kubernetes_io_version="v2.0.1",cluster_name="staging-nim-engines",container="istio-proxy",helm_sh_chart="metrics-server-exporter-1.2.1",instance="10.195.222.52:1994",job="kubernetes-service-endpoints",kind_owner="Deployment",name_owner="file-service",namespace="nim-engines-staging",node="ip-10-195-222-225.us-west-2.compute.internal",os="linux",pod="file-service-59fbb8bdf-2qbzd",service="metrics-server-exporter-nim-staging"}
Bạn có thể thấy là 2 metrics của 2 container khác nhau nhưng cùng 1 pod.
nếu như mình muốn tạo 1 query đếm số pods thì sẽ cần làm gì
Đầu tiên chúng ta dùng count by (xxx)
count by (pod) (metrics_server_pod_cpu_usage{cluster_name="$cluster",namespace=~"$namespace",kind_owner=~"$kind",name_owner=~"$owner",pod=~"$pod",container=~"$container"})
This part groups the metrics by the pod
label and counts the number of instances for each unique pod. The count by (pod)
ensures that each pod is counted only once, regardless of how many containers it has
look like:
The outer count
then counts the number of unique pods from the inner query.
count(count by (pod) (metrics_server_pod_cpu_usage{cluster_name="$cluster",namespace=~"$namespace",kind_owner=~"$kind",name_owner=~"$owner",pod=~"$pod",container=~"$container"}))