Kubernetes Namespace

Nandhabalan Marimuthu
Nerd For Tech
Published in
2 min readMay 19, 2021

--

what is a namespace?

Namespaces are intended for use in environments with many users spread across multiple teams, or projects. Namespaces cannot be nested inside one another and each Kubernetes resource can only be in one namespace. Namespaces are a way to divide cluster resources between multiple users. Kubernetes is using a default namespace as default.

Create a namespace:

you can create a namespace by using a command prompt or using a YAML file.

using terminal

kubectl create namespace dev

using yml file

For that create a yml file called namespace.yml and put the following commands in it.

apiVersion: v1
kind: Namespace
metadata:
name: dev

and run this command on the terminal

kubectl apply -f namespace.yml

How to deploy in this namespace?

After creating the namespace you can create a deployment in it by both ways but you have to mention the namespace that you're gonna deploy in it.

using terminal

kubectl run dev-nginx --image=nginx -n dev

using yml

This is similar to the normal deployment all you have to do is just add the following under the metadata in your yml file and apply it.

metadata:
namespace: dev

Normally all the deployments that you're created will be under the default namespace. So you can directly access that using the typical kubectl get command.

But In this case, you have to use an extra argument to view your created pods

kubectl get pods -n dev

By using this command you can see all the pods that you deployed under the namespace dev.

To view all the pods from all namespaces

kubectl get pods --all-namespaces

--

--