Kubernetes Namespace: How To Use It To Organize And Optimize Costs
Tags: kubernetes, cloud, devops
Published: 2022-12-22 16:49:48

<!-- wp:paragraph -->
<p>When you’re running a relatively small cluster, you might get away with having no structure in place for it. But with time, your cluster will inevitably scale to dozens of pods and hundreds of containers. If you keep running them carefree, you’ll deal with a mess sooner rather than later. Here’s your golden ticket to get out of this chaos: Kubernetes namespace.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>By keeping your cluster organized with namespaces, <a href="https://cast.ai/blog/kubernetes-labels-expert-guide-with-10-best-practices/">labels</a>, and annotations, you’ll avoid the performance, maintenance, and security issues that come together in a package with the lack of control over the deployed objects and services.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Keep reading to learn how to use Kubernetes namespaces to organize your clusters and optimize them for cost.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading" id="h-first-let-s-quickly-recap-how-namespaces-work">First, let’s quickly recap how namespaces work</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>When you create a physical Kubernetes cluster, you’ll get one workable namespace on top of it by default. This is where you create Kubernetes objects. But as your project grows, having a single namespace will quickly become an issue.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>A namespace functions like a virtual cluster, and Kubernetes lets you create multiple such virtual clusters in a single physical cluster. By configuring multiple namespaces, you’ll free your team members from the burden of working with a single namespace. This will improve your cluster’s manageability, performance, and security.</p>
<!-- /wp:paragraph -->

<!-- wp:quote {"className":"is-style-plain"} -->
<blockquote class="wp-block-quote is-style-plain"><!-- wp:paragraph -->
<p>But don’t rush away to create a bunch of random namespaces. What you need is a<strong> namespace strategy</strong>.</p>
<!-- /wp:paragraph --></blockquote>
<!-- /wp:quote -->

<!-- wp:paragraph -->
<p><strong>How do you build a namespace strategy? You’re not going to like this, but “it depends.”</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I’ve seen many different namespace strategies out there. They usually depend on factors like team size, team structure, and project complexity.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>A small team dealing with a few microservices can easily get away with that one namespace you get in Kubernetes by default. But the same setup will cause massive issues in a fast-growing company with multiple services. In this scenario, it’s better to create sub-teams and give each a separate namespace.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading" id="h-which-projects-or-teams-need-more-than-one-kubernetes-namespace">Which projects or teams need more than one Kubernetes namespace?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In larger companies, there may be many widely dispersed teams. Team A might be working on a project that Team B isn’t even aware of. But they all use resources in the same Kubernetes namespace. When Team A deploys its project to that namespace, it risks impacting the work of Team B.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>What these teams need is <strong>isolation and team-based access security on separate namespaces</strong>. Managers can even add resource quotas per namespace to make sure that an incredibly resource-hungry application doesn’t exhaust all the cluster capacity, affecting resources from other teams.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This is just one of the many problems a single namespace can cause in a larger company. Keeping up with frequent changes, dealing with third parties that contribute to the platform, and coordinating all of these resources become a massive challenge to DevOps folks.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Add to this approaches like service mesh or multi-cloud CD, and you got yourself into a fix. You need multiple namespaces to solve this.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Using namespaces just makes sense – especially considering that you get these benefits:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Adding more namespaces lets teams or projects work in their virtual clusters without impacting each other’s work.&nbsp;</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>You also improve role-based access controls (RBAC) by limiting users and processes to specific namespaces.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Namespaces help you divide a cluster’s resources between various teams with resource quotas.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>You can use namespaces to easily separate the development, testing, and deployment phases, keeping the entire lifecycle on one cluster.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>Most importantly (at least for the purpose of this article), namespaces open the door to resource control.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You can use them to define resource limits, ensuring that each namespace can access a specific amount of CPU and memory resources. This, in turn, allows you to separate cluster resources across multiple projects and ensure that every project has the resources it needs and doesn’t interfere with the resources you set for other projects.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading" id="h-how-to-use-namespaces-in-your-project">How to use namespaces in your project</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Create a Kubernetes cluster, and you’ll get three namespaces out of the box. Want to see what they are? Run this command:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>&gt; kubectl get namespaces</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>This is what you’ll get:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>NAME&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; STATUS &nbsp; AGE

default &nbsp; &nbsp; &nbsp; ACTIVE &nbsp; 2d

kube-system &nbsp; ACTIVE &nbsp; 2d

kube-public &nbsp; ACTIVE &nbsp; 2d</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>What are they all for?</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>kube-system – this namespace is reserved for the Kubernetes engine; you’re not supposed to use it.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>kube-public – this is where Kubernetes stores public access data (for example, cluster information).</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>default – you can use this namespace to create apps and services.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>If you create a new cluster component and don't specify a namespace for it, Kubernetes will create it in the default namespace. But as I mentioned, the default namespace might work only if you have a small team working on a small system.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading" id="h-how-do-you-create-a-new-kubernetes-namespace">How do you create a new Kubernetes namespace?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>A single kubectl command is what you need:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>&gt; kubectl create namespace dev</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>You can also create a new namespace using a YAML configuration file. Doing it this way is a good idea because you’ll have it logged in the history in the configuration file repository of objects created in a cluster.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Here’s how you create a namespace with a configuration file:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>kind: Namespace
apiVersion: v1
metadata:
&nbsp;&nbsp;name: dev
&nbsp;&nbsp;labels:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: dev</code></pre>
<!-- /wp:code -->

<!-- wp:code -->
<pre class="wp-block-code"><code>&gt; kubectl apply -f dev.yaml</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Easy!</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>But here’s where things get a little tricky: each project you run comes with its own lifecycle. Mixing development and production resources is a bad idea, so your cluster will need a more nuanced namespace strategy. For example, you can split your cluster into development, staging, and production environments.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Check out <a href="https://kubernetes.io/docs/tasks/administer-cluster/namespaces/#creating-a-new-namespace">Kubernetes docs</a> for more details.<a href="https://kubernetes.io/docs/tasks/administer-cluster/namespaces/#creating-a-new-namespace">&nbsp;</a></p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading" id="h-here-s-how-namespaces-help-you-in-cost-allocation">Here’s how namespaces help you in cost allocation</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>If you want to gain more control of that mounting cloud bill you’re struggling to reduce, cost visibility is the first step. Allocating resource costs within a Kubernetes environment is the first step.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>It makes sense to allocate costs at the level of workload by the individual container. Once that’s done, you can assign costs to specific teams, departments, and even individual developers by aggregating different workload collections.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Here’s an example framework for allocating costs at the workload level:</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading" id="1-start-by-measuring-resources">1. <strong>Start by measuring resources</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Depending on the resource, you can either rely on the Kubernetes scheduler to measure the average amount of resources consumed or turn to your cloud provider to check the amount of provisioned resources (for example, load balancers and persistent volumes).</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You’ll also need different metrics for particular resources. For example, it’s smart to measure CPU and memory allocation by the maximum level of request and usage. This is how you can check the number of resources reserved by the Kubernetes scheduler.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The Kubernetes API can measure the period of time during which a given resource (like memory, CPU, and GPU) was consumed by checking the amount of time spent in the running state for this resource. Teams should also reconcile this with data from the cloud provider - the duration when a particular resource was provisioned.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading" id="2-check-resource-pricing">2. Check resource pricing</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Another point relates to resource pricing. Cloud providers offer hundreds of virtual machines where you can run your cluster. AWS alone has more than 500 <a href="https://cast.ai/blog/400-ec2-instance-types-the-good-the-bad-and-the-ugly/">EC2 instance types</a> with completely different pricing per hour.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading" id="3-allocate-cloud-waste-if-it-s-unavoidable">3. <strong>Allocate cloud waste if it’s unavoidable</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>It’s common for teams to end up with idle capacity. When allocating resources, you can decide which team should pay for which idle resources. Assigning such costs to the team, which is responsible for making the provisioning decision, incentivizes engineers to pay more attention to costs.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Once you allocate costs across individual workloads, you can easily aggregate them using any Kubernetes concept – starting with Kubernetes namespace and labeling to annotation and controller.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Mapping your Kubernetes cloud spend to a team, product or department is a time-consuming task if you do it manually. Luckily, there are solutions on the market that do it for you.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading" id="h-4-use-this-free-monitoring-tool-to-track-kubernetes-costs-by-namespaces">4. Use this free monitoring tool to track Kubernetes costs by namespaces</h3>
<!-- /wp:heading -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://lh3.googleusercontent.com/s2MuWqU98zrg7_rlOUquOGkRkltaxGmM3lUepp-A2PdAbn5ZNz_NSSkmCK5r5fIXp1P1XMxPbYpa9aq5D0u7rrUmyZMHgl-jWctetjDUg_q_5FFoCxc1lqB5NY9zdgqXeei7tqsTev6zXJPvxDFuZDKz7DZY58wcu0TwpWtPwvrNhW8lm_lqZNBAL6AxaA" alt=""/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><a href="https://cast.ai/cloud-cost-monitoring/">Kubernetes cost monitoring</a> tool lets you check the costs of any of your workloads in real time and use any concept that works for you. Using the CAST AI free cost monitoring module, you can filter compute costs by namespaces and easily track cloud costs by team or service.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Connect your cluster and see how much you’re spending in real time.</p>
<!-- /wp:paragraph -->

<!-- wp:group {"style":{"spacing":{"padding":{"top":"2rem","right":"2rem","bottom":"2rem","left":"2rem"},"margin":{"top":"2rem","bottom":"2rem"}}},"backgroundColor":"cast-black","className":"pattern-cta-text-btn","layout":{"type":"flex","flexWrap":"wrap","justifyContent":"space-between","verticalAlignment":"center"}} -->
<div class="wp-block-group pattern-cta-text-btn has-cast-black-background-color has-background" style="margin-top:2rem;margin-bottom:2rem;padding-top:2rem;padding-right:2rem;padding-bottom:2rem;padding-left:2rem"><!-- wp:group {"style":{"spacing":{"padding":{"top":"0rem","right":"0rem","bottom":"0rem","left":"0rem"},"blockGap":"0.5rem"}},"layout":{"type":"constrained","contentSize":"520px"}} -->
<div class="wp-block-group" style="padding-top:0rem;padding-right:0rem;padding-bottom:0rem;padding-left:0rem"><!-- wp:heading {"level":5} -->
<h5 class="wp-block-heading" id="h-cast-ai-clients-save-an-average-of-63-on-their-kubernetes-bills">CAST AI clients <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-cast-green-color">save an average of 63%</mark> on their Kubernetes bills</h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Connect your cluster and see your costs in 5 min, no credit card required.</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->

<!-- wp:buttons {"layout":{"type":"flex","justifyContent":"left","orientation":"horizontal","flexWrap":"nowrap"}} -->
<div class="wp-block-buttons"><!-- wp:button {"width":100} -->
<div class="wp-block-button has-custom-width wp-block-button__width-100"><a class="wp-block-button__link wp-element-button" href="https://console.cast.ai/?screen_hint=SIGNUP" target="_blank" rel="noreferrer noopener">Get started</a></div>

<!-- /wp:group -->

<!-- wp:heading -->
<h2 class="wp-block-heading" id="faq">FAQ</h2>
<!-- /wp:heading -->

<!-- wp:yoast/faq-block {"questions":[{"id":"faq-question-1671618353660","question":[{"type":"strong","props":{"children":["What is a namespace in Kubernetes?"]}}],"answer":[{"type":"br","props":{"children":[]}},"A Kubernetes namespace lets engineers isolate groups of resources within one Kubernetes cluster. Namespaces are used in environments counting many users spread across multiple teams or projects.",{"type":"br","props":{"children":[]}},{"type":"br","props":{"children":[]}},"Namespaces also provide a scope for resource names, which need to be unique within a namespace, but not across namespaces. You can’t nest namespaces inside one another, and each of your Kubernetes resources can only be in one namespace."],"jsonQuestion":"\u003cstrong\u003eWhat is a namespace in Kubernetes?\u003c/strong\u003e","jsonAnswer":"\u003cbr/\u003eA Kubernetes namespace lets engineers isolate groups of resources within one Kubernetes cluster. Namespaces are used in environments counting many users spread across multiple teams or projects.\u003cbr/\u003e\u003cbr/\u003eNamespaces also provide a scope for resource names, which need to be unique within a namespace, but not across namespaces. You can’t nest namespaces inside one another, and each of your Kubernetes resources can only be in one namespace."},{"id":"faq-question-1671618391824","question":[{"type":"strong","props":{"children":["What is the difference between namespace and pod?"]}}],"answer":[{"type":"br","props":{"children":[]}},"In Kubernetes, a pod is a unit of replication in a Kubernetes cluster. A single cluster can contain many pods, which may be related or unrelated, and grouped under namespaces. A namespace, on the other hand, offers a mechanism to organize a cluster into virtual sub-clusters, which comes in handy when different teams or projects share that Kubernetes cluster."],"jsonQuestion":"\u003cstrong\u003eWhat is the difference between namespace and pod?\u003c/strong\u003e","jsonAnswer":"\u003cbr/\u003eIn Kubernetes, a pod is a unit of replication in a Kubernetes cluster. A single cluster can contain many pods, which may be related or unrelated, and grouped under namespaces. A namespace, on the other hand, offers a mechanism to organize a cluster into virtual sub-clusters, which comes in handy when different teams or projects share that Kubernetes cluster."},{"id":"faq-question-1671618412170","question":[{"type":"strong","props":{"children":["When to use a Kubernetes namespace?"]}}],"answer":[{"type":"br","props":{"children":[]}},"Kubernetes namespaces have many helpful use cases for engineers and teams:",{"type":"br","props":{"children":[]}},"- Namespaces let you isolate resources between teams and projects so developers can apply changes without worrying that they might affect the work of others.",{"type":"br","props":{"children":[]}},"- They improve security by enhancing role-based access controls (RBAC), which limit users and processes to specific namespaces.",{"type":"br","props":{"children":[]}},"- You can control cloud resources better by adding resource quotas to teams and ensuring that every namespace has access to a specified amount of CPU and memory.",{"type":"br","props":{"children":[]}},"- Namespaces offer a great way to separate the development, testing, and deployment of containerized applications. That way, your cluster can include the entire lifecycle."],"jsonQuestion":"\u003cstrong\u003eWhen to use a Kubernetes namespace?\u003c/strong\u003e","jsonAnswer":"\u003cbr/\u003eKubernetes namespaces have many helpful use cases for engineers and teams:\u003cbr/\u003e- Namespaces let you isolate resources between teams and projects so developers can apply changes without worrying that they might affect the work of others.\u003cbr/\u003e- They improve security by enhancing role-based access controls (RBAC), which limit users and processes to specific namespaces.\u003cbr/\u003e- You can control cloud resources better by adding resource quotas to teams and ensuring that every namespace has access to a specified amount of CPU and memory.\u003cbr/\u003e- Namespaces offer a great way to separate the development, testing, and deployment of containerized applications. That way, your cluster can include the entire lifecycle."}]} -->
<div class="schema-faq wp-block-yoast-faq-block"><div class="schema-faq-section" id="faq-question-1671618353660"><strong class="schema-faq-question"><strong>What is a namespace in Kubernetes?</strong></strong> <p class="schema-faq-answer"><br/>A Kubernetes namespace lets engineers isolate groups of resources within one Kubernetes cluster. Namespaces are used in environments counting many users spread across multiple teams or projects.<br/><br/>Namespaces also provide a scope for resource names, which need to be unique within a namespace, but not across namespaces. You can’t nest namespaces inside one another, and each of your Kubernetes resources can only be in one namespace.</p> </div> <div class="schema-faq-section" id="faq-question-1671618391824"><strong class="schema-faq-question"><strong>What is the difference between namespace and pod?</strong></strong> <p class="schema-faq-answer"><br/>In Kubernetes, a pod is a unit of replication in a Kubernetes cluster. A single cluster can contain many pods, which may be related or unrelated, and grouped under namespaces. A namespace, on the other hand, offers a mechanism to organize a cluster into virtual sub-clusters, which comes in handy when different teams or projects share that Kubernetes cluster.</p> </div> <div class="schema-faq-section" id="faq-question-1671618412170"><strong class="schema-faq-question"><strong>When to use a Kubernetes namespace?</strong></strong> <p class="schema-faq-answer"><br/>Kubernetes namespaces have many helpful use cases for engineers and teams:<br/>- Namespaces let you isolate resources between teams and projects so developers can apply changes without worrying that they might affect the work of others.<br/>- They improve security by enhancing role-based access controls (RBAC), which limit users and processes to specific namespaces.<br/>- You can control cloud resources better by adding resource quotas to teams and ensuring that every namespace has access to a specified amount of CPU and memory.<br/>- Namespaces offer a great way to separate the development, testing, and deployment of containerized applications. That way, your cluster can include the entire lifecycle.</p> </div> </div>
<!-- /wp:yoast/faq-block -->