Namespaces

▪ Namespaces are a way to organize clusters into virtual sub-clusters — they can be helpful when different teams or projects share a Kubernetes cluster. Any number of namespaces are supported within a cluster, each logically separated from others but with the ability to communicate with each other. Namespaces cannot be nested within each other.

▪ Any resource that exists within Kubernetes exists either in the default namespace or a namespace that is created by the cluster operator. Only nodes and persistent storage volumes exist outside the namespace; these low-level resources are always visible to every namespace in the cluster.

▪ Namespaces provide a mechanism for isolating groups of resources within a single cluster

▪ Like a virtual cluster inside a cluster

  • default: By default all the resource created in Kubernetes cluster are created in the default namespace. By default, the default namespace can allow applications to run with unbounded CPU and memory requests/limits (Until someone set resource quota for the default namespace).

  • kube-public: Namespace for resources that are publicly readable by all users. This namespace is generally reserved for cluster usage.

  • kube-system: It is the Namespace for objects created by Kubernetes systems/control plane.

  • kube-node-lease—a default space for objects related to cluster scaling.

Use Cases for when to use namespaces:

Access service in another namespace:

Control the Namespace

The following command is used to control the namespace.

$ kubectl create –f namespace.yml ---------> 1
$ kubectl get namespace -----------------> 2
$ kubectl get namespace <Namespace name> ------->3
$ kubectl describe namespace <Namespace name> ---->4
$ 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.

Using Namespace in Service - Example

Following is an example of a sample file for using namespace in service.

apiVersion: v1
kind: Service
metadata:
   name: elasticsearch
   namespace: elk
   labels:
      component: elasticsearch
spec:
   type: LoadBalancer
   selector:
      component: elasticsearch
   ports:
   - name: http
      port: 9200
      protocol: TCP
   - name: transport
      port: 9300
      protocol: TCP

In the above code, we are using the same namespace under service metadata with the name of elk.

Last updated