What are Namespaces in Kubernetes ??

What are Namespaces in Kubernetes ??

Kubernetes Namespaces

Namespaces are Kubernetes objects which partition a single Kubernetes cluster into multiple virtual clusters.
In Kubernetes, namespaces provides a mechanism for isolating groups of resources within a single cluster. Names of resources need to be unique within a namespace, but not across namespaces.

Let's try to understand the concept of ReplicaSets in very simple terms using real world example.

Lets Dig In !! ⛏️⛏️

Let's take a very simple example where we have the following scenario

Imagine there are three teams in office

  1. Design Team
  2. Develop ment Team
  3. Testing Team

So in the above scenario, following things will happen

  • Every team will be given different floors in the building to do their work
  • Every team will have their own resources to complete their work
  • Every team will have different number of team members depending upon the work load present on them

And none of the team would use each other's resources and would not work on each other's floor.
Here different floors would basically help to separate the teams with each other and would avoid any kind of mixing.

Now let us co-relate the above example with the world of Kubernetes

  • The Kubernetes cluster could also have multiple resources.
  • The resources would be allotted to different teams depending upon the work load they have.

So the question comes how can we separate the resources from one another and keep them isolated in different floors like the real world team s.

Here comes Namespaces in the picture. So Namespaces are nothing more than certain rooms which we create in a Kubernetes cluster just to isolate the resources from one another.
So that these different Namespaces (rooms) could be used by different teams without any one of them messing up and using other teams resources.

For example take an example where we create three namespaces

  1. DesignTeamNamespace
  2. DevelopmentTeamNamespace
  3. TestingTeamNamespce

As the names suggest, above will be used by respective teams and will have the resources required by that team.
This helps the users to keep everything isolated and clean.

Hurray !!🥳🥳 We have understood what actually are Namespaces.
Now Let us understand why we need Namespaces 🤔 🤔

Why should we use Kubernetes Namespaces ? 😓 😓

Photo by Evan Dennis on Unsplash

There are many use cases for Kubernetes namespaces, some of them are as follows :

  • Using Namespaces, Allows team or projects to exist in their own virtual clusters without fear of impacting each other's work.
  • Using Namespaces, Enhances role-based access controls (RBAC) by limiting users and processes to certain namespaces.
  • Using Namespaces, Enables the dividing of a cluster's resources between multiple teams and users via resource quotas.
  • Using Namespaces, Provides an easy method of separating development, testing, and deployment of containerized applications ena bling the entire lifecycle to take place on the same cluster

How can we create Namespaces ? 🤔🤔

We can create Namespaces using YAML files. To create a Kubernetes Namespace with YAML, you first create an empty file, assign it the necessary access permissions, and then define the necessary key-value pairs.

Below is an example of a Namespaces definition file (namespace.yaml)

apiVersion: v1
kind: Namespace
metadata:
name: MyNamespace

There are many aspects and components in this file. Lets break down each one of them 😀

  • Let's start with the apiVersion (key-value pair). This is used to clarify what API server and version you will be running in the background when creating the Namespace.
  • Next is kind which signifies the kind of definition file this is. In our case, it is a "Namespace".
  • < li>Next is metadata, which is a dictionary including the item name and labels. The metadata stores values that are assigned to the Namespace which is being created.

We are done with the Namespace definition file. Now we can save and exit the file.

Use this command to create the Namespace based on the above YAML file :

kubectl create -f namespace.yaml

Alternatively, you can create namespace by just using the below command :

kubectl create namespace MyNamespace

Working with Namespaces 🙌🙌

In this section, let us have a look at different and most used kubectl commands with which we can work with Namespaces inside a Kubernetes cluster.

Photo by insung yoon on Unsplash

Use the below command to get the list of PODs from a particular namespace (here "production")

kubectl get pods --namespace=production

Use the below command to create the POD in a particular namespace (here "development")

kubectl create –f pod-definition.yml --namespace=development

Use the below command to change the namespace where you are present/working (here "development")

kubectl config set-context $(kubectl config current-context)       
--namespace=development

Use the below command to get the PODs from all the namespaces that are present in the K8s Cluster

kubectl get pods --all-namespaces

What next ? 👀 👀

Thanks a lot for reaching till here! This is the end of this article.
But we h ave only scratched the surface of the K8s ecosystem :))
Much more to go, it will be a fun journey where we will learn a lot of cool stuff together.
Do clap and follow me 🙈 if you like my writings and want to read more from me in the future :))

In case of any doubts around this article or for some general chit chat, feel free to reach out to me on my social media handles

Twitter — https://twitter.com/ChindaVibhor

LinkedIn — https://www.linkedin.com/in/vibhor-chinda-465927169/

Related Articles

I will still keep on coming with new articles covering a bunch of topics I am exploring.

That's All folks !! Doodles :))


What are Namespaces in Kubernetes ?? was originally published in Google Cloud - Community on Medium, where people are continuing the conversation by highlighting and responding to this story.

Namaste Devops is a one stop solution view, read and learn Devops Articles selected from worlds Top Devops content publishers inclusing AWS, Azure and others. All the credit/appreciations/issues apart from the Clean UI and faster loading time goes to original author.

Comments

Did you find the article or blog useful? Please share this among your dev friends or network.

An android app or website on your mind?

We build blazing fast Rest APIs and web-apps and love to discuss and develop on great product ideas over a Google meet call. Let's connect for a free consultation or project development.

Contact Us

Trending DevOps Articles

Working with System.Random and threads safely in .NET Core and .NET Framework

Popular DevOps Categories

Docker aws cdk application load balancer AWS CDK Application security AWS CDK application Application Load Balancers with DevOps Guru Auto scale group Automation Autoscale EC2 Autoscale VPC Autoscaling AWS Azure DevOps Big Data BigQuery CAMS DevOps Containers Data Observability Frequently Asked Devops Questions in Interviews GCP Large Table Export GCP Serverless Dataproc DB Export GTmetrix Page Speed 100% Google Page Speed 100% Healthy CI/CD Pipelines How to use AWS Developer Tools IDL web services Infrastructure as code Istio App Deploy Istio Gateways Istio Installation Istio Official Docs Istio Service Istio Traffic Management Java Database Export with GCP Jenkin K8 Kubernetes Large DB Export GCP Linux MSSQL March announcement MySQL Networking Popular DevOps Tools PostgreSQL Puppet Python Database Export with GCP Python GCP Large Table Export Python GCP Serverless Dataproc DB Export Python Postgres DB Export to BigQuery Sprint Top 100 Devops Questions TypeScript Client Generator anti-patterns of DevOps application performance monitoring (APM) aws amplify deploy blazor webassembly aws cdk application load balancer security group aws cdk construct example aws cdk l2 constructs aws cdk web application firewall aws codeguru reviewer cli command aws devops guru performance management aws service catalog best practices aws service catalog ci/cd aws service catalog examples azure Devops use cases azure devops whitepaper codeguru aws cli deploy asp.net core blazor webassembly devops guru for rds devops guru rds performance devops project explanation devops project ideas devops real time examples devops real time scenarios devops whitepaper aws docker-compose.yml health aware ci/cd pipeline example host and deploy asp.net core blazor webassembly on AWS scalable and secure CI/CD pipelines security vulnerabilities ci cd pipeline security vulnerabilities ci cd pipeline aws smithy code generation smithy server generator
Show more