Kubernetes Namespaces Explained with Easy Examples

This tutorial post will help you to understand everything about Kubernetes Namespaces.

In the previous posts, already we have explained the below topics related to Kubernetes. Refer those links to understand this topic from basics.

What is Kubernetes – Learn Kubernetes from Basics
Install & Configure Kubernetes Cluster with Docker on Linux
Create Kubernetes Deployment, Services & Pods Using Kubectl
Create Kubernetes YAML for Deployment, Service & Pods
What is Docker – Get Started from Basics – Docker Tutorial
What is Container, What is Docker on Container – Get Started
How to Install Docker on CentOS 7 / RHEL 7

Kubernetes Namespaces Explained with Examples

In this post, We will cover the below topics on Kubernetes namespaces.

  1. Kubernetes Namespaces Explained with Examples
  2. What is Kubernetes Namespaces?
  3. Why Kubernetes Namespaces are used?
  4. What is “default” namespaces in Kubernetes?
  5. How to Get Namespaces available in Kubernetes?
  6. How to Create Kubernetes Namespaces?

Let’s get started.

What is Kubernetes Namespaces?

Clusters can be divided into virtual sub-clusters using namespaces. When multiple teams or projects share a Kubernetes cluster, they can be useful. Within a cluster, any number of namespaces can be maintained, each logically distinct from the others yet able to communicate with one another. Within each other, namespaces cannot be nested.

Any resource in Kubernetes exists in one of two namespaces: the default namespace or a namespace created by the cluster operator. Outside of the namespace, only nodes and persistent storage volumes exist; these low-level resources are always visible to every namespace in the cluster.

Why Kubernetes Namespaces are used?

The following are some of the most important features of a Kubernetes Namespaces.

  1. Using the same namespace, namespaces aid pod-to-pod communication.
  2. Namespaces are virtual clusters that can be stacked on top of each other.
  3. They create a logical barrier between the teams and their surroundings.

What is “default” namespaces in Kubernetes?

Kubernetes has three default namespaces comes within the cluster. They are as follows:

default: This is the namespaces that every Kubernetes command uses by default, and where every Kubernetes resource is stored by default. The entire cluster is in ‘default’ until additional namespaces are created.
kube-system: This term should be avoided when referring to Kubernetes components.
kube-public: It is a resource for public use. Users are not advised to utilise it.

How to Get Namespaces available in Kubernetes?

Use kubectl command to get namespaces available in kubernetes as every resources.

[kubernetes-master ~]$ kubectl get ns

If you want to get all the resources created and available within the namespaces, specify the namespaces to get every resources as below.

To get list of pods running within the specific namespaces:

[kubernetes-master ~]$ kubectl -n test-namespace get pods

To get list of services created within the specific namespaces:

[kubernetes-master ~]$ kubectl -n test-namespace get svc

Siimlaryly, you can “get” options to list any resources created for the namespaces.

How to Create Kubernetes Namespaces?

As every resources, namespaces also can be created using command or yaml files.

Command Method:

Use “kubectl create” command to create namespaces. lets say we want to create namespace called “develop”.

[kubernetes-master ~]$ kubectl create namespace develop
namespace/develop created

Where, “develop” is the namespace.

Yaml method:

Create a yaml file to create namespaces as below. Replace the namespace name in the ‘name’ field as you prefer to create it. in this example, lets create the namespace called “develop”.

[kubernetes-master ~]$ cat develop-ns.yaml
apiVersion: v1
kind: Namespace
metadata
   name: develop

Apply the created namespace yaml file.

[kubernetes-master ~]$ kubectl apply -f develop-ns.yaml
namespace/develop created

Thats all on this post about Kubernetes Namespaces.

Keep practicing and have fun. Leave your comments if any.

Support Us: Share with your friends and groups.

Stay connected with us on social networking sites, Thank you.

YouTube | Facebook | Twitter | Pinterest | Telegram