Cloud Native 8 min read

Mastering Kubernetes CPU & Memory: Requests, Limits, Quotas, and LimitRanges

This guide explains how to allocate and control CPU and memory for containers and Pods in Kubernetes, covering resource units, request and limit settings, ResourceQuota objects, LimitRange defaults, practical YAML examples, and useful monitoring tools.

Full-Stack DevOps & Kubernetes
Full-Stack DevOps & Kubernetes
Full-Stack DevOps & Kubernetes
Mastering Kubernetes CPU & Memory: Requests, Limits, Quotas, and LimitRanges

Allocate CPU and Memory to Containers and Pods

Kubernetes measures CPU in units that correspond to one vCPU on major cloud providers (AWS, GCP, Azure) or one hyper‑thread on a bare‑metal Intel CPU. Memory is expressed in bytes with suffixes such as Mi for mebibytes.

Define resource requests to guarantee a minimum amount of CPU and memory for a container, and limits to cap the maximum usage. The scheduler uses requests to place Pods, while limits are enforced at runtime.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: aks-application
spec:
  replicas: 1
  selector:
    matchLabels:
      app: aks-application
  template:
    metadata:
      labels:
        app: aks-application
    spec:
      containers:
      - name: aks-application
        image: hubname/aks-application-image:1.0.0
        resources:
          requests:
            cpu: 100m
            memory: 128Mi
          limits:
            cpu: 250m
            memory: 256Mi

This Deployment requests 100 millicores and 128 MiB of memory, while limiting the container to 250 millicores and 256 MiB.

ResourceQuota: Limiting Namespace Consumption

A ResourceQuota object caps the total amount of compute resources a namespace may consume, including the number of objects and aggregate CPU/memory requests.

When a quota is enforced, every Pod creation must specify both request and limit values; otherwise the API server rejects the request.

apiVersion: v1
kind: ResourceQuota
metadata:
  name: backend-storage-quota
spec:
  hard:
    persistentvolumeclaims: "2"
    requests.storage: "1Gi"
---
apiVersion: v1
kind: ResourceQuota
metadata:
  name: backend-namespace-quota
spec:
  hard:
    request.cpu: 400m
    request.memory: 9600Mi
    limits.cpu: 1200m
    limits.memory: 11600Mi

Apply the quota with:

kubectl apply -f resource-quota.yaml --namespace backend

LimitRange: Setting Default Requests and Limits

If a namespace has a quota, a LimitRange can provide default request and limit values for Pods and also enforce minimum and maximum storage sizes for PersistentVolumeClaims.

apiVersion: v1
kind: LimitRange
metadata:
  name: backend-limit-range
spec:
  limits:
  - default:
      memory: 110Mi
      cpu: 500m
    defaultRequest:
      memory: 20Mi
      cpu: 100m
    type: Container
---
apiVersion: v1
kind: LimitRange
metadata:
  name: backend-storage-limits
spec:
  limits:
  - type: PersistentVolumeClaim
    max:
      storage: 5Gi
    min:
      storage: 2Gi

Tools for Managing CPU and Memory

Popeye – scans clusters for configuration, resource, and network issues.

Goldilocks – analyzes Pod resource limits and suggests optimal values.

Kube‑advisor – Azure tool that detects missing resource requests.

K9s + benchmark – CLI for monitoring and benchmarking clusters.

These tools can be combined with observability platforms such as Datadog, Grafana + Prometheus, or Azure Monitor for enhanced visibility.

Summary Checklist

Set resource requests to reflect expected CPU and memory usage.

Define limits and perform load testing to verify behavior under pressure.

Continuously monitor container CPU/memory consumption.

Track PersistentVolume usage.

Use LimitRange to enforce defaults and constraints.

Apply ResourceQuota cautiously; avoid using it in production without thorough testing.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

KubernetesResource ManagementDevOpsCPUMemoryLimitRangeResourceQuota
Full-Stack DevOps & Kubernetes
Written by

Full-Stack DevOps & Kubernetes

Focused on sharing DevOps, Kubernetes, Linux, Docker, Istio, microservices, Spring Cloud, Python, Go, databases, Nginx, Tomcat, cloud computing, and related technologies.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.