Namespaces allow us to organize and isolate our Kubernetes objects. Kubernetes is an open-source orchestration system that allows us to deploy, scale, and manage our Kubernetes clusters.
In Kubernetes, we can use the Kubernetes ResourceQuota object to allocate namespace quotas to a namespace or objects. We can use this object to limit the amount of CPU, memory, storage, and objects created in a specific namespace. Namespace quotas are useful when we have many objects and want to ensure that the resources to different clusters or resources within a cluster are evenly distributed or distributed according to a certain ratio.
Let’s have a look at an executable. In the following example, we’re creating a kind
cluster of one node.
# Create a namespace apiVersion: v1 kind: Namespace metadata: name: educative-namespace --- # Create a ResourceQuota apiVersion: v1 kind: ResourceQuota metadata: name: resourcequota-demo namespace: educative-namespace spec: hard: requests.cpu: "1" requests.memory: 1Gi limits.cpu: "2" limits.memory: 2Gi
In the above example, the resource-namespace.yaml
file contains the definitions of two Kubernetes objects, a namespace, and ResourceQuota
.
Lines 2 and 11: We define the Kubernetes API version that we’ll use to create the namespace and the ResourceQuota
objects, i.e., v1
.
Lines 3 and 12: We define the types of objects that will be created, i.e., ResourceQuota
and a namespace object.
Lines 5 and 14: We give a name to the objects that will be created using their respective definitions, i.e., educative-namespace
and educative-resourcequota
.
Line 15: We specify the namespace for this ResourceQuota
.
Lines 16–20: We allocate the resources and specify the requirements for this namespace. All containers in every pod created in this namespace must have a memory request, memory limit, CPU request, and CPU limit.
The memory and CPU requests must not exceed 1 GiB and 1 CPU for all pods created in this namespace.
The total memory and CPU limit must not exceed 2 GiB and 2 CPUs for all pods created in this namespace.
To create the namespace and the ResourceQuota object, use the following commands:
kubectl apply -f resource-namespace.yaml
If you want to check the status of resources, execute the following command:
kubectl get namespace,resourcequota --all-namespaces
You can create pods and deployments in this namespace and try different namespaces.
Unlock your potential: Kubernetes Deployment and Advanced Operations series, all in one place!
To deepen your understanding of Kubernetes deployment and management, explore our series of Answers below:
How to create a Kubernetes cluster locally with Minikube
Learn how to set up a Kubernetes cluster on your local machine using Minikube for development and testing.
How to create deployment on a Kubernetes cluster via YAML files
Master the process of defining and managing deployments in Kubernetes using YAML configuration files.
What is an Ingress controller in Kubernetes?
Understand the role of an Ingress controller in managing external access and routing traffic within a Kubernetes cluster.
How to implement namespace quotas in Kubernetes
Discover how to apply resource limits to specific namespaces to optimize resource allocation and prevent overuse.
How to implement Kubernetes resource quotas
Learn how to enforce limits on CPU, memory, and storage consumption across your Kubernetes cluster.
Add users using certificates in a Kubernetes cluster
Explore the process of adding and authenticating users in Kubernetes by issuing and managing certificates.
What is service discovery in Kubernetes?
Understand how Kubernetes enables seamless service discovery, allowing applications to communicate efficiently.
How to put a database in Kubernetes
Learn the best practices for deploying and managing databases within a Kubernetes environment.
How to run WordPress on Kubernetes
Follow a step-by-step guide to deploying a scalable and resilient WordPress site on Kubernetes.
Free Resources