Implementing Resource Quotas in Kubernetes Namespaces

Updated: January 30, 2024 By: Guest Contributor Post a comment

Overview

Kubernetes has become a cornerstone of container orchestration in the software industry, helping teams manage complex deployments with ease. However, with great power comes the need for control—specifically, the control over resources each application consumes. Resource quotas in Kubernetes provide that control, allowing administrators to define how much of the Kubernetes cluster’s resources a Namespace can use. This tutorial aims to guide you through the implementation of resource quotas in Kubernetes Namespaces, from basic to advanced configurations.

Understanding Resource Quotas

Resource Quotas are a Kubernetes feature that allows cluster administrators to set limits on the compute resources a Namespace can use. Resource quotas can be set for various resources, such as CPU, memory, and storage, as well as for object count, such as the number of Pods, PersistentVolumeClaims, or ConfigMaps allowed in a Namespace.

Prerequisites

Before getting started, make sure you have the following:

  • A running Kubernetes cluster
  • kubectl, the command line tool for Kubernetes
  • Basic understanding of Kubernetes concepts like Pods, Namespaces, and Resources

Step-by-Step Instructions

Step 1: Creating a Namespace

kubectl create namespace quota-demo

This command creates a new Namespace named ‘quota-demo’. All later resource quota configurations will be applied within this Namespace.

Step 2: Defining a Basic Resource Quota

Let’s start with a simple example that sets memory and CPU limits for the ‘quota-demo’ Namespace.

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

To apply this quota, save the above YAML to a file named ‘basic-quota.yaml’ and run:

kubectl apply -f basic-quota.yaml

The ‘kubectl get resourcequota’ command can confirm the quota was created:

kubectl get resourcequota -n quota-demo

The output will show the defined memory and CPU limits.<output of creating resource quota>

Step 3: Advanced Resource Quota

For more complex scenarios, you can define quotas that track a wider range of resources or even specific types of compute resources for different quality of service.

apiVersion: v1
kind: ResourceQuota
metadata:
  name: advanced-quota
  namespace: quota-demo
spec:
  hard:
    requests.memory: "1Gi"
    requests.cpu: "1"
    limits.memory: "2Gi"
    limits.cpu: "2"
    pods: "10"
    services: "5"
    replicationcontrollers: "5"
    secrets: "10"
    configmaps: "10"
    persistentvolumeclaims: "4"
    services.loadbalancers: "2"
    services.nodeports: "2"

Again, apply the quota to your namespace and then verify:

kubectl apply -f advanced-quota.yaml
kubectl get resourcequota -n quota-demo

The output will show the details of the advanced quota applied to the ‘quota-demo’ Namespace.

Step 4: Monitoring and Managing Resource Usage

You can monitor the current usage of your Namespace’s resources with the ‘-o yaml’ flag:

kubectl get resourcequota basic-quota -n quota-demo -o yaml

You’ll see a section in the output named ‘status’ that provides real-time usage data.

Step 5: Dynamic Resource Quotas with Labels

Dynamic resource quotas are another advanced feature that use label selectors to apply different sets of quotas to Pods within the same Namespace.

apiVersion: v1
kind: ResourceQuota
metadata:
  name: dynamic-quota
  namespace: quota-demo
spec:
  hard:
    pods: "10"
  scopeSelector:
    matchExpressions:
    - scopeName: InPod
      operator: In
      values:
      - frontend

Apply the dynamic quota similarly with ‘kubectl apply -f dynamic-quota.yaml’, then observe how it affects Pods with different labels.

Step 6: Cleaning Up

To clean up the resources created for this tutorial, run:

kubectl delete namespace quota-demo

This will delete the ‘quota-demo’ Namespace and all related resource quotas.

Conclusion

In this tutorial, we’ve reviewed how to implement and manage Resource Quotas in Kubernetes. Resource Quotas ensure that applications running on a Kubernetes cluster are both fair in resource consumption and compliant with the organization’s operational requirements. By mastering Resource Quotas, cluster administrators can achieve a more stable and predictably performing system.