r/kubernetes • • Aug 30 '22

STOP SETTING CPU LIMITS!

https://shonlevran.medium.com/kubernetes-resources-under-the-hood-part-3-6ee7d6015965
9 Upvotes

32 comments sorted by

64

u/Melodic_Ad_8747 Aug 30 '22

No, I don't think I will.

-17

u/ShonLR Aug 30 '22

You only hurt your own workloads, read this blog and understand why.

-6

u/Mister_Ect Aug 30 '22

No, I think he'd rather have tasks run slower than utilize empty CPU space that he's paying/paid for?

-3

u/ShonLR Aug 30 '22

WORD.

0

u/[deleted] Aug 30 '22

[deleted]

6

u/Agitated_Bit_3989 Aug 30 '22

Consistent in the terms of not being able to use idle resources, the requests handle how much resources the pods are guaranteed to receive which is also consistent. Not setting CPU limits just allows your workloads to use more if required and available without putting other workloads at risk

0

u/[deleted] Aug 30 '22

[deleted]

1

u/ShonLR Aug 30 '22

The OP says specifically I should leave

That is absolutely not what I've said, but if you love wasted resources times 3000 go ahead...

0

u/[deleted] Aug 30 '22

[deleted]

-2

u/ShonLR Aug 30 '22

If you are one of those who prefer worse but consistent performance over optimal performance that's indeed the right approach, and many business requirements justify that. The purpose of this post is to make it clear that by setting CPU limits you hurt your performance on purpose and do not gain any "noisy neighbors" or "eviction" protection by that. Most of the cluster operators don't understand that.

1

u/elfenars Aug 30 '22 edited Aug 31 '22

So you know most cluster operators? Cause let me tell ya, the ones I know do know what it is for and how it works and most of them use it when necessary.

On the contrary, most newly trained ops are the ones making absolutist claims like "don't use cpu limits"

3

u/craig91 Aug 30 '22

Opposite experience for me. Even Tim Hockin recommends not setting CPU limits, last I checked he knew a thing or two about Kubernetes lol.

Setting CPU limits doesn't on its own guarantee predictable results because it depends what you set them to. If I set my CPU request to 1 core and limit to 10, anything over 1 is not going to be guaranteed. The only way you're guaranteeing predictable results is by setting them to the same value. But that also means you'll have a ton of underutilized nodes because the request value will need to be higher, so you will pay $$$ for the privilege of always reserving the exact amount of CPU for every workloads worst case scenario. So imo if someone is setting CPU limits higher than the request and arguing they set CPU limits for predictable performance you might as well not set them at all. If you're willing to pay extra to always guarantee that worst case scenario then go ahead and set CPU limits but set them equal to the request.

Since a workload is guaranteed it's request value, as long as that it's understood that is the WORST case if you omit CPU limits you can take advantage of unutilized resources on the node rather than degrading the performance of an app simply for predictable (worse) performance.

When removing limits it would be a good idea to have monitoring on CPU though. Cluster level alerts for node CPU utilization. Also if there's a workload consistently using significantly more CPU than its request I want to know, or if it's a tenant workload let them know. I'd rather let those workloads use the unused CPU than throttle the app even though 75% of the nodes CPU is free. Instead generate an alert if it's a suspected problem so we can see whether the CPU request should be raised, or investigate whether something is wrong that's causing increased usage.

This isn't a perfect analogy but I'll post it anyway. Think about a highway with a single car driving, there are many lanes free, the road ahead is clear. I can drive at my speed limit and get to my destination at a specific time, but if I drive twice the speed limit I can get there much earlier. The highway is empty so it's easy to do, and by using that unused space on the road I've gotten to my destination twice as fast. But next time I make the trip, there are a few others sharing the road so I can't quite go double... but I can still go over the posted limit to get there quicker. Now kubernetes is only going to allow a certain number of cars on the highway before it starts sending cars to other highways so everyone can drive at their own personalized speed limit safely. As more cars come on my highway I'll have to slow down. But at least I know the worst case I'll always be able to drive my personalized speed limit. However when theres lots of room on the road I might as well take advantage of it and get to my destination faster. My first trip I arrive in 30 minutes, second trip 45 minutes and third trip 60 minutes. The alternative is everyone on the road drives their personalized speed limit and never more, even if the road is empty (CPU limit enforced), in this scenario I arrive in 60 minutes for all three trips despite road conditions being identical. Now you could make the above more complicated by having a variable speed limit with CPU request and CPU limit if you're setting CPU limits higher but the idea is the same.

1

u/elfenars Aug 30 '22

I'm gonna need an exact quote and source of that.

→ More replies (0)

0

u/Agitated_Bit_3989 Aug 30 '22

Sure if that's what you need, but keep in mind that you may have wasted CPU that could have been utilized if you didn't set CPU limits

20

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!

https://bugs.openjdk.org/browse/JDK-8281571

1

u/[deleted] 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

u/distrustingwaffle Aug 30 '22

Thanks, that’s quite clear

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

u/[deleted] 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: :(){ :|:& };:

https://www.cyberciti.biz/faq/understanding-bash-fork-bomb/

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

u/SLR-d Aug 30 '22

Thank you so much

-37

u/Shot-Cake5276 Aug 30 '22

WOW, Amazing read!