Review Kubernetes namespace LimitRange configuration

Define resource constraints appropriate for individual objects in the namespace.

Description

A LimitRange applies resource bounds to individual Pods, containers or persistent volume claims (PVCs) in a namespace. It can also supply default container requests and limits. Use ResourceQuota to limit aggregate declared requests, limits or object counts.

Potential impact

Without suitable admission policies, workloads can request excessive resources or omit required values. Bounds that are too strict can prevent valid workloads from being created.

Remediation

  • Check existing workload settings and admission policies. Choose which object types and resources need minimums or maximums, and which container requests and limits need defaults. Consider CPU, memory and PVC storage separately.
  • Create a representative workload and inspect the effective values. Review existing Pods separately, because adding a LimitRange does not update them.

Examples

Ensure the mypod namespace exists and use an available, verified image. Adjust the CPU range for your workloads.

Before

yaml
apiVersion: v1
kind: Pod
metadata:
  name: frontend
  namespace: mypod
spec:
  containers:
    - name: app
      image: images.my-company.example/app:v4

This Pod omits resource settings. An existing namespace policy may still supply defaults.

After

yaml
apiVersion: v1
kind: LimitRange
metadata:
  name: cpu-min-max-demo-lr
  namespace: mypod
spec:
  limits:
    - type: Container
      min:
        cpu: "200m"
      max:
        cpu: "800m"
---
apiVersion: v1
kind: Pod
metadata:
  name: frontend
  namespace: mypod
spec:
  containers:
    - name: app
      image: images.my-company.example/app:v4

At admission, the LimitRange checks that the container’s CPU request and limit are between 200m and 800m. Inspect the admitted Pod for its default request and limit. Memory and PVC storage are outside this example.

References