Kubernetes cookbook · Intermediate
Set namespace quotas and default resources
Teams deploy pods without requests, and one namespace can consume the whole cluster.
Approach
A LimitRange gives containers default requests and limits when they don't set any; a ResourceQuota caps the
namespace's total requests, limits and object counts.
Solution
apiVersion: v1
kind: LimitRange
metadata: {name: defaults, namespace: team-a}
spec:
limits:
- type: Container
defaultRequest: {cpu: 100m, memory: 128Mi}
default: {memory: 512Mi}
max: {memory: 4Gi}
---
apiVersion: v1
kind: ResourceQuota
metadata: {name: team-a, namespace: team-a}
spec:
hard:
requests.cpu: "20"
requests.memory: 64Gi
limits.memory: 96Gi
pods: "200"
persistentvolumeclaims: "30"
Pitfalls
- A quota on
limits.cpuforces every pod to set CPU limits, which can cause throttling. - Defaults that are far from real usage; review them with data.
Variations
- Hierarchical namespaces or a platform tool (Capsule) for many tenants.
- Report usage against quota per team.
Further reading: Resource Quotas
Reference: https://kubernetes.io/docs/concepts/policy/resource-quotas/
More Kubernetes recipes
- Autoscale a Deployment on CPU and memory
- Debug a distroless pod with an ephemeral container
- Default-deny network policies that keep DNS working
- Enforce the restricted Pod Security Standard
- Expose a service over HTTPS with automatic certificates
- Give a CI pipeline least-privilege deploy access
- Handle SIGTERM in your application for Kubernetes
- Manage dev, staging and prod with Kustomize overlays