Setup

First of all, start the cluster (minikube)

$ minikube start

 

 and set k as an alias to kubectl so that we’ve to type less.

$ alias k="kubectl"

Let’s check all the namespaces the cluster has

$ k get ns

(ns is shorthand for namespace)

 

As we can see the cluster has 4 namespaces that come out of the box in a Kubernetes cluster.

How To Use Kubernetes Namespaces?

Because it is very easy for a team to accidentally overwrite or disrupt another service without even realizing it and Kubernetes Namespaces allows us to share the cluster with multiple teams or projects without impacting others’ work. They are logically separated from each other and within a namespace, you can’t create any two resources of the same name but you can create the same resource in a different namespace. While most types of resources are namespace, a few aren’t, for example, the Node resource which is global and not tied to a single namespace.

To follow this tutorial you need a working cluster it can be minikube, kind, or any other managed Kubernetes service like EKS, GKE, AKS, etc. For the sake of simplicity, I’m using a minikube cluster.

Similar Reads

Setup

First of all, start the cluster (minikube)...

Creating a Namespace

A namespace is like any other Kubernetes resource, so we can create it either Imperatively or declaratively. We’ll see both ways. Declarative means we describe the desired state of our cluster in the manifests file without specifying procedures for how that state will be achieved. We can create these manifests either in YAML or JSON format, since YAML is more human-readable we’ll write our files in YAML. Imperative management means creating Kubernetes objects directly at the command line against a Kubernetes cluster. It is suitable for development projects but not recommended for Production projects....

Managing resources in a namespace

To create a resource in a namespace we can either specify the namespace when creating the resource with the kubectl create command or by adding an entry in the metadata section, let’s see both ways. For our demo purpose, I’ll create a deployment resource you can create any other resource....

Deleting namespace

When we delete a namespace all the resources under it will be deleted as well. Let’s see:Quickly create a resource in a namespace...