Skip to main content

Namespace

Namespaces provide 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, Pods, etc.) and not for cluster-wide objects (e.g. StorageClass, Nodes, PersistentVolumes, etc.).

By default, a Kubernetes cluster will instantiate a default namespace when provisioning the cluster to hold the default set of Pods, Services, and Deployments used by the cluster.

Create

To create new namespace we can use kubectl create command or using yaml file definition.

Lets try to create new namespace using command.

➜ kubectl create namespace test  
namespace/test created

We can get list of namespace using kubectl get command.

➜ kubectl get namespace
NAME STATUS AGE
default Active 28d
kube-node-lease Active 28d
kube-public Active 28d
kube-system Active 28d
kubernetes-dashboard Active 28d
test Active 6s

To delete a namespace we can use kubectl delete command.

➜ kubectl delete namespace test
namespace "test" deleted

Now lets try to create namespace using yaml file definition. This method is preferred if you want to use infrastructure as a code (IaC).

Create new file namespace.yaml and put this namespace definition below. This will create a new namespace named limited.

apiVersion: v1
kind: Namespace
metadata:
name: limited
labels:
name: limited

Lets apply using kubectl apply and get the name space again.

kubectl apply -f namespace.yaml  
namespace/limited created
➜ kubectl get namespace        
NAME STATUS AGE
default Active 28d
kube-node-lease Active 28d
kube-public Active 28d
kube-system Active 28d
kubernetes-dashboard Active 28d
limited Active 10s

References