{"id":4081,"date":"2025-02-24T16:02:28","date_gmt":"2025-02-24T16:02:28","guid":{"rendered":"https:\/\/code2deploy.com\/blog\/?p=4081"},"modified":"2025-02-21T16:04:28","modified_gmt":"2025-02-21T16:04:28","slug":"enforcing-resource-limits-at-the-namespace-level-in-kubernetes","status":"publish","type":"post","link":"https:\/\/code2deploy.com\/blog\/enforcing-resource-limits-at-the-namespace-level-in-kubernetes\/","title":{"rendered":"Enforcing Resource Limits at the Namespace Level in Kubernetes"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In multi-tenant Kubernetes clusters, enforcing resource limits at the namespace level is crucial to prevent excessive resource consumption by certain workloads. This ensures fair resource distribution, avoids overloading nodes, and enhances cluster stability.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this blog, we will cover:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <strong>ResourceQuota<\/strong> concept.<\/li>\n\n\n\n<li>How to apply a <strong>ResourceQuota<\/strong> to enforce CPU and memory limits.<\/li>\n\n\n\n<li>A <strong>real-world use case<\/strong> demonstrating resource control in a shared environment.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding ResourceQuota<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>ResourceQuota<\/code> restricts the total resource consumption within a namespace. It applies to CPU, memory, storage, and even object counts (e.g., pods, services, PVCs).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example: Enforcing CPU and Memory Limits<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To ensure that no pod in <code>my-namespace<\/code> exceeds 4 CPUs and 8Gi of RAM, create a <code>ResourceQuota<\/code> as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apiVersion: v1\nkind: ResourceQuota\nmetadata:\n  name: namespace-quota\n  namespace: my-namespace\nspec:\n  hard:\n    requests.cpu: \"2\"\n    requests.memory: \"4Gi\"\n    limits.cpu: \"4\"\n    limits.memory: \"8Gi\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Breaking Down the Configuration:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>requests.cpu &amp; requests.memory<\/strong>: Ensures pods collectively request no more than 2 CPUs and 4Gi memory.<\/li>\n\n\n\n<li><strong>limits.cpu &amp; limits.memory<\/strong>: Prevents the namespace from using more than 4 CPUs and 8Gi memory.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Apply this quota to the cluster:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl apply -f resource-quota.yaml<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Verify the applied quota:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl get resourcequota -n my-namespace<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Real-World Scenario: Multi-Tenant Cluster Resource Management<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Problem:<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A company runs multiple applications in a shared Kubernetes cluster, where different teams deploy workloads in separate namespaces. One team accidentally deploys an unoptimized pod that consumes excessive CPU and memory, causing performance degradation for others.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Solution:<\/strong><\/h3>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>Define Resource Quotas for Each Namespace<\/strong><code>apiVersion: v1 kind: ResourceQuota metadata: name: team-a-quota namespace: team-a spec: hard: requests.cpu: \"1\" requests.memory: \"2Gi\" limits.cpu: \"2\" limits.memory: \"4Gi\"<\/code><\/li>\n\n\n\n<li><strong>Apply the Quota for Each Team&#8217;s Namespace<\/strong><code>kubectl apply -f team-a-quota.yaml kubectl apply -f team-b-quota.yaml<\/code><\/li>\n\n\n\n<li><strong>Monitor Resource Consumption<\/strong><code>kubectl describe resourcequota -n team-a<\/code>This command shows the current usage against the set quota.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Namespace Resource Management<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Set realistic quotas<\/strong> based on historical usage and application needs.<\/li>\n\n\n\n<li><strong>Use LimitRanges<\/strong> to enforce per-pod constraints within a namespace.<\/li>\n\n\n\n<li><strong>Monitor resource usage regularly<\/strong> with Kubernetes metrics (<code>kubectl top pods<\/code> and <code>kubectl top nodes<\/code>).<\/li>\n\n\n\n<li><strong>Adjust quotas dynamically<\/strong> based on scaling needs.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>ResourceQuota<\/code> provides an efficient way to manage resource allocation in a Kubernetes namespace. By enforcing CPU and memory limits, organizations can ensure fair resource distribution and prevent accidental overuse, keeping the cluster stable and performant.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Would you like to explore per-pod resource limits using <code>LimitRange<\/code>? Let us know in the comments!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In multi-tenant Kubernetes clusters, enforcing resource limits at the namespace level is crucial to prevent excessive resource consumption by certain workloads. This ensures fair resource distribution, avoids overloading nodes, and enhances cluster stability. In this blog, we will cover: Understanding ResourceQuota A ResourceQuota restricts the total resource consumption within a namespace. It applies to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,216],"tags":[11,540,539,471],"class_list":["post-4081","post","type-post","status-publish","format-standard","hentry","category-kubernetes","category-kubernetes-basic","tag-kubernetes-deployment","tag-namespace","tag-resource-limits-at-the-namespace-level","tag-resource-quotas"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Enforcing Resource Limits at the Namespace Level in Kubernetes - code2deploy.com<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code2deploy.com\/blog\/enforcing-resource-limits-at-the-namespace-level-in-kubernetes\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Enforcing Resource Limits at the Namespace Level in Kubernetes - code2deploy.com\" \/>\n<meta property=\"og:description\" content=\"Introduction In multi-tenant Kubernetes clusters, enforcing resource limits at the namespace level is crucial to prevent excessive resource consumption by certain workloads. This ensures fair resource distribution, avoids overloading nodes, and enhances cluster stability. In this blog, we will cover: Understanding ResourceQuota A ResourceQuota restricts the total resource consumption within a namespace. It applies to [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code2deploy.com\/blog\/enforcing-resource-limits-at-the-namespace-level-in-kubernetes\/\" \/>\n<meta property=\"og:site_name\" content=\"code2deploy.com\" \/>\n<meta property=\"article:published_time\" content=\"2025-02-24T16:02:28+00:00\" \/>\n<meta name=\"author\" content=\"enam\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"enam\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/enforcing-resource-limits-at-the-namespace-level-in-kubernetes\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/enforcing-resource-limits-at-the-namespace-level-in-kubernetes\\\/\"},\"author\":{\"name\":\"enam\",\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/#\\\/schema\\\/person\\\/e46930c19b999a87f12566fa8357481b\"},\"headline\":\"Enforcing Resource Limits at the Namespace Level in Kubernetes\",\"datePublished\":\"2025-02-24T16:02:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/enforcing-resource-limits-at-the-namespace-level-in-kubernetes\\\/\"},\"wordCount\":327,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/#\\\/schema\\\/person\\\/e46930c19b999a87f12566fa8357481b\"},\"keywords\":[\"Kubernetes deployment\",\"Namespace\",\"Resource Limits at the Namespace Level\",\"Resource Quotas\"],\"articleSection\":[\"Kubernetes\",\"Kubernetes Basic\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/code2deploy.com\\\/blog\\\/enforcing-resource-limits-at-the-namespace-level-in-kubernetes\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/enforcing-resource-limits-at-the-namespace-level-in-kubernetes\\\/\",\"url\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/enforcing-resource-limits-at-the-namespace-level-in-kubernetes\\\/\",\"name\":\"Enforcing Resource Limits at the Namespace Level in Kubernetes - code2deploy.com\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/#website\"},\"datePublished\":\"2025-02-24T16:02:28+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/enforcing-resource-limits-at-the-namespace-level-in-kubernetes\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/code2deploy.com\\\/blog\\\/enforcing-resource-limits-at-the-namespace-level-in-kubernetes\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/enforcing-resource-limits-at-the-namespace-level-in-kubernetes\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Enforcing Resource Limits at the Namespace Level in Kubernetes\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/\",\"name\":\"code2deploy.com\\\/blog\",\"description\":\"TechOps\",\"publisher\":{\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/#\\\/schema\\\/person\\\/e46930c19b999a87f12566fa8357481b\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/#\\\/schema\\\/person\\\/e46930c19b999a87f12566fa8357481b\",\"name\":\"enam\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d864e2f082f4499f8f1b33f004ec166eea77b9e94738553b120b6dca2410f203?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d864e2f082f4499f8f1b33f004ec166eea77b9e94738553b120b6dca2410f203?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d864e2f082f4499f8f1b33f004ec166eea77b9e94738553b120b6dca2410f203?s=96&d=mm&r=g\",\"caption\":\"enam\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d864e2f082f4499f8f1b33f004ec166eea77b9e94738553b120b6dca2410f203?s=96&d=mm&r=g\"},\"sameAs\":[\"https:\\\/\\\/code2deploy.com\\\/blog\"],\"url\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/author\\\/enam\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Enforcing Resource Limits at the Namespace Level in Kubernetes - code2deploy.com","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code2deploy.com\/blog\/enforcing-resource-limits-at-the-namespace-level-in-kubernetes\/","og_locale":"en_US","og_type":"article","og_title":"Enforcing Resource Limits at the Namespace Level in Kubernetes - code2deploy.com","og_description":"Introduction In multi-tenant Kubernetes clusters, enforcing resource limits at the namespace level is crucial to prevent excessive resource consumption by certain workloads. This ensures fair resource distribution, avoids overloading nodes, and enhances cluster stability. In this blog, we will cover: Understanding ResourceQuota A ResourceQuota restricts the total resource consumption within a namespace. It applies to [&hellip;]","og_url":"https:\/\/code2deploy.com\/blog\/enforcing-resource-limits-at-the-namespace-level-in-kubernetes\/","og_site_name":"code2deploy.com","article_published_time":"2025-02-24T16:02:28+00:00","author":"enam","twitter_card":"summary_large_image","twitter_misc":{"Written by":"enam","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/code2deploy.com\/blog\/enforcing-resource-limits-at-the-namespace-level-in-kubernetes\/#article","isPartOf":{"@id":"https:\/\/code2deploy.com\/blog\/enforcing-resource-limits-at-the-namespace-level-in-kubernetes\/"},"author":{"name":"enam","@id":"https:\/\/code2deploy.com\/blog\/#\/schema\/person\/e46930c19b999a87f12566fa8357481b"},"headline":"Enforcing Resource Limits at the Namespace Level in Kubernetes","datePublished":"2025-02-24T16:02:28+00:00","mainEntityOfPage":{"@id":"https:\/\/code2deploy.com\/blog\/enforcing-resource-limits-at-the-namespace-level-in-kubernetes\/"},"wordCount":327,"commentCount":0,"publisher":{"@id":"https:\/\/code2deploy.com\/blog\/#\/schema\/person\/e46930c19b999a87f12566fa8357481b"},"keywords":["Kubernetes deployment","Namespace","Resource Limits at the Namespace Level","Resource Quotas"],"articleSection":["Kubernetes","Kubernetes Basic"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code2deploy.com\/blog\/enforcing-resource-limits-at-the-namespace-level-in-kubernetes\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code2deploy.com\/blog\/enforcing-resource-limits-at-the-namespace-level-in-kubernetes\/","url":"https:\/\/code2deploy.com\/blog\/enforcing-resource-limits-at-the-namespace-level-in-kubernetes\/","name":"Enforcing Resource Limits at the Namespace Level in Kubernetes - code2deploy.com","isPartOf":{"@id":"https:\/\/code2deploy.com\/blog\/#website"},"datePublished":"2025-02-24T16:02:28+00:00","breadcrumb":{"@id":"https:\/\/code2deploy.com\/blog\/enforcing-resource-limits-at-the-namespace-level-in-kubernetes\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code2deploy.com\/blog\/enforcing-resource-limits-at-the-namespace-level-in-kubernetes\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/code2deploy.com\/blog\/enforcing-resource-limits-at-the-namespace-level-in-kubernetes\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code2deploy.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Enforcing Resource Limits at the Namespace Level in Kubernetes"}]},{"@type":"WebSite","@id":"https:\/\/code2deploy.com\/blog\/#website","url":"https:\/\/code2deploy.com\/blog\/","name":"code2deploy.com\/blog","description":"TechOps","publisher":{"@id":"https:\/\/code2deploy.com\/blog\/#\/schema\/person\/e46930c19b999a87f12566fa8357481b"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code2deploy.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/code2deploy.com\/blog\/#\/schema\/person\/e46930c19b999a87f12566fa8357481b","name":"enam","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d864e2f082f4499f8f1b33f004ec166eea77b9e94738553b120b6dca2410f203?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d864e2f082f4499f8f1b33f004ec166eea77b9e94738553b120b6dca2410f203?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d864e2f082f4499f8f1b33f004ec166eea77b9e94738553b120b6dca2410f203?s=96&d=mm&r=g","caption":"enam"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/d864e2f082f4499f8f1b33f004ec166eea77b9e94738553b120b6dca2410f203?s=96&d=mm&r=g"},"sameAs":["https:\/\/code2deploy.com\/blog"],"url":"https:\/\/code2deploy.com\/blog\/author\/enam\/"}]}},"_links":{"self":[{"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/posts\/4081","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/comments?post=4081"}],"version-history":[{"count":1,"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/posts\/4081\/revisions"}],"predecessor-version":[{"id":4082,"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/posts\/4081\/revisions\/4082"}],"wp:attachment":[{"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/media?parent=4081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/categories?post=4081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/tags?post=4081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}