How to get prometheus metrics.

In this tutorial, we’re going to use K8Studio—a Kubernetes UI that I use as a consultant when working on customers' clusters. Well, I guess the first step—if you want to collect metrics from a cluster—is to install Prometheus in your cluster to start gathering data. The easiest way to install Prometheus is by using Helm. You can do this via the terminal, or even more conveniently, by opening the Helm tab in K8Studio and searching for the right chart.

Here is the command if you want to do it on the terminal:

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install prometheus prometheus-community/prometheus

Here's how it's done using K8Studio. I use K8Studio because it makes it easier to search for the right repository, view the README, modify values, and manage the entire lifecycle of the deployment.

helm prometheus.png

This will install Prometheus and set up all the necessary workloads, services, and other resources. It’s not instantaneous, so it may take a little time so be patient. Take a look at your visualizer and after a couple o minutes you should see all the workload,pods and etc running.

Prometheus Running.png

you can also do this with kubectl running this command:

kubectl get pods -n <namespace>

Look for pods with names like:

Prometheus-metrics.png

If you don't have K8studio, that is ok you can use kubectl.

1.The first thing to do is to locate the promeheus service

To list Prometheus-related services:

kubectl get svc -n <namespace>

If you used the default monitoring namespace:

kubectl get svc -n monitoring

You’ll likely see a service named something like prometheus-server. For example:

NAME                   TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
prometheus-server      ClusterIP   10.0.0.123     <none>        80/TCP     5m
2.We need to access the prometheus UI

Port-forward the Prometheus service to your local machine:

kubectl port-forward -n monitoring svc/prometheus-server 9090:80

Then open http://localhost:9090 in your browser. You should see a windows like this. now you can type a query to give you the cpu usage

prometheus-ui.png
Here is a collection of usefull queries that you

1. Instantaneous CPU usage rate per pod
rate(container_cpu_usage_seconds_total{pod!="", image!=""}[5m])

This shows the per-second CPU usage rate (in cores) over the last 5 minutes for all pods.

2. CPU usage for a specific pod
sum(rate(container_cpu_usage_seconds_total{pod="my-pod-name", image!=""}[5m])) by (pod)

Replace my-pod-name with your actual pod name. This sums all container CPU usage within that pod.

3. CPU usage for all pods in a namespace
sum(rate(container_cpu_usage_seconds_total{namespace="my-namespace", image!=""}[5m])) by (pod)

Shows per-pod CPU usage in a given namespace.

4. CPU usage grouped by namespace and pod
sum(rate(container_cpu_usage_seconds_total{image!=""}[5m])) by (namespace, pod)

Good for an overview across multiple namespaces.

5. Top 5 CPU-consuming pods
topk(5, sum(rate(container_cpu_usage_seconds_total{image!=""}[5m])) by (pod))

Displays the top 5 pods with the highest CPU usage.

6. Compare CPU usage vs. CPU requests (Kubernetes metrics)

CPU Requests:

kube_pod_container_resource_requests_cpu_cores

CPU Limits:

kube_pod_container_resource_limits_cpu_cores

Usage vs. Request for a specific pod:

sum(rate(container_cpu_usage_seconds_total{pod="my-pod-name", image!=""}[5m])) by (pod)
/
sum(kube_pod_container_resource_requests_cpu_cores{pod="my-pod-name"}) by (pod)

This gives you the ratio of CPU used vs. requested, helpful for tuning resource requests.

7. CPU throttling (if relevant)

To check if the pod is hitting CPU limits:

rate(container_cpu_cfs_throttled_seconds_total{pod="my-pod-name"}[5m])

Higher values indicate throttling (container is limited by its CPU quota).