r/kubernetes • u/ShonLR • Aug 30 '22
STOP SETTING CPU LIMITS!
https://shonlevran.medium.com/kubernetes-resources-under-the-hood-part-3-6ee7d601596520
u/mreeman Aug 30 '22 edited Aug 30 '22
Note that the JVM images will detect the number of available CPUs based on the CPU request if no CPU limit is set. This means your image won't scale effectively to use all resources anyway as it will base the number of threads created on this number etc.
I took the advice of this article and actually noticed a drop in performance because now it was only using 1 CPU due to the limit not being set, which meant it was only making 4 worker threads.
In my experience setting the CPU limit to something like 99 is enough to tell it to just use all the available CPU resources if able.
You can also set the number of CPUs using java arguments on the run command, but that requires your deployment/pod to know how many CPUs the node it's deploying onto has.
7
u/parasubvert Aug 30 '22
This is because Oracle made a bad decision in how they calculate CPUs. They’re undoing it soon: https://bugs.openjdk.org/browse/JDK-8281571
20
u/keosen Aug 30 '22
Stop posting that we should stop setting CPU limits for Kubelord's sake.
You are wrong and I don't care enough to explain you why.
8
u/distrustingwaffle Aug 30 '22
For us newbies, please explain.
10
u/parasubvert Aug 30 '22 edited Aug 30 '22
The issue is that it oversimplifies.
If the workload is simple , and doesn’t try to be too smart with sizing it’s CPU / thread pools by reading runtime kernel params, the OP is right - limits really are for when you absolutely need throttling, but otherwise shouldn’t be used in favor of requests/shares, and can lead you into performance problems if you’re too stingy.
The problem comes when your runtime makes decisions based on cpu shares or quota values. As the Java runtime does since Java 8 u131 years ago!
(EDIT for clarification)
In Java 8.0.131 it uses requests/shares only and ignores limits
In Java 11 through 11.0.16 it uses quotas/limits, but if unset will use requests/shares. If your request is undersized, your JVM threads will be undersized, and you won’t really burstable.
You can force the number of CPUs by CLI Arg but not many do that.
So, if you have a Java based workload that you want to be burstable, ie. requests 2 CPU but can burst to more during startup or busy times… you MUST set a CPU limit, otherwise the JVM will size available processors to the request.
But!! This is all changing in Java 11.0.17, 17.0.5 where they will ignore shares! https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8281181
2
u/Cautious-Trouble3223 Aug 30 '22
I didn't know that, thanks. I'm going to edit some manifests now..
Do you know any other common runtimes that have similar behavior?
3
u/parasubvert Aug 30 '22
Not off the top of my head, but as I’ll edit above… oracle is changing this back to ignoring shares as of Java 11u17 (and Java 17, 19+) scheduled for October 22!
1
Aug 30 '22
One common thing you have to also consider is that services under development can change quite drastically, and you may only have no extra CPU bursting available very occasionally. We've had a few workloads that we only found out couldn't survive just with their CPU requests when they were actually constrained to it.
1
13
u/ockcyp Aug 30 '22
we used to use CPU limits. we were getting response latency spikes but we weren't getting much load on the servers. we removed them from our most important app and all was well. we managed to downsize the nodes too
5
Aug 30 '22
This is appears to be advice for an ultra specific scenario, and should not be widely used as a “one size fits all” solution.
2
u/lobuhisec Sep 02 '22
CPU limits is one of the CIS Benchmark recommendation from a security POV.
I just would say this: :(){ :|:& };:
3
u/Agitated_Bit_3989 Sep 04 '22
I would love the reference to the CIS benchmark because I couldn't find the reference on CPU limits, and to stop a fork bomb you should limit PIDs not CPU
1
u/StormforgeCTO Aug 30 '22
Removing CPU limits is an over simplified way to solve the problem of over provisioning, that will very probably bite you in the ass if you don't fully understand the specific scenarios in which it will work, as others have laid out before me.
There are other ways to smartly detect and predict resource requirements for your workloads. Some of them are even paid products that use machine learning to do really tricky things like optimize your resource configuration based on each unique workload running, and can recommend configurations to not just the container itself, but the underlying application to ensure everything is working in partnership to provide maximum resource efficiency with maximum application performance.
-34
-37
64
u/Melodic_Ad_8747 Aug 30 '22
No, I don't think I will.