# Kubernetes - Creating Namespaces

Kubernetes namespaces provide a way to divide cluster resources between multiple users, teams, or applications. By isolating different environments (like dev, test, and prod) within the same cluster, namespaces help in organizing and managing resources efficiently.

By default, Kubernetes comes with four namespaces: `default`,`kube-public`, `kube-system` and `kube-node-lease`

**<mark>Understanding Kubernetes Namespaces</mark>**

When you first deploy a Kubernetes cluster, it comes with four default namespaces:

* **default**: The default namespace for resources with no other specified namespace.
    
* **kube-public**: Used for resources that should be publicly accessible across the cluster. Typically contains cluster-wide information.
    
* **kube-system**: Reserved for Kubernetes system components, such as the API server, scheduler, and other core processes.
    
* **kube-node-lease**: Used for managing node lease objects, which help in determining the availability of nodes in the cluster.
    

These namespaces allow you to categorize and manage resources efficiently, especially as your cluster grows in complexity.

**<mark>Creating a Kubernetes Namespace</mark>**

Creating a namespace in Kubernetes is a straight forward process. You can use the `kubectl` command-line tool or a YAML file.

**1\. Verifying Existing Namespaces**

Before creating a new namespace, it's helpful to verify the namespaces already running in your cluster:

```plaintext
kubectl get namespaces
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724273537060/226aa4bd-07cb-4e06-9164-2fb981d10f8f.png align="center")

#### 2\. Creating a Namespace Using `kubectl create` Command (Imperative Approach)

The simplest way to create a namespace is by using the `kubectl create` command:

```plaintext
kubectl create namespace <namespace-name>
```

For example, to create a namespace called `dev`:

```plaintext
kubectl create namespace dev
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724273785985/1050ae26-d472-4c5e-a194-d462367dff28.png align="center")

You can verify the creation by listing all namespaces:

```plaintext
kubectl get namespaces
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724273884195/801fba93-d57a-4dd7-8525-19ad4753275a.png align="center")

#### 3\. Creating a Namespace Using a YAML Configuration (Declarative Approach)

Create a YAML file named `namespaces.yaml`:

```plaintext
apiVersion: v1
kind: Namespace
metadata:
  name: test
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724273990526/f857d882-c356-4bfc-886d-92ead7fd8d65.png align="center")

Apply the YAML file to create the namespace:

```plaintext
kubectl apply -f namespaces.yaml
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724274092578/23825d44-b27f-45f9-b1ee-1a6dfbdb8f71.png align="center")

This declarative method is ideal for maintaining version control and automating your Kubernetes infrastructure.

---

**<mark>Verifying the Namespace</mark>**

After creating a namespace, you can verify its existence with:

```plaintext
kubectl get namespaces
```

This command will list all namespaces in your cluster, including the newly created one.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724274136719/a013663f-032d-4356-9763-36db8a76cdbb.png align="center")

---

**<mark>Deleting a Kubernetes Namespace</mark>**

Deleting a namespace is just as simple as creating one, but you need to be cautious. When you delete a namespace, all resources (like pods, services, and deployments) within that namespace are also deleted.

To delete a namespace, use the following command:

```plaintext
kubectl delete namespace <namespace-name>
```

For example, to delete the `dev` namespace:

```plaintext
kubectl delete namespace dev
```

This will remove the namespace and all its resources from your cluster.

---

### Conclusion

Namespaces are a powerful feature in Kubernetes that allows for efficient resource management and isolation within a cluster. By understanding how to create and delete namespaces, you can better organize your workloads and maintain a clean and manageable cluster environment.
