KubeGlanceDownload
How to fix "failed quota: must specify limits.cpu" in Kubernetes

troubleshooting

How to fix "failed quota: must specify limits.cpu" in Kubernetes

Pod forbidden with "failed quota: must specify limits.cpu for:"? A container, init container or sidecar has no limit. Find which, and fix it.

· 10 min read

A ResourceQuota in the namespace limits CPU or memory, and at least one container in your Pod does not declare the resource that quota counts. The API server refuses to create the Pod at all. The message tells you exactly which containers, and the list includes init containers and sidecars, which is where most people stop looking.

kubectl apply -f web.yaml
Error from server (Forbidden): error when creating "web.yaml": pods "web" is forbidden: failed quota: compute-quota: must specify limits.cpu for: app; limits.memory for: app; requests.cpu for: app; requests.memory for: app

Read it as: quota compute-quota counts limits.cpu, and container app has no CPU limit; the same for each resource after the semicolons. There is no Pod object, so there is nothing Pending to describe. Behind a Deployment there is no error at your terminal either (see below).

Every output here is from a kind cluster on Kubernetes 1.36.1, with the key cases repeated on 1.37.0. Source references are to release-1.36 and release-1.37, which are identical for every file quoted unless noted.

The fastest check

The message already names the containers. What you need next is what the namespace enforces and whether anything fills in defaults:

kubectl get resourcequota,limitrange -n <namespace>
NAME                          REQUEST                                     LIMIT                                   AGE
resourcequota/compute-quota   requests.cpu: 0/2, requests.memory: 0/2Gi   limits.cpu: 0/4, limits.memory: 0/4Gi   3m43s

A quota with limits.cpu or limits.memory and no LimitRange listed is the whole diagnosis: every container must carry its own limits, and nothing will add them for it.

If the names in the message surprise you, list what each container in your manifest actually declares. Init containers first, because the sidecar and the migration job are the usual gaps:

kubectl create -f web-init.yaml --dry-run=client -o jsonpath='{range .spec.initContainers[*]}init	{.name}	{.resources.limits}{"\n"}{end}{range .spec.containers[*]}app	{.name}	{.resources.limits}{"\n"}{end}'
init	migrate	
init	log-shipper	
app	app	{"cpu":"500m","memory":"256Mi"}

The app container is sized. The two init containers are not, and the quota rejects the Pod for them:

Error from server (Forbidden): error when creating "web-init.yaml": pods "web" is forbidden: failed quota: compute-quota: must specify limits.cpu for: log-shipper,migrate; limits.memory for: log-shipper,migrate; requests.cpu for: log-shipper,migrate; requests.memory for: log-shipper,migrate

log-shipper is a native sidecar (restartPolicy: Always); it is still an entry in initContainers, and it is checked like one.

Behind a Deployment: 0/2 and nothing at the terminal

kubectl apply of a Deployment succeeds, because the quota counts Pods and the Deployment is not a Pod. The ReplicaSet controller is the client that gets the 403, and it has nowhere to print it:

kubectl get deploy,rs
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/web   0/2     0            0           9s

NAME                             DESIRED   CURRENT   READY   AGE
replicaset.apps/web-789dc4db69   2         0         0       9s

CURRENT 0 means no Pod was ever created. The error is an event on the ReplicaSet:

kubectl get events --field-selector reason=FailedCreate -o custom-columns='KIND:.involvedObject.kind,OBJECT:.involvedObject.name,MESSAGE:.message'
KIND         OBJECT           MESSAGE
ReplicaSet   web-789dc4db69   Error creating: pods "web-789dc4db69-lcxnk" is forbidden: failed quota: compute-quota: must specify limits.cpu for: app; limits.memory for: app; requests.cpu for: app; requests.memory for: app

Events expire after an hour by default. The Deployment keeps a copy in a condition that does not:

kubectl get deploy web -o jsonpath='{.status.conditions[?(@.type=="ReplicaFailure")].message}'
pods "web-789dc4db69-lcxnk" is forbidden: failed quota: compute-quota: must specify limits.cpu for: app; limits.memory for: app; requests.cpu for: app; requests.memory for: app

kubectl describe deploy web shows the same thing as ReplicaFailure True FailedCreate under Conditions. StatefulSets, Jobs and DaemonSets fail the same way: a FailedCreate warning on the StatefulSet, Job or DaemonSet itself, and no Pod.

If a Pod does exist and sits in Pending, this is not your problem. Quota never leaves a Pod Pending; it stops it being created. A Pending Pod is a scheduling question, covered in how to fix a Pending pod.

How the API server produces the message

LimitRanger fills in defaults in the mutating phase. ResourceQuota runs last in the validating phase, so it sees the Pod after the defaults. That ordering is why a LimitRange fixes this error.

Admission runs in two passes: every mutating plugin, then every validating plugin. The order within each pass is fixed in AllOrderedPlugins in pkg/kubeapiserver/options/plugins.go: LimitRanger is near the top (line 80 on release-1.36, 78 on release-1.37), and ResourceQuota is second to last, after the webhooks (line 118 / 114). Both are enabled by default.

LimitRanger mutates: it copies a LimitRange’s default and defaultRequest into every container and init container that has no value of its own (mergePodResourceRequirements, plugin/pkg/admission/limitranger/admission.go line 274, init containers at line 281). A mutating webhook that injects a container runs after it, but if any webhook changes the Pod, the API server runs the built-in mutating plugins a second time (reinvocation.go line 42 in k8s.io/apiserver/pkg/admission), so injected containers get the defaults too.

ResourceQuota only validates. For each quota that matches the Pod, it calls the Pod evaluator’s Constraints (pkg/quota/v1/evaluator/core/pods.go line 129), which walks spec.containers (line 153) and then spec.initContainers (line 156) and collects every container missing a resource the quota lists. The plugin wraps the result as failed quota: <name>: <err> (staging/src/k8s.io/apiserver/pkg/admission/plugin/resourcequota/controller.go line 471). Only if every container passes does it add the Pod’s usage and compare it with spec.hard, which is where exceeded quota comes from (line 622).

Only six quota resources trigger the check, listed in validationSet (line 103) under a comment that calls the requirement “a mistake” kept for backward compatibility: cpu, memory, requests.cpu, requests.memory, limits.cpu and limits.memory. A quota on ephemeral-storage, pods, GPUs or object counts never produces this message.

What each container must declare:

The quota listsEach container and init container needsSatisfied by
limits.cpu / limits.memorya limit for that resourcea limit, or a LimitRange default
requests.cpu / requests.memory, or bare cpu / memorya request for that resourcea request, or a limit (see below), or a LimitRange defaultRequest

A limit on its own satisfies a requests quota because the API server copies limits into missing requests before admission runs (SetDefaults_Pod, pkg/apis/core/v1/defaults.go line 164). The reverse is not true: requests never become limits. A container with only requests, in the same namespace:

Error from server (Forbidden): error when creating "web-requests.yaml": pods "web-requests" is forbidden: failed quota: compute-quota: must specify limits.cpu for: app; limits.memory for: app

The message used to hide the container names

Before Kubernetes 1.24 the same check ended in a bare, comma-joined list of resources, must specify limits.cpu,limits.memory,requests.cpu,requests.memory, with no container names (strings.Join(missingSet.List(), ","), pods.go line 138 on release-1.22 and release-1.23). That is the form most older answers online quote. Init containers were checked then too, which is why “I set limits on every container and it still fails” kept coming up: the container it meant was an init container, and nothing said so.

kubernetes/kubernetes#107210 (“add container name when violate quota constraints”, merged 10 February 2022, milestone v1.24) changed it to the per-container form. Resources are sorted, container names within each are sorted, and they are joined with ; . The format is unchanged on every release branch from release-1.24 to release-1.37 (line 170 of pods.go on the last three).

Fixing it

1. Add limits to every container, including init containers

The direct fix. Give each name in the message the resources it lists:

initContainers:
- name: migrate
  image: busybox:1.36
  command: ["sh", "-c", "echo migrated"]
  resources:
    requests: {cpu: 100m, memory: 64Mi}
    limits:   {cpu: 200m, memory: 64Mi}
- name: log-shipper
  image: busybox:1.36
  command: ["sleep", "infinity"]
  restartPolicy: Always
  resources:
    requests: {cpu: 50m, memory: 32Mi}
    limits:   {cpu: 100m, memory: 32Mi}

With the app container unchanged, the same namespace now takes it:

kubectl create -f web-init.yaml --dry-run=server
pod/web created (server dry run)

Size init containers for what they do, not by copying the app’s numbers: a native sidecar is charged against every ordinary init container listed after it, so generous init limits can turn this error into an exceeded quota one. That order effect is worked through in the order of your init containers changes your Pod’s CPU request.

2. Add a LimitRange with defaults

If you own the namespace, a LimitRange makes the error go away for everything that forgets, because it runs before the quota:

apiVersion: v1
kind: LimitRange
metadata:
  name: container-defaults
spec:
  limits:
  - type: Container
    defaultRequest:
      cpu: 100m
      memory: 128Mi
    default:
      cpu: 500m
      memory: 256Mi

The bare Pod that was rejected at the top of this article is now admitted, and LimitRanger records what it did in an annotation:

kubectl apply -f web.yaml
kubectl get pod web -o jsonpath='{.metadata.annotations.kubernetes\.io/limit-ranger}{"\n"}{.spec.containers[0].resources}{"\n"}'
pod/web created
LimitRanger plugin set: cpu, memory request for container app; cpu, memory limit for container app
{"limits":{"cpu":"500m","memory":"256Mi"},"requests":{"cpu":"100m","memory":"128Mi"}}

Init containers and sidecars get the same treatment. The rejected web-init.yaml through a server-side dry run:

LimitRanger plugin set: cpu, memory request for init container migrate; cpu, memory limit for init container migrate; cpu, memory request for init container log-shipper; cpu, memory limit for init container log-shipper

You do not have to redeploy, but you may want to. The ReplicaSet controller requeues a failed sync with client-go’s default backoff, which starts at 5 ms and doubles on every failure up to 1000 seconds (replica_set.go line 215 and 641; DefaultTypedControllerRateLimiter, util/workqueue/default_rate_limiters.go line 52). Caught early, the Deployment recovers on its own: the first one here went from 0/2 to 2/2 within 20 seconds of the LimitRange being applied.

One left failing for eleven minutes had spaced its attempts 164, 328 and then 655 seconds apart. After the LimitRange went in, it sat at 0/2 for another 10 minutes 37 seconds before the next attempt succeeded. kubectl rollout restart creates a new ReplicaSet, which has no backoff history and tries at once:

kubectl rollout restart deployment/api
kubectl get deploy api
deployment.apps/api restarted
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
api    2/2     2            2           18m

Three things to know before you rely on it:

  • Set defaultRequest explicitly. A LimitRange with only default copies it into defaultRequest, and one with only max copies max into both (SetDefaults_LimitRangeItem, pkg/apis/core/v1/defaults.go line 360 on release-1.36, 402 on release-1.37). A max-only range of 2 CPU turned an unsized container into this:

    {"limits":{"cpu":"2","memory":"1Gi"},"requests":{"cpu":"2","memory":"1Gi"}}
    
  • Defaults fill the quota. Every defaulted container is charged at the default. With a 500m default limit and limits.cpu: 4, the eighth replica was the last:

    Error creating: pods "web-789dc4db69-f68q6" is forbidden: exceeded quota: compute-quota, requested: limits.cpu=500m, used: limits.cpu=4, limited: limits.cpu=4
    

    That is the sibling error. It means the containers are declared and the namespace is full: requested is what this Pod asks for, used what is already charged, limited the hard cap. Lower the requests, raise the quota, or scale something else down. If requested is higher than the sum of your containers, it is the sidecar order effect in the article linked above.

  • It fights pod-level resources. A container LimitRange still stamps its defaults onto containers in a Pod that sets spec.resources, and a container default above the Pod’s own limit is invalid:

    The Pod "podlevel" is invalid: spec.resources.containers[0][cpu].limits: Invalid value: "500m": must be less than or equal to pod limits of 300m
    

3. Change what the quota asks for

If limits are not what you want to govern, take limits.cpu and limits.memory out of the quota. That does not remove the requirement, it moves it: a requests-only quota still rejects a container with nothing set.

Error from server (Forbidden): error when creating "web.yaml": pods "web" is forbidden: failed quota: requests-only: must specify requests.cpu for: app; requests.memory for: app

What it does change is that a request or a limit now satisfies it, and requests are what the scheduler and most capacity planning use anyway. The difference between the two, down to the cgroup, is in CPU requests vs limits.

Scopes change which Pods a quota applies to, and the result is not intuitive. A quota with scopes: ["NotBestEffort"] ignores Pods with no requests or limits at all, so the completely unsized Pod is admitted while one that sets only requests is rejected:

pod/web created (server dry run)
Error from server (Forbidden): error when creating "web-requests.yaml": pods "web-requests" is forbidden: failed quota: compute-quota: must specify limits.cpu for: app; limits.memory for: app

4. Pod-level resources skip the check entirely

With the PodLevelResources feature (beta and on by default since 1.34, still beta in 1.37), a Pod can set spec.resources once instead of per container. If it does, Constraints returns before looking at any container (pods.go line 143), and the quota charges the pod-level figures:

spec:
  resources:
    requests: {cpu: 200m, memory: 256Mi}
    limits:   {cpu: "1", memory: 512Mi}
  initContainers:
  - name: migrate
    image: busybox:1.36
    command: ["sh", "-c", "echo migrated"]
  containers:
  - name: app
    image: nginx:1.27
pod/podlevel created
NAME            REQUEST                                            LIMIT                                       AGE
compute-quota   requests.cpu: 200m/2, requests.memory: 256Mi/2Gi   limits.cpu: 1/4, limits.memory: 512Mi/4Gi   4s

For quota owners, the skip is broader than it looks. The test is IsPodLevelResourcesSet, which is true if any one pod-level CPU or memory value is set. A Pod with only spec.resources.requests.cpu: 200m, and nothing on its container, was admitted into the same namespace. It added its 200m to requests.cpu and nothing at all to limits.cpu, limits.memory or requests.memory:

pod/podlevel-requests created
NAME            REQUEST                                            LIMIT                                       AGE
compute-quota   requests.cpu: 400m/2, requests.memory: 256Mi/2Gi   limits.cpu: 1/4, limits.memory: 512Mi/4Gi   4s

Same result on 1.37.0. If you use a limits.* quota to force every workload to declare limits, a one-line spec.resources now gets around it. Enforce “must have limits” with a validating admission policy instead, and keep the quota for the arithmetic.

Preventing it

  • Ship a LimitRange with every compute quota, with default and defaultRequest both set. A quota without one turns every forgotten field into a failed rollout.

  • Test Pods, not Deployments, against the namespace. kubectl apply --dry-run=server on a Deployment passes, because the quota only sees Pods. A server-side dry run of the Pod spec goes through ResourceQuota like a real create and returns the exact message.

  • Let CI fail on the rollout. kubectl rollout status exits non-zero once the Deployment passes progressDeadlineSeconds (600 by default). With it set to 30:

    Waiting for deployment "api" rollout to finish: 0 out of 2 new replicas have been updated...
    error: deployment "api" exceeded its progress deadline
    

    after which the Deployment shows Progressing False ProgressDeadlineExceeded next to ReplicaFailure True FailedCreate.

  • Review init containers as carefully as the app. Charts often template resources for the main container and forget initContainers, and a logging or proxy sidecar added later needs its own resources block too.

When a Deployment sits at 0/2 on half-empty nodes, look at the namespace quota. KubeGlance is a native Kubernetes client for iPhone and iPad, with a full Mac app on the same core; its Capacity view shows every quota dimension next to the LimitRange defaults that fill it, straight from your kubeconfig.

Get KubeGlance
#resourcequota #limitrange #limits #admission #init-containers #deployments

The Kubernetes dashboard that fits in your pocket

KubeGlance is a native Kubernetes client for iPhone and iPad — the real dashboard, not a companion — with a full Mac app on the same core. Pods, workloads, logs and events, straight from your kubeconfig.

Download KubeGlance