In this lesson, you will learn how to enforce resource limits on a namespace to ensure that a single project or team cannot consume all the CPU and memory available in your Kubernetes cluster.
Why Resource Quotas Matter
Imagine your Kubernetes cluster is like a shared office building. If one tenant decides to run a massive server farm in their room, the power grid (your cluster's compute capacity) might fail for everyone else. Without boundaries, a runaway process or an over-eager developer could deploy hundreds of pods, causing the nodes to crash due to "Out of Memory" (OOM) errors or CPU starvation. Resource Quotas act as the circuit breakers for your namespaces, keeping the environment stable.
Defining a Quota
A Resource Quota is a Kubernetes object that tracks the aggregate usage of resources within a specific namespace. When you apply one, you place a "hard cap" on the total amount of resources that all pods in that namespace can collectively request or consume.
Here is an example of a quota applied to a namespace called development:
apiVersion: v1
kind: ResourceQuota
metadata:
name: compute-limit
namespace: development
spec:
hard:
requests.cpu: "2"
requests.memory: 2Gi
limits.cpu: "4"
limits.memory: 4Gi
In this configuration, any pods deployed into the development namespace will be blocked if their total combined requests exceed 2 CPU cores or 2 GiB of memory. Even if you have plenty of physical capacity left on your cluster nodes, the API server will reject any new pod creation if it causes the namespace to exceed these assigned quotas.
Best Practices for Stability
When you implement these quotas, it is important to remember that Kubernetes requires individual pods to declare their resource requirements (requests and limits). If you have a namespace with a quota, every pod you deploy must define its resource needs; otherwise, the cluster won't know how to track the consumption against your limit, and the deployment will fail. By using quotas, you ensure fair distribution and protect your cluster's overall health from resource-heavy workloads.
Key Takeaway: Resource Quotas prevent "noisy neighbor" scenarios by enforcing hard limits on the aggregate CPU and memory consumption of all pods within a specific namespace.