{"id":193988,"date":"2021-03-04T17:00:48","date_gmt":"2021-03-04T14:00:48","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-set-kubernetes-pod-resource-limits-cloudsavvy-it\/"},"modified":"2021-03-04T17:00:48","modified_gmt":"2021-03-04T14:00:48","slug":"how-to-set-kubernetes-pod-resource-limits-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-set-kubernetes-pod-resource-limits-cloudsavvy-it\/","title":{"rendered":"#How to Set Kubernetes Pod Resource Limits \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 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-6a28ff8f28c9b\" 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-6a28ff8f28c9b\" 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-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-set-kubernetes-pod-resource-limits-cloudsavvy-it\/#Resource_Units\" >Resource Units<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-set-kubernetes-pod-resource-limits-cloudsavvy-it\/#Creating_a_CPU_Limit\" >Creating a CPU Limit<\/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\/how-to-set-kubernetes-pod-resource-limits-cloudsavvy-it\/#Creating_a_Memory_Limit\" >Creating a Memory Limit<\/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\/how-to-set-kubernetes-pod-resource-limits-cloudsavvy-it\/#Storage_Limits\" >Storage Limits<\/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\/how-to-set-kubernetes-pod-resource-limits-cloudsavvy-it\/#Resource_Requests\" >Resource Requests<\/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\/how-to-set-kubernetes-pod-resource-limits-cloudsavvy-it\/#Using_Requests_and_Limits\" >Using Requests and Limits<\/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\/how-to-set-kubernetes-pod-resource-limits-cloudsavvy-it\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Set Kubernetes Pod Resource Limits \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9632\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/06268c7060f33c17d0b8db5f1f90faf9\/p\/uploads\/2021\/02\/748108a6.jpg\" alt=\"Graphic showing the Kubernetes logo\" width=\"1602\" height=\"902\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Setting resource limits on your Kubernetes pods prevents an errant container from impacting other workloads. Kubernetes lets you cap resources, including CPU and memory consumption. Pods can be terminated when their limits are exceeded, maintaining the overall stability of the cluster.<\/p>\n<h2 id=\"resource-units\"><span class=\"ez-toc-section\" id=\"Resource_Units\"><\/span>Resource Units<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Before <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/concepts\/configuration\/manage-resources-containers\">defining limits<\/a>, it\u2019s worth noting how Kubernetes expresses resource availability.<\/p>\n<p>CPU consumption is measured in terms of vCPUs used. A limit of <code>0.5<\/code> vCPUs indicates that the pod can consume half of the available time of one of the available vCPUs. A vCPU is what you\u2019ll see advertised on cloud providers\u2019 hosting pages. When using bare-metal hardware, it\u2019s one hyperthread on your processor.<\/p>\n<p>Memory is measured in bytes. You can specify it as an integer number of bytes, or as a friendlier quantity, such as <code>512Mi<\/code> or <code>1Gi<\/code>.<\/p>\n<h2 id=\"creating-a-cpu-limit\"><span class=\"ez-toc-section\" id=\"Creating_a_CPU_Limit\"><\/span>Creating a CPU Limit<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To add a CPU limit to pod containers, include the <code>resources:limits<\/code> field in your container\u2019s manifest:<\/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\"><span class=\"co3\">apiVersion<\/span><span class=\"sy2\">: <\/span>v1<span class=\"co3\">\nkind<\/span><span class=\"sy2\">: <\/span>Pod<span class=\"co4\">\nmetadata<\/span>:<span class=\"co3\">\n  name<\/span><span class=\"sy2\">: <\/span>demo<span class=\"co3\">\n  namespace<\/span><span class=\"sy2\">: <\/span>demo<span class=\"co4\">\nspec<\/span>:<span class=\"co4\">\n  containers<\/span>:<span class=\"co3\">\n    - name<\/span><span class=\"sy2\">: <\/span>my-container<span class=\"co3\">\n      image<\/span><span class=\"sy2\">: <\/span>example\/example<span class=\"co4\">\n      resources<\/span>:<span class=\"co4\">\n        limits<\/span>:<span class=\"co3\">\n          cpu<\/span><span class=\"sy2\">: <\/span><span class=\"st0\">\"0.5\"<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The example above will limit your containers to 0.5 vCPUs. They\u2019ll be throttled so that they cannot consume more than half of the available CPU time within a 100ms period.<\/p>\n<h2 id=\"creating-a-memory-limit\"><span class=\"ez-toc-section\" id=\"Creating_a_Memory_Limit\"><\/span>Creating a Memory Limit<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Memory limits are created in a similar way. Change the <code>limits:cpu<\/code> field in the manifest to <code>limits:memory<\/code>:<\/p>\n<p>The container will be limited to 512Mi of RAM. Kubernetes will still allow it to access more if the node it\u2019s scheduled on has excess capacity. Otherwise, exceeding the limit will result in the container being marked as a candidate for termination.<\/p>\n<h2 id=\"storage-limits\"><span class=\"ez-toc-section\" id=\"Storage_Limits\"><\/span>Storage Limits<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>All Kubernetes nodes have an amount of ephemeral storage available. This storage is used by pods to store caches and logs. The ephemeral storage pool is also where the Kubernetes cluster keeps container images.<\/p>\n<p>You can set up limits for a pod\u2019s ephemeral storage use. This is a beta feature intended to ensure that a single pod\u2019s cache can\u2019t consume the entire storage pool. Use the <code>limits:ephemeral-storage<\/code> container manifest field:<\/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\"><span class=\"co4\">limits<\/span>:<span class=\"co3\">\n  ephemeral-storage<\/span><span class=\"sy2\">: <\/span><span class=\"st0\">\"1Gi\"<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>This container would now be limited to using 1Gi of the available ephemeral storage. Pods that try to use more storage will be evicted. When there are multiple containers in a pod, the pod is evicted if the sum of the storage usage from all of the containers exceeds the overall storage limit.<\/p>\n<p>Kubernetes usually tracks storage use by periodically scanning the node\u2019s ephemeral storage filesystem. It\u2019ll then sum the storage use of each pod and container. There is optional support for OS-level filesystem storage quotas, which enable more accurate monitoring.<\/p>\n<p>You\u2019ll need a project quota-supported filesystem such as XFS or ext4. Make sure that the filesystem is mounted with project quota tracking enabled, then enable the <code>LocalStorageCapacityIsolationFSQuotaMonitoring<\/code> feature flag in <code>kubelet<\/code>. Guidance on <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/concepts\/configuration\/manage-resources-containers\">configuring this system<\/a> is provided in the Kubernetes documentation.<\/p>\n<h2 id=\"resource-requests\"><span class=\"ez-toc-section\" id=\"Resource_Requests\"><\/span>Resource Requests<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In addition to resource limits, you can set resource <em>requests<\/em>. These are available for CPU, memory and ephemeral storage\u2014change the <code>limits<\/code> field to <code>requests<\/code> in each of the examples shown above.<\/p>\n<p>Setting a resource request indicates the amount of that resource that you expect the container will use. Kubernetes takes this information into account when determining which node to schedule the pod to.<\/p>\n<p>Using memory as an example, a <code>request<\/code> of <code>512Mi<\/code> will result in the pod getting scheduled to a node with at least 512Mi of memory available. The availability is calculated by summing the memory requests of all the existing pods on the node and subtracting that from the node\u2019s total memory capacity.<\/p>\n<p>A node will be ineligible to host a new container if the sum of the workload requests, including the new container\u2019s request, exceeds the available capacity. This remains the case even if the real-time memory use is actually very low. The available capacity has already been allocated to the existing containers to ensure that their requests can be satisfied.<\/p>\n<p>Unlike a limit, Kubernetes always allows containers to exceed their resource request. They can consume any unused resource quantities that other containers have requested but are not currently using.<\/p>\n<h2 id=\"using-requests-and-limits\"><span class=\"ez-toc-section\" id=\"Using_Requests_and_Limits\"><\/span>Using Requests and Limits<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The differing behaviors of requests and limits mean that you should carefully consider the values that you use. It\u2019s usually best to keep requests low. You then set the limits as high as possible without affecting your workloads\u2019 ability to coexist.<\/p>\n<p>Using a low resource request value gives your pods the best chance of getting scheduled to a node. The scheduler has more flexibility when making allocation decisions, as it\u2019s more likely that any given node will be able to host the container. The container will be provided with ready access to any excess resources it needs, beyond the request, up to the specified limit.<\/p>\n<p>Each request and limit needs to be balanced in order to achieve the greatest effect. You should look at the requests and limits of the other pods running in your cluster. Make sure that you\u2019re aware of the total resource quantities provided by your nodes so that you don\u2019t set limits that are either too high (risking stability) or too low (a waste of capacity).<\/p>\n<h2 id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You should always set up resource limits for your Kubernetes workloads. Effective use of limits helps workloads peacefully coexist without risking the health of your cluster.<\/p>\n<p>This is particularly important in the case of memory. Without limits, a container with an errant process can quickly consume all the memory offered by its node. Such an out-of-memory scenario could take down other pods scheduled to that node, as the OS-level memory manager would begin killing processes to reduce the memory use.<\/p>\n<p>Setting a memory limit allows Kubernetes to terminate the container before it starts impacting other workloads in the cluster, let alone external processes. You lose your workload, but the overall cluster gains greater stability.\n<\/p><\/div>\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.cloudsavvyit.com\/9636\/how-to-set-kubernetes-pod-resource-limits\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Set Kubernetes Pod Resource Limits \u2013 CloudSavvy IT&#8221; Setting resource limits on your Kubernetes pods prevents an errant container from impacting other workloads. Kubernetes lets you cap resources, including CPU and memory consumption. Pods can be terminated when their limits are exceeded, maintaining the overall stability of the cluster. Resource Units Before defining&#8230;<\/p>\n","protected":false},"author":1,"featured_media":193989,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/06268c7060f33c17d0b8db5f1f90faf9\/p\/uploads\/2021\/02\/748108a6.jpg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-193988","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\/193988","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=193988"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/193988\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/193989"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=193988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=193988"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=193988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}