Kubernetes resource quota, keeping cpu and memory in check
If you share cluster with other teams, sooner or later there is one pod that eat all node memory and then everything else start getting OOMKilled. Usually that is the moment people found out ResourceQuota is exist.
ResourceQuota is just hard cap for one namespace, you set it one time and after that API server refuse anything that make namespace go over the line. No magic, no autoscaler involved.
First, requests and limits
Quota is counted from requests and limits of each container, so quick recap:
resources:
requests:
cpu: "250m"
memory: "256Mi"
limits:
cpu: "1"
memory: "512Mi"
request is what scheduler reserve for your container. Two container requesting 250m on 1 CPU node, scheduler will not put third one there. limit is the ceiling. And here is the part people mix up, cpu and memory behave differently at the ceiling.
CPU is throttled. You over the limit, container just run slow but still alive.
Memory is not gentle like that. There is no throttling for memory, cross the limit and kernel OOMKill your container on the spot. I got bitten by this couple of times already, so now i always leave some headroom on memory limit.
The quota itself
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"
This one say team-a can reserve up to 4 cpu and 8Gi memory total, and burst until 8 cpu and 16Gi.
$ kubectl apply -f quota.yaml
resourcequota/team-a-quota created
$ kubectl describe resourcequota team-a-quota -n team-a
Name: team-a-quota
Namespace: team-a
Resource Used Hard
-------- ---- ----
limits.cpu 500m 8
limits.memory 1Gi 16Gi
requests.cpu 200m 4
requests.memory 512Mi 8Gi
The Used column is live, so when a namespace suddenly cannot schedule
anything i check this first. Also remember quota count requests against
requests and limits against limits, they is separate budget.
The one that usually bite is limits.memory, pod keep starting fine until
sum of memory limits hit the quota, after that pod creation get rejected with
forbidden error and people google it for one hour. Yes, i was that person.
The annoying part
After quota with requests.cpu or requests.memory exist in the namespace,
every pod in that namespace must set those field. Pod without resources get
rejected right away. Actually this is good discipline, but it surprise
people.
The fix is LimitRange, it fill the default so you not need to touch every manifest:
apiVersion: v1
kind: LimitRange
metadata:
name: defaults
namespace: team-a
spec:
limits:
- type: Container
defaultRequest:
cpu: "100m"
memory: "128Mi"
default:
cpu: "500m"
memory: "256Mi"
Now container without resources get sane value instead of rejected. This object can also set min and max per container, handy to stop someone request 64 cpu “just in case”.
What i actually do
-
one quota per team or environment, not per app. Per app quota only create paperwork.
-
keep request low-ish but memory limit generous. Tight memory limit not throttle anything, it only kill your pod at worst possible moment.
-
before deploy something big,
kubectl describe resourcequotatake 2 second and tell you if it fit. -
if pod suddenly cannot start, check quota first. The error message confusing, but the cause usually obvious after you know it.