Updated April 13, 2023
Introduction to Kubernetes Namespace
Kubernetes Namespace is reasonable when you have different teams, projects or customers which share a same Kubernetes Cluster.
This works by providing:
- A scope of names.
- A way to attach authorizations and policies to a subsection of a cluster.
In Kubernetes, when you create a service, a corresponding DNS entry is created which is of the form as follows:
If a container only uses <service_name>, then it will be resolved to the service which is local to current namespace.
If you need to reach a service in other namespaces, then you need to use the FQDN (fully qualified domain name) like “<service_name>.
We need to note that not all object in Kubernetes need to be in namespace, like nodes and persistentVolumes need not to be in namespace as those are low-level resources.
To get the list of resources which are in a namespace and which are not, use below commands:
kubectl api-resources --namespaced=true
kubectl api-resources --namespaced=false
Point to note that, if you have only few users like with in tens, you don’t need Namespaces. But if you need any basic features which Namespace provides like having resource’s uniqueness in a Namespace in a cluster, then start using Namespaces. Also note that one Namespaces can’t be nested in another Namespace and a Kubernetes resource can only be in one single Namespace.
How to use Kubernetes Namespace?
To use Kubernetes Namespace, you must have below at least, even for a testing lab.
- Kubernetes Cluster
- Kubectl command line tool configured
- Basic understanding of Kubernetes Pods, containers, Services and Deployments.
First, we see the default namespace. Which is provided by Kubernetes systems to all object which doesn’t have any other Namespace like default set of Pods, Deployments and Services used by Cluster. Creating multiple Namespaces and use them efficiently for your services to manage your objects collections.
Also, note that Kubernetes starts with three namespaces initially at least. These can be more as per your environment.
- default: For objects with no other namespaces.
- kube-system: For those objects which are created by system.
- kube-public: This is usable by all users and created automatically. This Namespace is mostly reserved for Cluster usage.
Namespaces have below functionalities and on basis of same we use can use them.
- Within same Namespace, Pod to Pod communication.
- Namespaces are virtual cluster sitting on top of physical cluster.
- Namespaces provides logical separation between the environments.
- Namespaces are only hidden from each other but are not fully isolated from each other.
- One service in a Namespace can talk to another service in another Namespace, if the target and sources are used with full name which includes service/object name followed by Namespace.
However, if we use the only service name and DNS internally identifies it, resolves it with in same Namespace.
Examples of Kubernetes Namespace
Given below are the examples:
There are few operations, which can be used for controlling the Namespaces.
Example #1: Create
You can create as much Namespaces as you want. As there is no performance penalty due to this. But sometimes actually improves your performance and efficiency, when you have limited API objects within a Namespace. To create a namespace, you can use command like below. Here namespace.yaml is a sample file which contains your Namespace specifications .
You can use the below command to deploy the file.
kubectl create -f namespace.yml
You can see details like below:
kubectl describe namespace test
Creation of Namespace can also be done through a single command. Here you just must give the name of Namespace.
kubectl create namespace test1
Example #2 Get
To get the current list of Namespaces. You can run below command. Also note that in the output you will get all user created and system created Namespaces.
kubectl get namespace
Above command will output like below, here you see all available Namespaces.
kubectl get namespaces
To list a Namespace or a set of Namespaces you have to specify them like below.
kubectl get namespaces test test1
Example #3: Describe
To get the details about a Namespace. You must run below command.
kubectl describe namespaces kube-system
You can also do same by running like below:
kubectl get namespaces test
If you need more detailed output. Run like below:
kubectl get namespaces test -o yaml
Example #4: Delete
To delete a Namespace. Here example of Namespace is “test”
kubectl delete namespaces test
If you want to delete more than one Namespace, then specify same as below:
kubectl delete namespaces test1 test3
Example #5: Creating and Viewing resources under a Namespace
When you create an object, without specifying the namespace, it will be created in current Namespace which, if not changed manually to a custom Namespace, is default Namespace.
You can specify a namespace for an object in two ways by either using namespace field in specification YAML file for object or Using namespace flag in command line. In below example, you can see that scenario.
Take a sample task like creating a pod using a simple specification like below:
Deploy this file like below:
kubectl apply -f pod-create.yaml
Now when you check the description of this pod, you will find that this pod was created in default namespace.
kubectl describe pod mypod | grep Namespace
Now if create another object by specifying namespace in YAML file, like below:
But note that you should have the specified namespace existing, otherwise the command will fail.
Check this like below:
kubectl get namespaces test
Now apply the file.
kubectl apply -f pod-create-1.yaml
Now when you check like below, you will see you have your pod created in specified namespace.
kubectl get pod mypod-1 –namespace=test
Also, note that you can create objects with same name in different namespaces like below where pod with same name is existing in default as well as user-made Namespace.
kubectl get pod mypod –namespace=test
Likewise, if you create an object like pod using a specification file and don’t mention namespace field in it. But while deploying this file use namespace flag like below, your object will be created in that namespace.
kubectl apply -f pod-create.yaml –namespace=test-2
Check this like below again.
kubectl get pod mypod –namespace=test-2
In Kubernetes environments, where you have a lot of users, resources and objects. You can use multiple Namespaces in your physical cluster. Which then can be treated as virtual clusters as those are logically isolated from one another. This segregation makes your team efficient, help in performance improvement and somehow ease of managing them all.
We hope that this EDUCBA information on “Kubernetes Namespace” was beneficial to you. You can view EDUCBA’s recommended articles for more information.