Sub topic 15
Restricting High Priorities with ResourceQuota
This chapter assumes you can write PriorityClass objects and assign them to Pods, and that you know how a namespace-level ResourceQuota works. Any user who can create Pods can set priorityClassName to any existing PriorityClass. Without a guard, one tenant can pick a high priority and preempt other teams' workloads. A ResourceQuota with a PriorityClass scope limits how much of that priority a namespace can use.
Why priority needs a limit
Priority is a request for importance, and Kubernetes does not check whether a team deserves it. If high-priority exists, a Pod in any namespace can reference it. That Pod can then evict lower-priority Pods on other teams' workloads. Two controls address this:
- A quota scoped to a PriorityClass caps the resources that class can consume in a namespace.
- A quota with a count of zero blocks the class in that namespace altogether.
Scoping a quota to a PriorityClass
A ResourceQuota accepts a scopeSelector. With the PriorityClass scope, the quota counts only Pods that use the listed classes.
apiVersion: v1
kind: ResourceQuota
metadata:
name: high-priority-quota
spec:
hard:
pods: "10"
cpu: "20"
memory: 40Gi
scopeSelector:
matchExpressions:
- operator: In
scopeName: PriorityClass
values: ["high-priority"]This quota applies in the namespace where you create it. Pods in that namespace that use high-priority can total at most 10 Pods, 20 CPUs, and 40Gi of memory in requests. Pods with other classes are not counted against it.
The operators work as follows:
| Operator | Matches |
|---|---|
| `In` | Pods whose class is in `values` |
| `NotIn` | Pods whose class is not in `values` |
| `Exists` | Pods that have any priority class set |
| `DoesNotExist` | Pods with no priority class |
How admission enforces it
The quota is checked when a Pod is created. If the Pod would push usage past a limit, the API server rejects it with a 403 Forbidden error that names the exceeded quota. Pods that already exist are not changed.
Compute quotas such as cpu and memory require each container to declare requests. A Pod without them is rejected in a namespace where such a quota matches the Pod.
Blocking a class in most namespaces
To keep a class out of namespaces by default, create a quota with pods: "0" in each namespace. A common pattern is to give every namespace a zero quota for high classes and raise it only for approved teams.
apiVersion: v1
kind: ResourceQuota
metadata:
name: no-high-priority
spec:
hard:
pods: "0"
scopeSelector:
matchExpressions:
- operator: In
scopeName: PriorityClass
values: ["high-priority"]Cluster administrators can also use the ResourceQuota admission with a quota in the kube-system namespace, or an admission policy, to cover system-wide cases. Check your cluster's version and distribution documentation, because behavior for cluster-wide enforcement varies.
Checking usage
Inspect the quota to see how much of the class a namespace has used.
kubectl describe resourcequota high-priority-quotaThe output lists Used and Hard columns for each resource. A Pod rejected at creation does not appear in the usage, so check the error message from kubectl apply or the Deployment's ReplicaSet events.
Exercise
A namespace has a ResourceQuota with scopeName: PriorityClass, operator: In, values: ["high-priority"] and pods: "2". A team creates a third Pod using high-priority. A fourth Pod in the same namespace uses no priority class. What happens to each?
Show answer
The third Pod is rejected at creation with a 403 error, because it would exceed the quota of 2 Pods for that class. The fourth Pod is not counted by this quota, so it is admitted, subject to any other quotas in the namespace.