Sub topic 9
Namespace defaults and bounds with LimitRange
This chapter assumes you know how requests and limits work on a container, and that you can apply manifests and use namespaces. A LimitRange is a namespaced object. It gives containers default requests and limits and rejects containers whose values fall outside bounds you set.
What a LimitRange does
A LimitRange does two jobs in one namespace:
- Defaults: it fills in
requestsandlimitson containers that omit them. - Bounds: it rejects objects whose values are too small, too large, or too far apart.
The API server applies it at admission, when a pod is created. Pods that already exist are not changed. If you edit a LimitRange, only new pods see the new rules.
This matters because of the QoS rules from earlier chapters. A container with no resources makes its pod BestEffort, and the scheduler counts it as needing nothing. A namespace default removes that risk. It is also required for ResourceQuota on CPU and memory, which refuses pods that don't state those values.
Fields
| Field | Meaning |
|---|---|
| `defaultRequest` | Request applied when the container sets none |
| `default` | Limit applied when the container sets none |
| `min` | Smallest allowed request and limit |
| `max` | Largest allowed limit |
| `maxLimitRequestRatio` | Largest allowed limit divided by request |
Each entry has a type: Container, Pod (the sum over all containers), or PersistentVolumeClaim. Note that default means the default limit, which is easy to misread.
Example
apiVersion: v1
kind: LimitRange
metadata:
name: default-resources
namespace: team-a
spec:
limits:
- type: Container
defaultRequest:
cpu: 100m
memory: 128Mi
default:
cpu: 500m
memory: 256Mi
min:
cpu: 50m
memory: 64Mi
max:
cpu: "1"
memory: 512Mi
maxLimitRequestRatio:
cpu: "10"Create the namespace first if it doesn't exist, then apply the file and start a pod with no resources:
kubectl create namespace team-a
kubectl apply -f limitrange.yaml
kubectl run probe --image=nginx:1.27 -n team-a
kubectl get pod probe -n team-a -o jsonpath='{.spec.containers[0].resources}'The output shows the injected values:
{"limits":{"cpu":"500m","memory":"256Mi"},"requests":{"cpu":"100m","memory":"128Mi"}}Because requests are below limits, this pod is Burstable.
Rejection
Defaults are applied first, and bounds are checked afterward. This pod asks for more memory than max allows:
apiVersion: v1
kind: Pod
metadata:
name: too-big
namespace: team-a
spec:
containers:
- name: app
image: nginx:1.27
resources:
requests:
memory: 256Mi
limits:
memory: 1GiThe API server refuses it with a message like maximum memory usage per Container is 512Mi, but limit is 1Gi. The pod is never created, so you see the error at kubectl apply time. Nothing shows up as Pending.
Common pitfalls
- Partial settings: if a container sets only a limit, its request defaults to that limit, not to
defaultRequest. A container that sets only a request gets the default limit. That limit must not be below the request, or the pod is rejected. - Defaults that violate bounds: a
defaultabovemaxmakes every unconfigured pod fail. Keepmin≤defaultRequest≤default≤max. - Several LimitRanges: with more than one in a namespace, which default wins is not specified. Use one per namespace.
- Old pods: a new LimitRange does not fix running pods. Restart the workloads to pick up the defaults.
Exercise
A container in team-a sets limits.memory: 400Mi and no request. The LimitRange above is active. What request does the container get, and what happens if you set limits.memory: 40Mi instead?
Show answer
With a 400Mi limit and no request, the request defaults to the limit, 400Mi, which is within max (512Mi). That makes memory Guaranteed-style for that resource. With a 40Mi limit, the value is below min (64Mi), so the API server rejects the pod with 403 Forbidden.