How to Create New Namespace in Kubernetes [2 Steps]

In this article, We are going to perform How to Create New Namespace in Kubernetes Cluster

What is Namespace in Kubernetes/k8s ?

It is Kubernetes objects which is used to create multiple virtual clusters within same physical cluster.

We can deploy Pods, deployment, service within each Virtual Cluster called as Kubernetes Namespace.

What is use of Namespace in Kubernetes ?

Suppose you have Dev, QA and Prod Environment in your project and you want separate each environment in same cluster and deploy pods, deployments and services also.

In this scenario you can separate these resource in by creating Namespaces for Dev,QA,Prod and create pods, deployments, services.

When you creates the Kubernetes Cluster you can find below Namespaces already in Kubernetes.

Default: It is default namespace, if you create any resource it goes to default namespace, all pods, deployments and services are hold in default namespace if you have not set while creating.

Kube-system: It is used for Kubernetes Components.

Kube-public: It is used for public resources, used only by Cluster only

Prerequisite

Kubernetes Cluster with at-least 1 Master and 1 Worker Node.

Follow below articles to Setup Kubernetes Cluster

9 Steps to Setup Kubernetes on AWS using KOPS

How To Setup Kubernetes Cluster Using Kubeadm on Ubuntu 18.04/16.04 LTS

First List out all Namespaces in Kubernetes cluster using below command

kubectl get namespaces

Output:

NAME                           STATUS           AGE
default                        Active            3d
kube-public                   Active             3d
kube-system                   Active             3d

You can view particular Kubernetes Namespace also

kubectl get namespace kube-system

Output:

NAME          STATUS   AGE
kube-system   Active   3d

To get detailed info about specific namespace

kubectl describe  namespace kube-system

Output:

Name: kube-system
Labels:
Status: Active
No resource quota.
No LimitRange resource

As per output it is showing it is Active Status

How to Create New Namespace in Kubernetes

You can create Namespace using below ways

  1. Creating Kubernetes Namespace using kubectl
  2. Creating Kubernetes Namespace using YAML/JSON

1. Creating Kubernetes Namespace using kubectl

Lets create Kubernetes Namespace named “k8s-dev” using kubectl using below command

kubectl create namespace k8s-dev

 2. Creating Kubernetes Namespace using YAML

We can create Kubernetes Namespace named “k8s-prod” using yaml.

Create a yaml file called k8snamespace.yaml

sudo nano k8snamespace.yaml

Paste the below lines

kind: Namespace
apiVersion: v1
metadata:
name: k8s-prod

Run the below command to create Kubernetes namespace using yaml

kubectl apply -f k8snamespace.yaml

Verify the Namespace if it is created

kubectl get namespaces

OR

kubectl get ns

Output:

NAME                   STATUS   AGE
k8s-prod               Active   1m
k8s-dev                Active   2m
Default                Active   3d
kube-public            Active   3d
kube-system            Active   3d

We have covered How to Create New Namespace in Kubernetes.

Deploy a Pod in Kubernetes Namespace

Lets a deploy a pod in created namespace “k8s-dev”

kubectl run nginx --image=nginx --namespace=k8s-dev

OR

You can create a pod using yaml in “k8s-dev” namespace. Create a pod yaml named “nginxpod.yaml”

sudo nano nginxpod.yaml

Paste the below lines into it

apiVersion: v1
kind: Pod
metadata:
  name: fosstechnix
  namespace: k8s-dev
  labels:
    name: fosstechnix
spec:
  containers:
  - name: nginxcontainer
    image: nginx

Apply the “nginxpod.yaml” to create a pod in “k8s-dev” namespace

kubectl apply -f nginxpod.yaml

Lets check if pod is running

kubectl get pods --namespace=k8s-dev

OR

Kubectl get pods -n k8s-dev

How to set Default Namespace in Kubernetes

If you don’t want enter “–namespace” every time while checking Kubernetes objets then you can set new namespace as dafault using below command

kubectl config set-context --current --namespace=k8s-dev

How to delete a pod in Namespace

Kubectl delete pod <pod-name> --namespace <namespace-name>

OR

Kubectl delete pod <pod-name> -n <namespace-name

To delete all pods from Namespace

kubectl delete pods --all --n <namespace name>

How to Delete Namespace in Kubernetes

If you want to delete namespace in Kubernetes using kubectl . Enter below command

Syntax:

kubectl delete namespaces <namespace-name>

Example:

kubectl delete namespaces k8s-dev

Kuernetes Namespace Frequently Asked Questions(FAQ)

What is Kubernetes Namespace limits ?

By default, Pod , deployment, services, .etc are created in default Namespace . Pod has Unbounded CPU and Memory Limits. You can specify CPU and Memory for limit for each pod.

kubernetes namespace vs cluster ?

Namespaces act as a logical Cluster on Kubernetes Physical Cluster which devides kubernetes resources into smaller unit.

Conclusion

We have covered What is Namespace in Kubernetes , How to Create New Namespace in Kubernetes, list out namespace, kubernetes namespace yaml, Kubernetes namespace commands

Related Articles:

Kubernetes Concepts for Beginners

How to Configure Traefik Ingress Controller on Kubernetes [5 Steps]

4 Steps to Install Kubernetes Dashboard

Reference:

Kubernetes Official Guide

FOSS TechNix

FOSS TechNix (Free,Open Source Software's and Technology Nix*) founded in 2019 is a community platform where you can find How-to Guides, articles for DevOps Tools,Linux and Databases.

1 thought on “How to Create New Namespace in Kubernetes [2 Steps]”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap