Diagnosing Resource Contention in Kubernetes with CPU PSI
In a Kubernetes environment, simply monitoring CPU utilization can lead to misleading conclusions about performance issues. CPU PSI provides deeper insights by measuring how much time tasks are ready to run but are waiting for CPU resources. This metric helps you pinpoint contention and optimize resource allocation, ultimately improving application performance.
Kubernetes exposes PSI metrics through the kubelet starting from version 1.36. You can access these metrics using the kubelet Summary API, which provides a comprehensive view of CPU usage alongside PSI data. The command kubectl get --raw "/api/v1/nodes/<node-name>/proxy/stats/summary" | jq '{cores: (.node.cpu.usageNanoCores / 1e9), psi: .node.cpu.psi}' gives you a clear picture of CPU usage and PSI metrics for each node and pod. This allows you to see not just how much CPU is being used, but also how often tasks are waiting for CPU resources, which is crucial for diagnosing performance bottlenecks.
In production, understanding CPU PSI can significantly enhance your ability to manage workloads effectively. Be aware that you need a Linux host with cgroup v2 and a kernel that has PSI enabled. Also, ensure you have tools like Docker, kind, kubectl, and jq installed, with at least four CPUs available for accurate measurements. Keep in mind the warning about using the --kubelet-insecure-tls flag when deploying the metrics server, as this can expose your cluster to security risks.
Key takeaways
- →Use CPU PSI to diagnose when tasks are waiting for CPU resources.
- →Access PSI metrics via the kubelet Summary API for detailed insights.
- →Ensure your environment meets prerequisites like cgroup v2 and a compatible kernel.
Why it matters
By utilizing CPU PSI, you can proactively identify and resolve resource contention issues, leading to improved application responsiveness and resource efficiency in your Kubernetes clusters.
Code examples
kubectl get --raw "/api/v1/nodes/<node-name>/proxy/stats/summary" \
| jq '{cores: (.node.cpu.usageNanoCores / 1e9), psi: .node.cpu.psi}'cat /proc/pressure/cpu
# expect: some and full lineskubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.8.0/components.yaml
# See the previous warning about the --kubelet-insecure-tls flag!When NOT to use this
The official docs don't call out specific anti-patterns here. Use your judgment based on your scale and requirements.
Want the complete reference?
Read official docsIndustry-standard certifications built by the people behind Linux and Kubernetes. Earn the CKA — the gold standard Kubernetes administrator cert. OpsCanary readers get 30% off year-round with code OPSCANARY3.
Get CKA certified →Kubernetes v1.38: Embracing cgroup v2 and Enhanced Pod Eviction
Kubernetes v1.38 is set to phase out cgroup v1 in favor of the more efficient cgroup v2. With the introduction of the EvictionRequest API, managing Pod evictions becomes more graceful and coordinated.
Tracing Kueue Admission: Unraveling Workload Mysteries with Headlamp
Understanding how Kueue admission works is crucial for optimizing resource allocation in Kubernetes. With the Kueue plugin for Headlamp, you can trace the admission path of a Workload, revealing insights into its resource requests and conditions. Dive in to learn how to leverage this tool effectively.
Navigating the Shift to cgroup v2 in Kubernetes
Kubernetes is evolving, and so is its resource management with the shift to cgroup v2. Understanding how cgroups manage resources is crucial, especially with features like Memory QoS and the new kubelet defaults. This article dives into what you need to know to leverage these changes effectively.
Get the daily digest
One email. 5 articles. Every morning.
No spam. Unsubscribe anytime.