How to calculate containers' cpu usage in kubernetes with prometheus as monitoring?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Calculating Container CPU Usage in Kubernetes with Prometheus
Monitoring CPU usage is crucial for ensuring efficient resource utilization and identifying performance bottlenecks in Kubernetes environments. Prometheus, an open-source monitoring solution, provides a robust framework to capture and analyze container metrics, including CPU usage. This article will guide you through understanding and calculating containers' CPU usage using Prometheus within a Kubernetes cluster.
Understanding Kubernetes and Prometheus Metrics
Kubernetes dynamically allocates resources like CPU and memory based on the needs of running containers. Each node in a Kubernetes cluster has a kubelet agent that exposes metrics through a REST API. These metrics can be consumed by monitoring systems such as Prometheus for detailed analysis.
Prometheus is designed to collect metrics from various locations, store them efficiently, and make them available for querying. It uses a powerful query language called PromQL and supports custom alerts to notify operations teams of potential issues.
Key Metrics for Calculating CPU Usage
To accurately calculate CPU usage, Prometheus needs to be configured to scrape the appropriate metrics from Kubernetes nodes. Two key metrics can be used:
container_cpu_usage_seconds_total: The total CPU usage time measured in seconds for each container.container_cpu_cfs_quotaandcontainer_cpu_cfs_period: Metrics useful for understanding the CPU constraints applied to the container via cgroups (control groups).
Steps to Calculate CPU Usage in Prometheus
- Install Prometheus in Kubernetes: Use Helm or any preferred method to deploy Prometheus. Ensure it's configured to scrape metrics from the kubelet endpoints.
- Configure Metric Collection: Confirm that the necessary metrics are enabled on the kubelet. The kubelet typically exposes these metrics on port 10250.
- Write PromQL Query: Use Prometheus Query Language (PromQL) to calculate CPU usage.
This query computes the per-second average rate of CPU usage over the last minute for the specified pod in the namespace your-namespace.
- Normalize CPU Usage: Sometimes, analysis might require normalization of CPU usage. You can divide the usage rate by the number of available CPU cores to get a percentage usage:
- Visualize and Alert: Use the Prometheus UI, Grafana (which integrates well with Prometheus), or any other tool to visualize these metrics. Set up alerts to notify you when CPU usage crosses a predefined threshold.
Example Prometheus Configuration
Below is an example of a prometheus.yml configuration file snippet to scrape metrics from kubelets:
Summary Table
| Step | Description |
| Install Prometheus | Deploy Prometheus in your Kubernetes cluster. |
| Configure Metrics | Ensure kubelet metrics are accessible on each node. |
| Write PromQL Query | Use PromQL to create queries for CPU usage. |
| Normalize Usage | Calculate as a percentage if needed, based on available CPU cores. |
| Visualize & Alert | Use Prometheus UI/Grafana and set up alerts. |
Additional Considerations
- Scaling Prometheus: As your cluster grows, consider scaling Prometheus or using Thanos for long-term storage and high availability.
- Security: Secure Prometheus endpoints with HTTPS and authentication to protect sensitive metrics.
- Resource Optimization: Use insights from CPU usage analysis to optimize resource allocations by updating Kubernetes resource requests and limits for different workloads.
Final Thoughts
By leveraging Prometheus' powerful monitoring capabilities, Kubernetes administrators can gain deep insights into their containers' CPU usage. This not only helps in maintaining the performance and reliability of applications but also assists in efficient resource management and cost optimization.
Related reading
- How to call the services within a mesh in ISTIO?
- How to change --horizontal-pod-autoscaler-sync-period field in kube-controller-manager to 5sec in gke
- How to change a running pod name?
- How to change control-plane-endpoint option in a live cluster?
- How to change default K8s cluster registry?
- How to change nginx config in amazon elastic beanstalk running a docker instance
- How to calculate the CodeSha256 of aws lambda deployment package before uploading
- How to change broadcasted ip in tomcat cluster

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.