Saiyam Pathak avatar
By Saiyam Pathak
Field CTO


Learn how to create a Kubernetes namespace, change the namespace context, and create objects with identical names in different namespaces.

Understanding Kubernetes can be a challenge, but one of the secrets to mastering it lies in understanding namespaces. Namespaces are a fundamental component of Kubernetes, providing a streamlined way to manage resources within a cluster. This lesson will take you through the steps of creating Kubernetes namespaces, changing the namespace context, and creating objects with identical names in different namespaces.

What are Kubernetes Namespaces?

In Kubernetes, namespaces are a way to provide isolated environments to different teams or groups within the same cluster. They are essentially multiple virtual clusters within the same physical cluster. This setup allows for the enforcement of specific resource quotas and policies, making resource management more manageable and efficient.

Creating a Kubernetes Namespace

To view the namespaces in Kubernetes, you can use the command kubectl get ns or kubectl get namespace. This will provide a list of all the existing namespaces. To create a new namespace, you can use the command kubectl create namespace dev or Kubectl get ns dev. To verify the creation of the namespace, use kubectl get ns.

Resource quotas and policies can be applied to the namespace, ensuring that it does not overuse the cluster resources. This feature is particularly useful when managing multiple teams like the development team, testing team, or other groups. Each team can work within a specific namespace with defined resource quotas and policies.

Spinning up Objects of the Same Name in Different Namespaces

Kubernetes allows you to spin up objects of the same name in different namespaces. This feature can be beneficial when you want to run different versions of the same application in separate namespaces. For example, you can use the command kubectl run nginx -- image=nginx to create a pod nginx in the default namespace. If you want to create a pod in the dev namespace, you can use the command kubectl run nginx -- image=nginx -n dev and verify it by using kubectl get pods -n dev.

Changing the Context of a Namespace

To view resources from a specific namespace, we specify the namespace at the end of the command. However, you can also switch the context to view resources from a different namespace. For example, to switch the context to the dev namespace, you can use the command kubectl config set-context --current --namespace=dev.

To confirm whether the context has been successfully switched, you can utilize the command kubectl config view --minify | grep namespace. By executing kubectl get pods, only the pod from the dev namespace will be visible, indicating that we have effectively transitioned the default context from the default namespace to the dev namespace.


In conclusion, namespaces are an essential part of Kubernetes, allowing for efficient resource management within a cluster. You can create, view, and manage namespaces using various kubectl commands. Remember, you can also delete a namespace using the delete command, which will also delete all the resources within it.

Don't stop now, check out your next lesson