{"id":510067,"date":"2022-11-15T04:48:07","date_gmt":"2022-11-15T01:48:07","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/should-you-set-kubernetes-cpu-limits\/"},"modified":"2022-11-15T04:48:07","modified_gmt":"2022-11-15T01:48:07","slug":"should-you-set-kubernetes-cpu-limits","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/should-you-set-kubernetes-cpu-limits\/","title":{"rendered":"#Should You Set Kubernetes CPU Limits?"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_89 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6af60cec58dd2\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6af60cec58dd2\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/should-you-set-kubernetes-cpu-limits\/#%E2%80%9CShould_You_Set_Kubernetes_CPU_Limits%E2%80%9D\" >&#8220;Should You Set Kubernetes CPU Limits?&#8221;<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/should-you-set-kubernetes-cpu-limits\/#How_Requests_and_Limits_Work\" >How Requests and Limits Work<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/should-you-set-kubernetes-cpu-limits\/#Why_CPU_Limits_Are_Dangerous\" >Why CPU Limits Are Dangerous<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/should-you-set-kubernetes-cpu-limits\/#No_Limit_Still_Prevents_Pod_Resource_Hogging\" >No Limit Still Prevents Pod Resource Hogging<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/should-you-set-kubernetes-cpu-limits\/#CPU_Requests_Still_Matter\" >CPU Requests Still Matter<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/should-you-set-kubernetes-cpu-limits\/#What_About_Memory\" >What About Memory?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/should-you-set-kubernetes-cpu-limits\/#Summary\" >Summary<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E2%80%9CShould_You_Set_Kubernetes_CPU_Limits%E2%80%9D\"><\/span>&#8220;Should You Set Kubernetes CPU Limits?&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<div>\n<!-- UNCACHED CONTENT --><br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-803403\" data-pagespeed-no-defer=\"\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/Kubernetes.jpg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Kubernetes logo\" width=\"1602\" height=\"902\"\/><\/p>\n<p>Managing the resources <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/concepts\/configuration\/manage-resources-containers\">available to your Pods and containers<\/a> is a best practice step for Kubernetes administration. You need to prevent Pods from greedily consuming your cluster\u2019s CPU and memory. Excess utilization by one set of Pods can cause resource contention that slows down neighboring containers and destabilizes your hosts.<\/p>\n<p>Kubernetes resource management is often misunderstood though. Two mechanisms are provided to control allocations: requests and limits. This leads to four possible settings per Pod, if you set a request and limit for both CPU and memory.<\/p>\n<p>Following this simple path is usually sub-optimal: CPU limits are best omitted because they harm performance and waste spare capacity. This article will explain the problem so you can run a more effective cluster.<\/p>\n<h2 id=\"how-requests-and-limits-work\"><span class=\"ez-toc-section\" id=\"How_Requests_and_Limits_Work\"><\/span>How Requests and Limits Work<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Requests are used for scheduling. New Pods will only be allocated to Nodes that can satisfy their requests. If there\u2019s no matching Node, the Pod will stick in the Pending state until resources become available.<\/p>\n<p>Limits define the maximum resource utilization the Pod is allowed. When the limit is reached, the Pod can\u2019t use any more of the resource, even if there\u2019s spare capacity on its Node. The actual effect of hitting the limit depends on the resource concerned: exceeding a CPU constraint results in throttling, while going beyond a memory limit will cause the Pod OOM killer to terminate container processes.<\/p>\n<p>In the following example, a Pod with these constraints will <strong>only<\/strong> schedule to Nodes that can provide 500m (equivalent to 0.5 CPU cores). Its maximum runtime consumption can be up to 1000m before throttling if the Node has capacity available.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"yaml\">\n<pre class=\"de1\"><strong class=\"co4\">resources<\/strong>:<strong class=\"co4\">\n  requests<\/strong>:<strong class=\"co3\">\n    cpu<\/strong><strong class=\"sy2\">: <\/strong>500m<strong class=\"co4\">\n  limits<\/strong>:<strong class=\"co3\">\n    cpu<\/strong><strong class=\"sy2\">: <\/strong>1000m<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h2 id=\"why-cpu-limits-are-dangerous\"><span class=\"ez-toc-section\" id=\"Why_CPU_Limits_Are_Dangerous\"><\/span>Why CPU Limits Are Dangerous<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To understand why CPU limits are problematic, consider what h<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>ens if a Pod with the resource settings shown above (500m request, 1000m limit) gets deployed to a quad-core Node with a total CPU capacity of 4000m. For simplicity\u2019s sake, there are no other Pods running on the Node.<\/p>\n<pre>$ kubectl get pods -o wide&#13;\nNAME            READY       STATUS      RESTARTS    AGE     IP              NODE&#13;\ndemo-pod        1\/1         Running     0           1m      10.244.0.185    quad-core-node<\/pre>\n<p>The Pod schedules onto the Node straightaway because the 500m request is im<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>tely satisfied. The Pod transitions into the Running state. Load could be low with CPU use around a few hundred millicores.<\/p>\n<p>Then there\u2019s a sudden traffic spike: requests are flooding in and the Pod\u2019s effective CPU utilization jumps right up to 2000m. Because of the CPU limit, this is throttled down to 1000m. The Node\u2019s not running any other Pods though, so it could provide the full 2000m, if the Pod wasn\u2019t being restricted by its limit.<\/p>\n<p>The Node\u2019s capacity has been wasted and the Pod\u2019s performance reduced unnecessarily. Omitting the CPU limit would let the Pod use the full 4000m, potentially fulfilling all the requests up to four times as quickly.<\/p>\n<h2 id=\"no-limit-still-prevents-pod-resource-hogging\"><span class=\"ez-toc-section\" id=\"No_Limit_Still_Prevents_Pod_Resource_Hogging\"><\/span>No Limit Still Prevents Pod Resource Hogging<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Omitting CPU limits doesn\u2019t compromise stability, provided you\u2019ve set appropriate requests on each Pod. When multiple Pods are deployed, each Pod\u2019s share of the CPU time gets scaled in proportion to its request.<\/p>\n<p>Here\u2019s an example of what happens to two Pods without limits when they\u2019re deployed to an 8-core (8000m) Node and each simultaneously requires 100% CPU consumption:<\/p>\n<table role=\"presentation\">\n<colgroup>\n<col style=\"width: 6%;\"\/>\n<col style=\"width: 15%;\"\/>\n<col style=\"width: 34%;\"\/>\n<col style=\"width: 43%;\"\/><\/colgroup>\n<thead\/>\n<tbody>\n<tr class=\"odd\">\n<td><strong>1<\/strong><\/td>\n<td>500m<\/td>\n<td>100%<\/td>\n<td>2000m<\/td>\n<\/tr>\n<tr class=\"even\">\n<td><strong>2<\/strong><\/td>\n<td>1500m<\/td>\n<td>100%<\/td>\n<td>6000m<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>If Pod 1\u2019s in a quieter period, then Pod 2 is free to use even more CPU cycles:<\/p>\n<table role=\"presentation\">\n<colgroup>\n<col style=\"width: 6%;\"\/>\n<col style=\"width: 15%;\"\/>\n<col style=\"width: 34%;\"\/>\n<col style=\"width: 43%;\"\/><\/colgroup>\n<thead\/>\n<tbody>\n<tr class=\"odd\">\n<td><strong>1<\/strong><\/td>\n<td>500m<\/td>\n<td>20%<\/td>\n<td>400m<\/td>\n<\/tr>\n<tr class=\"even\">\n<td><strong>2<\/strong><\/td>\n<td>1500m<\/td>\n<td>100%<\/td>\n<td>7600m<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"cpu-requests-still-matter\"><span class=\"ez-toc-section\" id=\"CPU_Requests_Still_Matter\"><\/span>CPU Requests Still Matter<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>These examples demonstrate why CPU requests matter. Setting appropriate requests prevents contention by ensuring Pods only schedule to Nodes that can support them. It also guarantees weighted distribution of the available CPU cycles when multiple Pods are experiencing increased demand.<\/p>\n<p>CPU limits don\u2019t offer these benefits. They\u2019re only valuable in situations when you want to throttle a Pod above a certain performance threshold. This is almost always undesirable behavior; you\u2019re asserting that your other Pods will always be busy, when they could be idling and creating spare CPU cycles in the cluster.<\/p>\n<p>Not setting limits allows those cycles to be utilized by any workload that needs them. This results in better overall performance because available hardware\u2019s never wasted.<\/p>\n<h2 id=\"what-about-memory\"><span class=\"ez-toc-section\" id=\"What_About_Memory\"><\/span>What About Memory?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Memory is managed in Kubernetes using the same request and limit concepts. However memory is a physically different resource to CPU utilization which demands its own allocation method. Memory is non-compressible: it can\u2019t be revoked once allocated to a container process. Processes share the CPU as it becomes available but they\u2019re given individual portions of memory.<\/p>\n<p>Setting an identical request and limit is the best practice approach for Kubernetes memory management. This allows you to reliably anticipate the total memory consumption of all the Pods in your cluster.<\/p>\n<p>It might seem logical to set a relatively low request with a much higher limit. However using this technique for many Pods can have a destabilizing effect: if several Pods reach above their requests, your cluster\u2019s memory capacity <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/home.robusta.dev\/blog\/kubernetes-memory-limit\">could be exhausted<\/a>. The OOM killer will intervene to terminate container processes, potentially causing disruption to your workloads. Any of your Pods could be targeted for eviction, not just the one that caused the memory to be exhausted.<\/p>\n<p>Using equal requests and limits prevents a Pod from scheduling unless the Node can provide the memory it requires. It also enforces that the Pod can\u2019t use any more memory than its explicit allocation, eliminating the risk of over-utilization when multiple Pods exceed their requests. Over-utilization will become apparent when you try to schedule a Pod and no Node can satisfy the memory request. The error occurs earlier and more predictably, without impacting any other Pods.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Kubernetes allows you to distinguish between the quantity of resources that a container <em>requires<\/em>, and an upper bound that it\u2019s <em>allowed<\/em> to scale up to but cannot exceed. However this mechanism is less useful in practice than it might seem at first glance.<\/p>\n<p>Setting CPU limits prevents your processes from utilizing spare CPU capacity as it becomes available. This unnecessarily throttles performance when a Pod could be temporarily using cycles that no neighbor requires.<\/p>\n<p>Use a sensible CPU request to prevent Pods scheduling onto Nodes that are already too busy to provide good performance. Leave the limit field unset so Pods can access additional resources when performing demanding tasks at times when capacity is available. Finally, assign each Pod a memory request and limit, making sure to use the same value for both fields. This will prevent memory exhaustion, creating a more stable and predictable cluster environment.<\/p>\n<\/div>\n<p><script>\n setTimeout(function(){\n  !function(f,b,e,v,n,t,s)\n  {if(f.fbq)return;n=f.fbq=function(){n.callMethod?\n  n.callMethod.apply(n,arguments):n.queue.push(arguments)};\n  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';\n  n.queue=[];t=b.createElement(e);t.async=!0;\n  t.src=v;s=b.getElementsByTagName(e)[0];\n  s.parentNode.insertBefore(t,s) } (window, document,'script',\n  'https:\/\/connect.facebook.net\/en_US\/fbevents.js');\n   fbq('init', '335401813750447');\n   fbq('track', 'PageView');\n  },3000);\n<\/script><\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.howtogeek.com\/devops\/should-you-set-kubernetes-cpu-limits\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;Should You Set Kubernetes CPU Limits?&#8221; Managing the resources available to your Pods and containers is a best practice step for Kubernetes administration. You need to prevent Pods from greedily consuming your cluster\u2019s CPU and memory. Excess utilization by one set of Pods can cause resource contention that slows down neighboring containers and destabilizes your&#8230;<\/p>\n","protected":false},"author":1,"featured_media":510068,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/Kubernetes.jpg?height=200p&trim=2,2,2,2","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-510067","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/510067","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=510067"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/510067\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/510068"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=510067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=510067"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=510067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}