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

$ kubectl config set-context --current --namespace=<insert-namespace-name-here>

Validate it

$ kubectl config view | grep namespace:

Creating namespaces using Command line

$ kubectl create ns dev

Creating namespaces using Yaml

apiVersion: v1
kind: Namespace
metadata:
  name: "development"
  labels:
    name: "development"

# kubectl apply -f test.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

apiVersion: v1
kind: Namespace
metadata:
  name: myspace
---

apiVersion: v1
kind: ResourceQuota
metadata:
  name: compute-quota
  namespace: myspace
spec:
  hard:
    requests.cpu: "1"
    requests.memory: 1Gi
    limits.cpu: "2"
    limits.memory: 2Gi
---

apiVersion: v1
kind: ResourceQuota
metadata:
  name: object-quota
  namespace: myspace
spec:
  hard:
    configmaps: "10"
    persistentvolumeclaims: "4"
    replicationcontrollers: "20"
    secrets: "10"
    services: "10"
    services.loadbalancers: "2"
Rajesh Kumar
Follow me