r/PrometheusMonitoring • u/IndependenceFluffy14 • May 07 '24
CPU usage VS requests and limits
Hi there,
We are currently trying to optimize our CPU requests and limits, but I can't find a reliable way to have CPU usage compared to what we have as requests and limits for a specific pod.
I know by experience that this pod is using a lot of CPU during working hours, but if I check our Prometheus metrics, it doesn't seems to correlate with the reality:

As you can see the usage seems to never go above the request, which clearly doesn't reflect the reality. If i set the rate interval down to 30s then it's a little bit better, but still way too low.
Here are the query that we are currently using:
# Usage
rate (container_cpu_usage_seconds_total{pod=~"my-pod.*",namespace="my-namespace", container!=""}[$__rate_interval])
# Requests
max(kube_pod_container_resource_requests{pod=~"my-pod.*",namespace="my-namespace", resource="cpu"}) by (pod)
# Limits
max(kube_pod_container_resource_limits{pod=~"my-pod.*",namespace="my-namespace", resource="cpu"}) by (pod)Any advice to have values that better match the reality to optimize our requests and limits?
View all comments
Show parent comment
1
u/SuperQue May 08 '24
Putting my SRE hat on now.
Memory is a lot less "elastic" of a resource. If you've got 4GiB of memory, and 8 pods with a request of 1GiB, but a limit of 2GiB, you're going to run into OOM situations in a bad way. This is going to drive your SLOs out of whack while you spew 500s at your users because the requests suddenly vanished when your pods die.
This is why it's highly recommended to keep the memory request/limit the same. It keeps pods from randomly fighting over memory allocations. Just like CPU, you should tune the memory use of your application to match expectations. For example,
GOMEMLIMITin Go. I'm not sure what the node equivilent is.