How to Use Resource Quota in Kubernetes Namespace
Use a
ResourceQuota object in a Kubernetes namespace to limit resource usage like CPU, memory, and object counts. Create a YAML manifest defining the quota and apply it with kubectl apply -f in the target namespace.Syntax
A ResourceQuota is a Kubernetes object that limits resource consumption in a namespace. It has a metadata section for naming and namespace, and a spec.hard section where you define limits like CPU, memory, pods, or services.
Each key in spec.hard represents a resource type, and the value is the maximum allowed amount.
yaml
apiVersion: v1
kind: ResourceQuota
metadata:
name: quota-name
namespace: target-namespace
spec:
hard:
cpu: "2"
memory: 4Gi
pods: "10"
services: "5"Example
This example creates a resource quota named compute-resources in the dev namespace. It limits CPU to 2 cores, memory to 4Gi, and allows up to 10 pods.
yaml
apiVersion: v1
kind: Namespace
metadata:
name: dev
---
apiVersion: v1
kind: ResourceQuota
metadata:
name: compute-resources
namespace: dev
spec:
hard:
cpu: "2"
memory: 4Gi
pods: "10"Output
namespace/dev created
resourcequota/compute-resources created
Common Pitfalls
- Not specifying the
namespacein themetadatacauses the quota to apply to the default namespace or fail if none is set. - Setting limits too low can block pod creation or resource allocation unexpectedly.
- Resource names in
spec.hardmust be exact (e.g.,cpu,memory,pods), or the quota will be ignored. - For CPU and memory, use string values with units (e.g., "2", "4Gi").
yaml
apiVersion: v1
kind: ResourceQuota
metadata:
name: wrong-quota
namespace: dev
spec:
hard:
cpu: "2" # Added quotes
memory: "4Gi" # Added quotes
pods: "10" # Corrected resource nameQuick Reference
| Resource | Description | Example Limit |
|---|---|---|
| cpu | Total CPU cores allowed | "2" |
| memory | Total memory allowed | "4Gi" |
| pods | Maximum number of pods | "10" |
| services | Maximum number of services | "5" |
| configmaps | Maximum number of ConfigMaps | "10" |
Key Takeaways
Create a ResourceQuota YAML with limits under spec.hard and apply it in the target namespace.
Always specify the namespace in metadata to ensure the quota applies correctly.
Use correct resource names and units to avoid quota misconfiguration.
Resource quotas help control resource usage and prevent overconsumption in namespaces.
Check quota status with kubectl describe resourcequota -n .