2022-07-10

Kubernetes Namespace

What is Namespace in Kubernetes

In Kubernetes, a Namespace is a high-level abstraction that allows multiple virtual clusters to exist within the same physical cluster. It is essentially a mechanism for dividing cluster resources between multiple users or teams. Namespaces provide a scope for names, making it easier to manage and isolate resources within the same cluster.

The Purpose and Benefits of Namespaces

Namespaces serve a variety of purposes in a Kubernetes cluster. They allow you to partition resources, manage access control, and provide a way to divide cluster resources into logically named groups. This is particularly useful when a cluster is shared by several teams or projects, each with its own set of resources.

There are several benefits to using Namespaces. For one, they can help manage resource allocation and usage in large clusters. They can also isolate processes, helping to ensure that a process in one namespace does not interfere with a process in another. This can be particularly useful in multi-tenant environments where many users or teams are sharing a single Kubernetes cluster.

Creating a Namespace

Starting Minikube

Before creating a Namespace, we must ensure that our Kubernetes cluster is running. Minikube, a tool that lets you run Kubernetes locally, can be initiated with the following command:

bash
$ minikube start

Upon executing this command, Minikube will start a virtual machine and install a single-node Kubernetes cluster on it.

Checking Existing Namespaces

Before generating a new Namespace, we should identify the current namespaces in our cluster. We can do this by running the following command:

bash
$ kubectl get namespace

NAME              STATUS   AGE
default           Active   8h
kube-node-lease   Active   8h
kube-public       Active   8h
kube-system       Active   8h

Executing this command will yield a list of all existing namespaces in your cluster.

Creating a New Namespace: staging

Once we have a grasp on our existing namespaces, we can now proceed to create a new one. This can be achieved by the kubectl create namespace command followed by the desired name for the namespace. To create a namespace called staging, we can use the command:

bash
$ kubectl create namespace staging

namespace/staging created

The above command will create a new Namespace named staging in your cluster.

Deploying an Application in a Namespace

Running an Application

With a Namespace established, we can deploy an application within that Namespace. In our case, we'll deploy a sample application called helloworld.

The deployment can be achieved using the kubectl run command, as shown below:

bash
$ kubectl run \
    --image gcr.io/google-samples/hello-app:1.0 \
    --restart Never \
    --namespace staging \
    helloworld

pod/helloworld created

Executing this command will initiate a new deployment of the 'helloworld' application within the staging Namespace.

Verifying the Deployment in staging

The following command will list all the pods running in the staging Namespace, confirming whether the helloworld application was deployed successfully.

bash
$ kubectl get pod --namespace staging

NAME         READY   STATUS    RESTARTS   AGE
helloworld   1/1     Running   0          14s

The default namespace does not show anything because the Pod has not been created.

bash
$ kubectl get pod --namespace default

No resources found in default namespace.

Deleting a Namespace

When a particular namespace is no longer required, you can delete it. In Kubernetes, you can do this using the kubectl delete namespace command followed by the name of the namespace. For instance, to delete the staging namespace, use the following command:

bash
$ kubectl delete namespace staging

namespace "staging" deleted

Remember, deleting a namespace will also eliminate all the resources within that namespace. Therefore, ensure all vital data and applications are safely backed up or moved to a different namespace before performing this operation. Once the staging namespace is deleted, all the Pods running the helloworld application in this namespace will also be removed.

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!