Kubernetes Namespaces

Kubernetes Namespaces

Introduction

Namespaces are Kubernetes objects which partition a single Kubernetes cluster into multiple virtual clusters. Each Kubernetes namespace provides the scope for Kubernetes Names it contains; which means that using the combination of an object name and a Namespace, each object gets an unique identity across the cluster.

By default, a Kubernetes cluster is created with the following three namespaces:

  • default: By default all the resource created in Kubernetes cluster are created in the default namespace. By default the default namespace can allow applications to run with unbounded CPU and memory requests/limits (Until someone set resource quota for the default namespace).
  • kube-public: Namespace for resources that are publicly readable by all users. This namespace is generally reserved for cluster usage.
  • kube-system: It is the Namespace for objects created by Kubernetes systems/control plane.

 

Prerequisites

  • Basic experience with Linux/Unix system.
  • Familiarity with Kubernetes.
  • Kubernetes cluster to perform the demo.

Creating a Namespace

  • List the namespaces present in the Kubernetes cluster.

We can create a Namespace in two ways either by using kubectl create namespace command or by using configuration file.

  • Create a new namespace my-namespace with command.

  • List the namespaces.

We can also create a namespace using the configuration file.

  • Lets create a namespace from configuration file.

  • Deploy above file.
  • List the namespaces.

 

Use-cases for Namespaces

Namespaces have several use-cases. Lets take a look at some of the main reasons why namespaces should be used:

Partition Cluster resources.

Kubernetes Namespaces can be used to divide a cluster into logical partitions allowing a single large Kubernetes cluster to be used by multiple users and teams, or a single user with multiple applications. Each user, team, or application running in a Namespace, is isolated from every other user, team, or application in other Namespaces and they operate as if they are the sole user of the cluster (note that Namespaces do not provide network segmentation).

Deploy application in specific namespace.

Now we will see how to create a Kubernetes object inside specific namespace.

  • List the namespaces.
  • Lets create a pod from following configuration.

In above configuration, we have specified the namespace with field namespace: my-namespace. So this pod will be created inside the my-namespace namespace.

  • Deploy a pod with above configuration.
  • List the pods present in my-namespace namespace.

Bind user to specific namespace.

By setting the context, we can bind a user to a specific namespace, so this namespace will be the default namespace for that user. You can do it either by modifying the kubeconfig file or using following command

  • Create a Namespace for different teams like dev, qa and production

  • Set the context for different user groups.

Here we have created bindings for the Dev team to dev namespace, QA team to qa namespace and Production team to production namespace. This specific namespaces will act as default namespace for particular team assigned to it. For detailed information of Kubernetes context and kubeconfig follow the documentation given at kubernetes.io.

Ability to specify resource consumption policies.

With Resource Quotas applied on namespaces, we can limit the cluster resources usage of a particular set of users. A resource quota is responsible for limiting resource consumption per namespace. It also can limit the quantity of objects that can be created in a namespace by type, as well as the total amount of compute resources that may be consumed by resources in that namespace.

Now we will see how we can configure the resource quota for specific Namespace.

  • Create a new demo namespace.
  • Create a resource qouta object for demo namespace.

Here, we are specifying that in demo namespace; we cannot request more than 0.5 CPU and 512Mi RAM overall. So if we already have a pod running with 256 Mi request; we would not be able to schedule a new pod, if it requires more than remaining 256 Mi memory. Similarly, we are setting the hard limit of 1 CPU and 1GB RAM for the namespace.
  • Deploy this resource quota object.
  • Get the detailed information about the above created resource quota.

Here we can see the information about hard limit as well as used resources for given namespace.

  • Deploy Demo1 application within this namespace. Whose resource request can be fulfilled by the resource quota set for this namespace.

  • Deploy this demo1 application.

This application resource request was within the resource quota’s specification. So this pod got deployed.

  • Now lets create another pod in demo namespace. Whose resource request is exceeding the resource quota set for the namespace. And let’s see what happens.

  • Lets try to deploy this demo2 application.

This error occured because this pod is requesting the greater resources than specified in resource quota.

  • Lets delete the demo namespace. It will delete namespace along with all the object running inside it.
  • Delete earlier created namespace.

Policies to run resources.

With the Role-based access control (RBAC), we can control the access permissions within particular Namespace. Cluster Admin can controls the access permission granted to the user or groups of users in specific namespaces, this can be achieved by the help of Role Binding.

A unique scope for Kubernetes Names.

Kubernetes namespace provides the scope for Kubernetes Names. Within the Namespace an Object can be referred by a short name like my-app and the Namespace adds further scope to identify the object, e.g. my-app.my-namespace. Within the Namespace the name of the object must be unique and the combination of Name and Namespace (e.g. my-app.my-namespace ) must have to be unique at cluster level.

Summary

Namespaces are essential objects for dividing and managing Kubernetes clusters. Namespaces allow us to logically segregate and assign resources to individual users, teams or applications. Namespaces provide the basic building blocks for resource usage allowance, access control and isolation for applications, users or groups of users. By using Namespaces, you can increase resource efficiencies as a single cluster can now be used for a diverse set of workloads.

References

  • Kubernetes Namespaces : https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/
  • Kubernetes Names : https://kubernetes.io/docs/concepts/overview/working-with-objects/names/
  • Role-based access control (RBAC) : https://kubernetes.io/docs/reference/access-authn-authz/rbac/
  • Resource Quotas : https://kubernetes.io/docs/concepts/policy/resource-quotas/)
  • Kubeconfig : https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/

 

This blog post was contributed by our training partner CloudYuga.
For expert training in Kubernetes, check out their website at https://cloudyuga.guru/.

 

Kubernetes Namespaces with Nirmata
Kubernetes For Developers Part 5 - Volume Objects and Persistent Volumes
No Comments

Post a Comment