What are Namespaces in Kubernetes ??
What are Namespaces in Kubernetes ??

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
- Design Team
- Develop ment Team
- 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
- DesignTeamNamespace
- DevelopmentTeamNamespace
- 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 ? 😓 😓
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.
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.
Comments
Post a Comment