Saiyam Pathak avatar
By Saiyam Pathak
Director of Technical Evangelism


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



In this video, we'll be going through the concept of namespaces. In Kubernetes, namespaces are a way to provide isolated environments to different teams or groups within the same cluster with the specific amount of resource quota and other policies enforced. You can think of it as multiple virtual clusters within the same physical cluster or the same cluster.

Creating a namespace

In Kubernetes, you can view the namespace using the command kubectl get ns or kubectl get namespace. You will get a list of all the namespaces which are present. To create the namespace, you can use the command kubectl create namespace dev or Kubectl get ns dev, then verify it by using kubectl get ns. So there can be different resource quotas and policies applied to the namespace, which will ensure that this particular namespace does not overuse the cluster resources. So you can have multiple teams like the dev team, testing team, or the other teams where you can provide different quotas. Then that particular team can do their specific work in a particular namespace with specific resource quotas and policies defined.

Spinning up objects of same name in different namespaces

You can also spin up objects of the same name in different namespaces. So this can help you to run different versions of the same application in other namespaces. For example, use the command kubectl run nginx -- image=nginx. So now, this particular command will create a pod nginx in the default namespace because we haven't provided any namespace. So, with the command, kubectl get pods you can see that it is coming in the next pod. If you try to rerun it, it will say that it already exists. But if you try to run it by using the command kubectl run nginx -- image=nginx -n dev, it'll create a pod in the dev namespace. So you can verify by using the kubectl get pods -n dev command.

Changing the context of a namespace

To view the resources from a particular namespace, we specify the namespace in the end. So there is another thing we can do, which is switching the context. We are getting the pod from the default namespace because the default is the default context. So now, if we want to change the namespace's context and switch to another context while setting the current context to the dev namespace, we can use a command called kubectl config set-context --current --namespace=dev.

And if you want to see whether it's switched or not, you can use the command kubectl config view --minify | grep namespace. If I use kubectl get pods, we'll only see the pod from the dev namespace. So we have switched the default context from default namespace to dev namespace.

In conclusion

So that's it about namespaces. You can also delete a namespace using the delete, and the namespace of dev will be deleted. Internally, it will delete all the resources as well. So that's it about namespaces. Thank you for watching. See you in the following lecture.

Don't stop now, check out your next lesson