Namespaces in Kubernetes

Updated: Aug 12, 2020







In the last article we studied about deployments in kubernetes. Now there can be scenarios where kubernetes cluster may be hosting different applications on the same cluster and resources need to be isolated.


Namespaces in Kubernetes are a way to isolate cluster resources.They can be thought about segregated regions inside a cluster.

Objects within a namespace share same set of resources and communicate with each other directly.

Whatever we have been doing in the last articles like creation of pods,services and deployments, we were doing it inside a namespace( By default the namespace used is the default namespace)

By default below namespaces are created at the time of cluster installation. We can create our own as well.

  • default The default namespace for objects with no other namespace

  • kube-system The namespace for objects created by the Kubernetes system

  • kube-public This namespace is created automatically and is readable by all users (including those not authenticated). This namespace is mostly reserved for cluster usage, in case that some resources should be visible and readable publicly throughout the whole cluster. The public aspect of this namespace is only a convention, not a requirement.

  • kube-node-lease This namespace for the lease objects associated with each node which improves the performance of the node heartbeats as the cluster scales.


[root@node1 ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   6d19h
kube-node-lease   Active   6d19h
kube-public       Active   6d19h
kube-system       Active   6d19h
[root@node1 ~]# kubectl create ns demo-ns
namespace/demo-ns created
[root@node1 ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   6d19h
demo-ns           Active   2s
kube-node-lease   Active   6d19h
kube-public       Active   6d19h
kube-system       Active   6d19h


To change the current namespace

[root@node1 ~]# kubectl config set-context --current --namespace=demo-ns

Each namespace can have its own quota of resources

Services within the namespace can communicate with only the name but if communication needs to happen between the namespaces, then the namespace name must be appended with the service.


For listing/creating resources we have to mention the namespace as well

like kubectl get pods -n namespace_name.

Resource quota can be created as required as shown below.



## namespace-resource-quota.yaml
apiVersion: v1
kind: ResourceQuota
metadata:
  name: mem-cpu-demo
spec:
  hard:
    requests.cpu: "1"
    requests.memory: 1Gi
    limits.cpu: "2"
    limits.memory: 2Gi
    
    ## Save and exit this file and execute below command 
    kubectl create -f namespace-resource-quota.yaml
    
    






Alright that is it for this article. I hope you are loving our journey learning kubernetes. We will study the concept of services in the next article.






347 views0 comments