How to Calculate Containers' Cpu Usage in Kubernetes With Prometheus as Monitoring?

Better Stack Team
Updated on December 2, 2024

To calculate CPU usage for containers in a Kubernetes cluster using Prometheus, you need to set up Prometheus to scrape metrics from your Kubernetes nodes and pods. Here’s a step-by-step guide on how to achieve this, including a sample query to calculate CPU usage.

Step 1: Set Up Prometheus

If you haven't already set up Prometheus in your Kubernetes cluster, you can use the Prometheus Operator or a Helm chart for an easier installation. The following steps assume that you have Prometheus running and collecting metrics from your Kubernetes cluster.

Step 2: Scrape Metrics from kubelet

Ensure that your Prometheus configuration is set up to scrape metrics from the Kubernetes kubelet. The kubelet exposes metrics for all the containers running on a node.

Here’s a snippet of what your prometheus.yml might look like:

 
scrape_configs:
  - job_name: 'kubelet'
    kubernetes_sd_configs:
      - role: node
    metrics_path: /metrics
    scheme: https
    tls_config:
      insecure_skip_verify: true

Step 3: Use the Right Metrics

Prometheus collects various metrics from the containers. The most relevant ones for CPU usage are:

  • container_cpu_usage_seconds_total: This metric represents the total CPU time consumed by the container.

Step 4: Calculate CPU Usage

To calculate CPU usage for containers, you generally want to look at the rate of CPU usage over a specific time interval. This can be done using the rate function in PromQL.

Here’s a sample query to calculate CPU usage for a specific container over the last 5 minutes:

 
sum(rate(container_cpu_usage_seconds_total{job="kubelet", cluster="", container!="POD"}[5m])) by (pod, namespace)

Breakdown of the Query

  • sum(rate(...[5m])): This calculates the per-second rate of CPU usage over the last 5 minutes. The sum function aggregates this usage across all containers matching the labels.
  • container_cpu_usage_seconds_total{job="kubelet", cluster="", container!="POD"}: This specifies the metric to be used. The label filter container!="POD" excludes infrastructure containers that are not part of your application.
  • by (pod, namespace): This groups the results by pod and namespace, allowing you to see CPU usage per pod in each namespace.

Example: Displaying CPU Usage in Percentage

If you want to display CPU usage as a percentage of the total CPU capacity available to the containers, you can combine it with the kube_pod_container_resource_limits_cpu_cores metric, which represents the CPU limits set for the containers.

Here's an example of how to do that:

 
sum(rate(container_cpu_usage_seconds_total{job="kubelet", cluster="", container!="POD"}[5m])) by (pod, namespace) /
sum(kube_pod_container_resource_limits_cpu_cores{job="kube-state-metrics"}) by (pod, namespace) * 100

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github