Kubernetes
CKAD
Resourcelimitrequestquota

๐Ÿ”‘ What Are requests and limits?

  • requests
  • limits
  • resource quotas
  • limit ranges

Each with practical YAML examples and common use cases.


TermDescription
requestsMinimum resources a container is guaranteed
limitsMaximum resources a container can use

If a container exceeds limits, it may be throttled (cpu) or killed (memory).


๐Ÿงช Real-World Example: Pod with Requests and Limits

apiVersion: v1
kind: Pod
metadata:
  name: app-with-resources
spec:
  containers:
  - name: app
    image: nginx
    resources:
      requests:
        memory: "128Mi"
        cpu: "250m"
      limits:
        memory: "256Mi"
        cpu: "500m"

โœ… What this means:

  • Pod is guaranteed 128Mi memory & 250m CPU.
  • It can use up to 256Mi memory and 500m CPU.
  • If it uses more memory โ†’ it gets OOMKilled.

โš–๏ธ Use Case: Resource Control on Shared Cluster

In multi-tenant clusters, you want:

  • Predictable resource usage
  • Prevent one team from starving others
  • Apply limits to all pods

๐Ÿ” 1. ResourceQuota (Namespace-level enforcement)

apiVersion: v1
kind: ResourceQuota
metadata:
  name: team-a-quota
  namespace: team-a
spec:
  hard:
    requests.cpu: "2"
    requests.memory: "4Gi"
    limits.cpu: "4"
    limits.memory: "8Gi"
    pods: "10"

โœ… Effect: In the team-a namespace:

  • Max of 10 pods
  • Total requests.cpu must not exceed 2 cores
  • Total limits.memory must not exceed 8Gi

๐Ÿšง 2. LimitRange (Enforce defaults and max/min per pod/container)

apiVersion: v1
kind: LimitRange
metadata:
  name: container-defaults
  namespace: dev
spec:
  limits:
  - default:
      cpu: 500m
      memory: 512Mi
    defaultRequest:
      cpu: 200m
      memory: 256Mi
    max:
      cpu: 1
      memory: 1Gi
    min:
      cpu: 100m
      memory: 128Mi
    type: Container

โœ… Effect:

  • If pod does not specify resources โ†’ gets defaults (500m, 512Mi)
  • Cannot request more than 1 CPU or less than 100m
  • Helps standardize resource use

๐Ÿงฉ Example Use Case Summary

Use CaseFeatureExample
Guarantee app gets minimum resourcesrequestscpu: 200m
Prevent app from hogging resourceslimitsmemory: 1Gi
Restrict total resource usageResourceQuotalimits.cpu: 4
Enforce per-pod default/max valuesLimitRangedefault: 500m
Avoid defining requests manuallyLimitRange with defaultRequestAuto-applies

๐Ÿ” View Resource Usage

Check namespace quota:

kubectl describe quota -n team-a

Check LimitRanges:

kubectl get limitrange -n dev

๐Ÿ“ฆ Full Example: LimitRange + Quota + Pod

# 1. Resource Quota
apiVersion: v1
kind: ResourceQuota
metadata:
  name: my-quota
  namespace: myspace
spec:
  hard:
    pods: "5"
    requests.cpu: "1"
    requests.memory: "1Gi"
    limits.cpu: "2"
    limits.memory: "2Gi"
---
# 2. Limit Range
apiVersion: v1
kind: LimitRange
metadata:
  name: defaults
  namespace: myspace
spec:
  limits:
  - default:
      cpu: 500m
      memory: 512Mi
    defaultRequest:
      cpu: 200m
      memory: 256Mi
    type: Container
---
# 3. Pod (No resources set โ€” will use defaults)
apiVersion: v1
kind: Pod
metadata:
  name: app
  namespace: myspace
spec:
  containers:
  - name: nginx
    image: nginx

โœ… Result:

  • Pod will auto inherit 200m request and 500m limit from LimitRange.
  • Quota ensures you donโ€™t exceed total 2 CPU / 2Gi.

๐Ÿง  Tips

TipDescription
๐Ÿ’ก Always define requests and limitsHelps scheduler make better decisions
๐Ÿ“Š Use kubectl top to check usageInstall Metrics Server
โœ… Use LimitRanges in dev namespacesPrevent accidental resource abuse
๐Ÿ” Use Quotas in production namespacesProtect cluster-wide limits

๐Ÿง™ AI Wizard - Instant Page Insights

Click the button below to analyze this page.
Get an AI-generated summary and key insights in seconds.
Powered by Perplexity AI!