X

Everything you need to know about Kubernetes namespaces.


In this article we will be discussing more about Kubernetes namespaces. Basically we will try to understand what is Kubernetes namespace, how to create it and use it in real scenario.


What is Kubernetes namespaces?

Kubernetes namespaces is a virtual cluster being created within the actual Kubernetes cluster. This will bring separation between the different Kubernetes objects such as Pods, deployments and service etc. This will comes handy in order to separate your cluster environment wise or among the different teams.

How to create Kubernetes namespaces:

Basically there are two methods by which you can create namespaces in Kubernetes:

  • By using the YAML based definitions:

In this method we will defining the namespaces in the YAML file and creating the namespace using the “kubectl create” command as below:

Create the sample YAML file with following contents:

$ cat ns.yaml
apiVersion: v1
kind: Namespace
metadata:
name: test

Now create the namespace object using below command:

$ kubectl create -f ns.yaml
namespace/test created
$

Now list down all the namespaces to cross-verify whether “test” namespace created or not:

$ kubectl get ns
NAME              STATUS   AGE
default           Active   23m
kube-node-lease   Active   23m
kube-public       Active   23m
kube-system       Active   23m
mobile-gateway    Active   23m
oasis             Active   23m
test              Active   19s
$
  • Other method will be creating namespace using command line.

Here in this method we will be creating the namespace directly using kubectl create command as below:

$ kubectl create namespace my-namespace
namespace/my-namespace created
$ kubectl get ns
NAME              STATUS   AGE
default           Active   38m
kube-node-lease   Active   38m
kube-public       Active   38m
kube-system       Active   38m
mobile-gateway    Active   38m
my-namespace      Active   57s
oasis             Active   38m
test              Active   15m
$

How to create the objects within the namespaces

In this demo we will create sample Pod within the “test” namespace that we have created in earlier step. For this create a Pod definition file as below which will define the namespace within which it has to be created.

$ cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: podintest-ns
namespace: test
spec:
containers:
- name: mynginx
image: nginx
$

Now create the Pod using kubectl create command as below:

$ kubectl create -f pod.yaml
pod/podintest-ns created
$

Now in order to list down the Pod status you need to specify the namespace by adding option with “–namespace=test”.

$ kubectl get pods --namespace=test
NAME           READY   STATUS    RESTARTS   AGE
podintest-ns   1/1     Running   0          11s
$

Note: In case you dont specify the namespace option while listing out the objects, Kubernetes will show the objects from the default namespace. So you need to make sure that you are specifying the namespaces while listing out the Kubernetes objects.

Trick!

In case you are not sure about the namespace within which object is created simply use following command which will list down objects(Pods) within all the namespace from the cluster.

kubectl get pods --all-namespaces

Delete namespace

Please note that here while deleting the namespace it will also delete the objects within the namespace. So be cautious while deleting the namespace. In order to delete the namespace use below command:

$ kubectl delete ns test
namespace "test" deleted
$ kubectl delete ns my-namespace
namespace "my-namespace" deleted
$

So this is how we can use namespace in Kubernetes cluster in order to separating the objects within your Kubernetes cluster.

Related Post