Saumashankar
2 min readNov 11, 2021

--

Kubernetes -Namespace

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. Namespace-based scoping is applicable only for namespaced objects (e.g. Deployments, Services, etc) and not for cluster-wide objects (e.g. StorageClass, Nodes, PersistentVolumes, etc)

Let’s take a step back and begin with a simple analogy that will help build some intuition.

Imagine there are two boys, both named Mark.

To differentiate them from each other, call them by their last names: Smith and Williams.

They come from different houses: the Smith and Williams

There are other members in the house, and these refer to each other simply by their first names.

For example, the father addresses Mark simply as “Mark”.

However, if the father wishes to address Mark from the other house he would use his full name.

Likewise, someone outside of these houses would also use the full name to refer to the boys or anyone within these houses.

Each of these houses have their own set of rules that defines each person’s responsibilities. Likewise, these houses have a set of resources that they can consume.

You can list the current namespaces in a cluster using:

kubectl get namespace

Creating Namespaces

There are a couple ways to create a namespace in kubernetes.

  • Use an namespace definition file:

In namespace-dev.yaml:

apiVersion: v1
kind: Namespace
metadata:
name: dev
kubectl create -f namespace-dev.yaml
  • Without a YAML definition file:
kubectl create namespace dev

If you wish to switch from the default namespace, run the following command:

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

This previous command switches the current namespace by modifying the kubeconfig file.

Finally, to view pods in all namespace:

kubectl get pods --all-namespaces

This will list all the pods in all of the namespaces.

--

--