Working with multiple virtual Kubernetes clusters aka Namespaces

Kubernetes supports multiple virtual clusters backed by the same physical cluster. These virtual clusters are called namespaces.

Namespaces ca be used in multiple environment with many users spread across multiple teams, or projects. Names of resources need to be unique within a namespace, but not across namespaces.

Namespaces are a way to divide cluster resources between multiple users (via resource quota).

Viewing namespaces

$ kubectl get namespace

Setting the namespace preference in kubectl config file

Validate it

$ kubectl config view | grep namespace:

Creating namespaces using Command line

$ kubectl create ns dev

Creating namespaces using Yaml

Deleting namespaces

$ kubectl delete ns dev

Filtering and Performing Actions by Namespace

$ kubectl create deployment –image nginx demo-nginx –namespace=demo-namespace

Interesting facts of Namespaces and DNS

When you create a Service, it creates a corresponding DNS entry. This entry is of the form ..svc.cluster.local, which means that if a container just uses , it will resolve to the service which is local to a namespace.

This is useful for using the same configuration across multiple namespaces such as Development, Staging and Production. If you want to reach across namespaces, you need to use the fully qualified domain name (FQDN).

Not All Objects are in a Namespace

In a namespace
$ kubectl api-resources –namespaced=true
Not in a namespace
$ kubectl api-resources –namespaced=false

Creating namespaces and ResourceQuota using Yaml