# Namespaces

&#x20;   ▪ 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.

&#x20;   ▪ Any resource that exists within Kubernetes exists either in the default namespace or a namespace that is created by the cluster operator. <mark style="color:red;">**`Only nodes`**</mark> and <mark style="color:red;">**`persistent storage volumes`**</mark> exist outside the namespace; these low-level resources are always visible to every namespace in the cluster.

&#x20;   ▪ Namespaces provide a mechanism for isolating groups of resources within a single cluster&#x20;

&#x20;   ▪ Like a virtual cluster inside a cluster

![](https://2601183865-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8QpIzz6VwCOTMwaqKYa3%2Fuploads%2FogE0Cymg1E9JYE9qersj%2FScreen%20Shot%202022-06-29%20at%2023.32.57.png?alt=media\&token=cd15dd6f-c0e7-4964-a98a-89c6ef68f40e)

* **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:

![](https://2601183865-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8QpIzz6VwCOTMwaqKYa3%2Fuploads%2FCFyhEChzmMdrRl4tCaeo%2Fimage.png?alt=media\&token=0c86024f-dffe-404c-a759-fc40269e2bf5)

![](https://2601183865-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8QpIzz6VwCOTMwaqKYa3%2Fuploads%2FF818cRpq9vjiubW1FlIv%2Fimage.png?alt=media\&token=4e5a4f15-10b2-4294-be03-d625f1ddbcd6)

### Access service in another namespace:

![](https://2601183865-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8QpIzz6VwCOTMwaqKYa3%2Fuploads%2F1zIELiAwJ6ROH5xJ7ZAZ%2Fimage.png?alt=media\&token=5e5045b1-418c-419f-a7f9-9018b69a9bef)

### 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**.
