How to Create New Custom Namespaces in Kubernetes{3 Best Methods}

In this tutorial, I will guide you through the steps to create new custom namespaces in Kubernetes. Namespaces are one of the key features in Kubernetes frequently used to divide the physical cluster into multiple virtual clusters. Each virtual cluster acts as a namespace under which multiple resources can be created.

What is Namespace in Kubernetes

Namespaces are logical segmentation of Kubernetes Cluster resources like pods, services, replication controllers and so on. It is mostly required when you want to divide Kubernetes Cluster resources between different users via resource quotas. So one resource under a namespace cannot be shared with another namespace.

However there are some resources in Kubernetes like nodes and persistent volumes which does not lie in any namespace. You can check more about namespaces on Kubernetes Documentation.

How to Create New Custom Namespaces in Kubernetes{Best Practices}

How to Create New Custom Namespaces in Kubernetes

Also Read: Create a Service to Expose Your Apps on Kubernetes(v1.16)

Method 1: Create Namespaces Using YAML File

In this method you need to use below template to create a namespace where you need to define the apiVersion, kind, metadata and the namespace name under metadata. It is also the recommended way where you can not only create namespaces but the pods and other resources using single YAML file.

apiVersion: v1
kind: Namespace
metadata:
name: <name_of_namespace>

Here we are trying to create a new custom namespace appcom using YAML file as shown in below example.

[root@localhost ~]# vi appcom.yml
apiVersion: v1
kind: Namespace
metadata:
name: appcom

As of now we have below namespaces currently available in Kubernetes Cluster. From the output, you can check the List of namespaces, its current status and the total time from when the namespaces were created.

Advertisements
[root@localhost ~]# kubectl get namespaces
NAME              STATUS AGE
nginxnam          Active 75m
default           Active 77m
kube-node-lease   Active 77m
kube-public       Active 77m
kube-system       Active 77m
example           Active 42m

nginxnam : This is custom created namespace for all Nginx applications.

default : It is the default namespace for all the objects which does not have any namespace.

kube-node-lease : This namespace is for leased objects which improves the node heartbeat as cluster scales.

kube-public : This namespace is used by the objects which was created by Kubernetes System.

kube-system : This namespace gets automatically created and is mostly reserved for cluster usage by all the users.

example : This is also a custom namespace created earlier for all example applications.

Now to create our new custom namespace you need to use kubectl apply -f appcom.yml command as shown below. This will create appcom namespace as evident from below output.

[root@localhost ~]# kubectl apply -f appcom.yml
namespace/appcom created

Once it is created you can again run kubectl get namespaces command and check the status of our custom namespace. From below output you can notice that appcom namespace is been active from last 69 seconds.

[root@localhost ~]# kubectl get namespaces
NAME        STATUS AGE
nginxnam    Active 78m
appcom      Active 69s
default     Active 80m
hello-name  Active 80m
kube-public Active 80m
kube-system Active 80m
example     Active 45m

If you want to delete the namespaces then you need to use kubectl delete namespaces <name_of_namespace> command. Here we are trying to delete appcom namespace using kubectl delete namespaces appcom command as shown below.

[root@localhost ~]# kubectl delete namespaces appcom
namespace "appcom" deleted

Method 2: Create Namespaces Without Using Any YAML File

Second method that you can use to create namespace would be directly through kubectl create namespace <namespace_name> command without using any YAML file as shown below. Here we are trying to create appcom namespace using kubectl create namespace appcom command.

[root@localhost ~]# kubectl create namespace appcom
namespace/appcom created

Similarly like above if you want you can delete appcom namespace using kubectl delete namespaces appcom command as shown below.

[root@localhost ~]# kubectl delete namespaces appcom
namespace "appcom" deleted

You can verify the successful deletion using kubectl get namespaces command as shown below. Now you don’t see that namespace in the shown output.

[root@localhost ~]# kubectl get namespaces
NAME         STATUS AGE
nginxnam     Active 83m
default      Active 85m
kube-example Active 85m
kube-public  Active 85m
kube-system  Active 85m
example      Active 50m

Method 3: Create Namespaces Using JSON File

Another method that you can use to create namespaces is through JSON File. Few programmers and developers comfortable in JSON prefers to use JSON file to create namespaces. Syntax of creating Namespace is mentioned below. As you can see from below syntax it is not much different from YAML File.

{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": {
"name": "<namespace_name>",
"labels": {
"name": "<label_name>"
}
}
}

Like shown in above methods, we are creating appcom namespace but this time is through JSON file instead of using YAML file as you can see below.

[root@localhost ~]# vi appcom.json
{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": {
"name": "appcom",
"labels": {
"name": "appcom"
}
}
}

Now to create appcom namespace using JSON file you need to use kubectl create -f appcom.json command as shown below.

[root@localhost ~]# kubectl create -f appcom.json
namespace/appcom created

Once it is created you can again run kubectl get namespaces command like above and check the status of our custom namespace. It shows Active from last 10 seconds which means it is created successfully and working fine.

[root@localhost ~]# kubectl get namespaces
NAME        STATUS AGE
nginxnam    Active 88m
appcom      Active 10s
default     Active 90m
hello-name  Active 90m
kube-public Active 90m
kube-system Active 90m
example     Active 55m

 

 

 

 

 

Popular Recommendations:-

How to Install and Configure Kubernetes on Redhat/CentOS 7 with Best Example

Best 15 Kubectl and Kubeadm Commands

50 Useful zypper command examples to Manage Packages on OpenSUSE Linux

How to Check Stateful and Stateless Pods in Kubernetes Cluster

6 Easy Steps to Setup and Manage Log rotation Using logrotate in Linux

Migrate CentOS 8 to CentOS Stream 8 in 6 Easy Steps

26 iostat, vmstat and mpstat command examples to Monitor Linux Performance

22 Best Kubectl Command Examples

Advertisements

Leave a Reply