Sub topic 9
Defaults and bounds per container with LimitRange
This chapter assumes you know Pods, resource requests and limits, and ResourceQuota from the previous chapter. A ResourceQuota caps a whole namespace. A LimitRange sets rules for each container or Pod inside it.
Why you need it
Suppose team-a has a ResourceQuota on requests.cpu and limits.memory. A Pod that declares no requests or limits is rejected, because the quota system cannot count it. A LimitRange fixes this by filling in defaults. It can also reject containers that ask for too little or too much.
Setting defaults
A LimitRange is a namespaced object. Its rules apply only to Pods created in that namespace.
apiVersion: v1
kind: LimitRange
metadata:
name: container-defaults
namespace: team-a
spec:
limits:
- type: Container
defaultRequest:
cpu: 250m
memory: 256Mi
default:
cpu: 500m
memory: 512MidefaultRequestis the request given to a container that sets none.defaultis the limit given to a container that sets none.
Create a Pod with no resources and see what happens:
apiVersion: v1
kind: Pod
metadata:
name: plain
namespace: team-a
spec:
containers:
- name: app
image: nginx:1.27kubectl apply -f limitrange.yaml
kubectl apply -f plain-pod.yaml
kubectl get pod plain --namespace team-a \
--output jsonpath='{.spec.containers[0].resources}'The output shows the defaults written into the Pod spec:
{"limits":{"cpu":"500m","memory":"512Mi"},"requests":{"cpu":"250m","memory":"256Mi"}}Setting bounds
The same object can enforce minimum and maximum values. Add min, max, and optionally maxLimitRequestRatio:
apiVersion: v1
kind: LimitRange
metadata:
name: container-defaults
namespace: team-a
spec:
limits:
- type: Container
defaultRequest:
cpu: 250m
memory: 256Mi
default:
cpu: 500m
memory: 512Mi
min:
cpu: 100m
memory: 128Mi
max:
cpu: "1"
memory: 1Gi
maxLimitRequestRatio:
cpu: "4"A container that asks for memory: 2Gi is refused at creation with a Forbidden error. The message says the maximum memory usage per Container is 1Gi. The ratio rule stops a container from requesting 100m CPU with a 4-CPU limit, which would let it burst far beyond what the scheduler reserved.
How it is applied
The LimitRange is enforced by an admission controller. This is a step in the API server that checks and can modify a request before it is stored.
Three consequences follow from this:
- Defaults are written into the Pod spec at creation. Editing the LimitRange later does not change running Pods.
- Existing Pods are never re-checked against new bounds.
- If you set
defaultbut notdefaultRequest, the request defaults to the limit value.
Other types
Besides Container, a LimitRange accepts type: Pod, which bounds the sum across all containers in a Pod. It also accepts type: PersistentVolumeClaim, which bounds the requested storage size. Defaults only apply to the Container type.
Common errors
- A Pod is rejected by the quota with "must specify limits.cpu". The namespace has no LimitRange defaults, so add them.
- A Pod is rejected for being above
max. Check the limit of every container, including init containers. - A new default has no effect. The Pod already existed. Recreate it.
Exercise
The namespace team-a has a ResourceQuota on limits.memory and a LimitRange with default memory of 512Mi. You change the default to 1Gi. What happens to a Pod created yesterday with no memory limit, and to a Pod created now with none?
Show answer
The Pod from yesterday keeps its 512Mi limit, because defaults are written into the spec at creation and never re-applied. The new Pod gets a 1Gi limit.