Sub topic 10
Capping a Namespace with ResourceQuota
This chapter assumes you can write pod manifests with requests and limits, and that you know what a LimitRange does. A ResourceQuota caps the total resources a whole namespace can claim. A LimitRange shapes individual containers, so the two work together.
What a ResourceQuota does
A ResourceQuota is a namespaced object. It sets hard totals for a namespace, such as the sum of all CPU requests, the sum of all memory limits, or the number of pods. The API server checks the quota when you create or update an object. If the new object would push the namespace over a total, the request is rejected right away with a 403 Forbidden error.
Quotas count what is declared in manifests, not what pods actually use. A namespace with a 4 CPU request quota can be full even if its pods are idle.
Here is the quota from the plan:
apiVersion: v1
kind: ResourceQuota
metadata:
name: team-a-quota
namespace: team-a
spec:
hard:
requests.cpu: "4"
requests.memory: 8Gi
limits.cpu: "8"
limits.memory: 16Gi
pods: "20"Apply it with kubectl apply -f quota.yaml. Every pod in team-a now counts against these five numbers.
How admission checks a quota
The "must specify" rule
Once a quota tracks requests.cpu or limits.memory, every new pod in that namespace must set those values. If a manifest omits them, the API server rejects the pod with an error like must specify limits.cpu.
This is where a LimitRange helps. Its defaultRequest and default fields fill in missing values before the quota check runs. The usual setup is to create both objects in the namespace:
apiVersion: v1
kind: LimitRange
metadata:
name: default-resources
namespace: team-a
spec:
limits:
- type: Container
defaultRequest:
cpu: 100m
memory: 128Mi
default:
cpu: 500m
memory: 256MiWith this in place, a pod that omits resources gets 100m CPU and 128Mi memory as requests, so it can still pass the quota check.
Checking quota usage
Use kubectl describe to compare used and hard totals:
kubectl describe resourcequota team-a-quota -n team-aThe output lists each resource with a Used and a Hard column. Run this first when a deployment in a namespace stops creating pods.
Common errors
- Pods missing, Deployment looks fine. A Deployment can be accepted while its ReplicaSet fails to create pods because of the quota. The error appears in the ReplicaSet events, not on the Deployment. Run
kubectl describe replicaset -n team-aand look forexceeded quota. - Rolling updates stall. During a rollout, old and new pods exist together. If the quota has no headroom, the surge pods are rejected. Leave room for the surge, or lower
maxSurge. - Missing resource settings. Pods without requests or limits are rejected once the quota tracks them. Add a LimitRange or set the values in the manifest.
- Quota too low for one pod. A single pod that asks for more than the whole
requests.cpuquota can never be admitted. Check the numbers before blaming the scheduler.
A rejected pod never reaches the scheduler, so it does not show as Pending. This is the main difference from a pod that is admitted but cannot find a node with enough free capacity.
Other things a quota can count
Besides CPU and memory, a quota can cap object counts such as pods, services, persistentvolumeclaims and configmaps. It can also cap requests.ephemeral-storage and limits.ephemeral-storage. Add them under spec.hard in the same way.
Exercise
A namespace has a ResourceQuota with requests.cpu: "4". Its pods use only 0.5 CPU in total, but a new pod that requests 1 CPU is rejected. Why can this happen, and what command shows the cause?
Show answer
Quotas count declared requests, not real usage. If existing pods already request about 3.5 CPU or more, adding 1 CPU would pass the 4 CPU total, so the API server rejects the pod. Run kubectl describe resourcequota -n <namespace> to compare the Used and Hard columns.
Exercise
Your Deployment shows 0 of 3 pods ready, and kubectl describe deployment shows no errors. Where do you look next to find a quota error?
Show answer
Look at the ReplicaSet events with kubectl describe replicaset -n <namespace>. Pod creation is done by the ReplicaSet, so that is where an exceeded quota or must specify message appears.