Security
Quotas and defaults for a tenant
LimitRange, ResourceQuota, Pod Security
Team A gets a namespace of its own in a shared cluster. Three things built into the API server keep it in its lane: a LimitRange that fills in resources nobody wrote, a ResourceQuota that caps what the namespace may use in total, and Pod Security admission at the restricted level. None of them is a webhook or a controller you install. The team deploys its first app and runs into all three, in the order the API server checks them.
- Namespace
- team-a · enforce and warn: restricted
- Quota
- requests.cpu 2 · requests.memory 4Gi · pods 5
- Defaults
- request 100m / 128Mi · limit 500m / 512Mi
- All built in
- admission plugins in the API server
Read the whole lesson as text
1. The tenant
You create the namespace team-a with two labels: Pod Security's enforce and warn modes, both at the restricted level. Then a ResourceQuota, which caps the sum of all Pods' CPU and memory requests and the number of Pods, and a LimitRange, which gives every container a default request and limit when it doesn't set its own. The quota controller in the controller manager counts what's already there: nothing.
- You create team-a with the restricted Pod Security level.
- You add a ResourceQuota and a LimitRange to the namespace.
- The quota controller counts the namespace's usage and writes it into the quota's status.
2. Warned, not blocked
The team applies a Deployment app with no securityContext and no resources. Zoom into the API server. The request passes authentication and authorization, and Pod Security, in validating admission, looks at the Deployment's Pod template. It breaks four of the restricted rules. But enforce mode applies only to Pods: for a Deployment, only warn and audit apply. So the Deployment is saved, and kubectl prints a warning that's easy to scroll past.
- The team's Deployment arrives at the API server.
- Authorized. The LimitRange only fills in Pods, so the Deployment passes through unchanged.
- Pod Security checks the template against restricted: four violations. For a Deployment, that's a warning.
- No one said no, so the Deployment is saved, warning and all.
3. The ReplicaSet can't
The Deployment controller creates a ReplicaSet, and the ReplicaSet controller tries to create the first Pod. This time the object is a Pod. On the way in, the LimitRange fills in its requests and limits. Then Pod Security, now in enforce mode, finds the same four violations and refuses: 403 Forbidden. The ReplicaSet records a FailedCreate event and keeps retrying, and the Deployment shows ReplicaFailure. Nothing appears in kubectl get pods, which is where people look.
- The ReplicaSet controller asks to create the first Pod.
- The LimitRanger admission plugin gives the container the namespace's default requests and limits.
- Pod Security enforces restricted on Pods, and refuses this one.
4. Fixed
The team adds a securityContext that meets the restricted level: run as non-root, no privilege escalation, drop every capability, and the runtime's default seccomp profile. The image must actually work as a non-root user, which is a change to the image, not just the YAML. The new template gets a new ReplicaSet, whose Pods pass admission, are scheduled and start.
- The team applies the Deployment again, with a securityContext. No warning this time.
- A new ReplicaSet creates three Pods, and this time all three are admitted.
5. Counted
Follow one of those Pods through admission. LimitRanger has filled in 100m and 128Mi. The ResourceQuota plugin, in validating admission, adds those to what the namespace already uses and compares the totals with the quota's hard limits. They fit, so it writes the new totals into the quota's status before the Pod is saved, using the quota's resourceVersion so two Pods created at once can't both spend the same room. A quota on requests.cpu also means a Pod with no CPU request would be refused; the LimitRange's default is what lets the team skip writing one.
- ResourceQuota adds this Pod's requests to the namespace's total and compares with the limits.
- The new totals are written to the quota's status, then the Pod is saved.
6. Exceeded
The three Pods start, and later the team scales app to six. Two more Pods fit and the quota reaches 5 of 5. For the sixth, the ResourceQuota plugin refuses: 403, exceeded quota. Like the Pod Security error, it reaches the ReplicaSet controller, not the team: a FailedCreate event on the ReplicaSet and a ReplicaFailure condition on the Deployment, which shows 5 of 6 Pods and stays that way until the quota is raised or the team scales down.
- The Pods start on node-1 and node-2.
- The team scales app to six replicas.
- Pods four and five are admitted, and the quota is full.
- The sixth Pod would take the namespace over its Pod limit. Refused.
- The five admitted Pods run. The sixth will keep failing until something changes.
7. The full picture
A namespace becomes a tenant with five pieces: these three, plus RBAC so team A can only touch team-a, and a NetworkPolicy so its Pods only talk to whom they should. All five are rules about the API and the network. The Pods still share nodes, kernels and the control plane, so the next steps up are separate node pools for the tenant, and then a cluster of its own. Compared with Kyverno, everything here is built into the API server and fixed in what it can express; Kyverno's policies are written by you and can check anything.
- A RoleBinding and a NetworkPolicy complete the namespace's walls.
Summary
A LimitRange, a ResourceQuota and Pod Security are all admission plugins inside the API server, so each Pod meets them in a fixed order: defaults filled in while mutating, then Pod Security and the quota while validating. Pod Security only enforces on Pods, so a bad Deployment is saved with a warning and fails later, on the ReplicaSet. The quota is spent in admission, before the Pod is saved. Both refusals land on the ReplicaSet as FailedCreate, which is where to look when Pods don't appear.
Next lesson: Runtime alerts with Falco