Namespaces

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).

When to Use Multiple Namespaces

Namespaces are intended for use in environments with many users spread across multiple teams, or projects. For clusters with a few to tens of users, you should not need to create or think about namespaces at all. Start using namespaces when you need the features they provide.

Namespaces provide a scope for names. Names of resources need to be unique within a namespace, but not across namespaces. 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

It is not necessary to use multiple namespaces to separate slightly different resources, such as different versions of the same software: use labels to distinguish resources within the same namespace

Functionality of Namespace

Following are some of the important functionalities of a Namespace in Kubernetes −

  • Namespaces help pod-to-pod communication using the same namespace.
  • Namespaces are virtual clusters that can sit on top of the same physical cluster.
  • They provide logical separation between the teams and their environments.

Create a Namespace

The following command is used to create a namespace.

apiVersion: v1
kind: Namespce
metadata
   name: test

Control the Namespace

The following command is used to control the namespace.

$ kubectl create –f namespace.yml
$ kubectl get namespace
$ kubectl get namespace <Namespace name>
$ kubectl describe namespace <Namespace name>
$ kubectl delete namespace <Namespace name>

In the above code,

  • We are using the command to create a namespace.
  • This will list all the available namespace.
  • This will get a particular namespace whose name is specified in the command.
  • This will describe the complete details about the service.
  • This will delete a particular namespace present in the cluster.