Implement Kubernetes resource quotas and limits for namespace isolation and workload management

Intermediate 25 min Apr 04, 2026 167 views
Ubuntu 24.04 Debian 12 AlmaLinux 9 Rocky Linux 9

Configure namespace resource quotas, container resource limits, and limit ranges to ensure fair resource allocation and prevent resource exhaustion in multi-tenant Kubernetes clusters.

Prerequisites

  • Running Kubernetes cluster
  • kubectl configured with admin permissions
  • Basic understanding of Kubernetes concepts

What this solves

Kubernetes resource quotas and limits prevent individual workloads from consuming excessive cluster resources and ensure fair resource allocation across namespaces. Without proper resource management, a single workload can starve other applications of CPU, memory, or storage, leading to cluster-wide performance issues and potential outages.

Step-by-step configuration

Install and verify Kubernetes cluster

Ensure you have a running Kubernetes cluster with proper administrative access. This tutorial assumes you have kubectl configured and cluster admin permissions.

kubectl cluster-info
kubectl get nodes
kubectl auth can-i create resourcequotas --all-namespaces

Create test namespaces for demonstration

Create separate namespaces to demonstrate resource isolation and quota enforcement. These namespaces will simulate different teams or applications in your cluster.

kubectl create namespace production
kubectl create namespace development
kubectl create namespace testing

Configure namespace resource quotas

Create resource quotas to limit total resource consumption at the namespace level. This prevents any namespace from consuming more resources than allocated.

apiVersion: v1
kind: ResourceQuota
metadata:
  name: production-quota
  namespace: production
spec:
  hard:
    requests.cpu: "4"
    requests.memory: 8Gi
    limits.cpu: "8"
    limits.memory: 16Gi
    persistentvolumeclaims: "10"
    pods: "20"
    services: "10"
    secrets: "20"
    configmaps: "20"

Create development and testing quotas

Configure smaller resource quotas for development and testing environments to ensure production workloads get priority access to cluster resources.

apiVersion: v1
kind: ResourceQuota
metadata:
  name: development-quota
  namespace: development
spec:
  hard:
    requests.cpu: "2"
    requests.memory: 4Gi
    limits.cpu: "4"
    limits.memory: 8Gi
    persistentvolumeclaims: "5"
    pods: "10"
    services: "5"
apiVersion: v1
kind: ResourceQuota
metadata:
  name: testing-quota
  namespace: testing
spec:
  hard:
    requests.cpu: "1"
    requests.memory: 2Gi
    limits.cpu: "2"
    limits.memory: 4Gi
    persistentvolumeclaims: "3"
    pods: "5"
    services: "3"

Apply resource quotas to namespaces

Deploy the resource quota configurations to enforce limits on namespace resource consumption.

kubectl apply -f production-quota.yaml
kubectl apply -f development-quota.yaml
kubectl apply -f testing-quota.yaml

Create limit ranges for pod and container constraints

Configure LimitRange objects to set default resource requests and limits for containers, and enforce minimum and maximum boundaries per pod.

apiVersion: v1
kind: LimitRange
metadata:
  name: production-limits
  namespace: production
spec:
  limits:
  - type: Container
    default:
      cpu: "500m"
      memory: "1Gi"
    defaultRequest:
      cpu: "100m"
      memory: "256Mi"
    min:
      cpu: "50m"
      memory: "128Mi"
    max:
      cpu: "2"
      memory: "4Gi"
  - type: Pod
    max:
      cpu: "4"
      memory: "8Gi"
  - type: PersistentVolumeClaim
    min:
      storage: "1Gi"
    max:
      storage: "100Gi"

Create limit ranges for development and testing

Configure smaller limit ranges for non-production environments to prevent resource waste and encourage efficient resource usage during development.

apiVersion: v1
kind: LimitRange
metadata:
  name: development-limits
  namespace: development
spec:
  limits:
  - type: Container
    default:
      cpu: "200m"
      memory: "512Mi"
    defaultRequest:
      cpu: "50m"
      memory: "128Mi"
    min:
      cpu: "25m"
      memory: "64Mi"
    max:
      cpu: "1"
      memory: "2Gi"
  - type: Pod
    max:
      cpu: "2"
      memory: "4Gi"
apiVersion: v1
kind: LimitRange
metadata:
  name: testing-limits
  namespace: testing
spec:
  limits:
  - type: Container
    default:
      cpu: "100m"
      memory: "256Mi"
    defaultRequest:
      cpu: "25m"
      memory: "64Mi"
    min:
      cpu: "10m"
      memory: "32Mi"
    max:
      cpu: "500m"
      memory: "1Gi"
  - type: Pod
    max:
      cpu: "1"
      memory: "2Gi"

Apply limit ranges

Deploy the limit range configurations to enforce container and pod resource constraints within each namespace.

kubectl apply -f production-limits.yaml
kubectl apply -f development-limits.yaml
kubectl apply -f testing-limits.yaml

Create test deployment with resource specifications

Deploy a sample application to demonstrate how resource limits and requests work in practice. This deployment explicitly defines resource requirements.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-test
  namespace: production
  labels:
    app: nginx-test
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-test
  template:
    metadata:
      labels:
        app: nginx-test
    spec:
      containers:
      - name: nginx
        image: nginx:1.25
        ports:
        - containerPort: 80
        resources:
          requests:
            cpu: "100m"
            memory: "256Mi"
          limits:
            cpu: "500m"
            memory: "1Gi"
        livenessProbe:
          httpGet:
            path: /
            port: 80
          initialDelaySeconds: 30
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /
            port: 80
          initialDelaySeconds: 5
          periodSeconds: 5

Deploy test application

Apply the test deployment to verify that resource quotas and limits are properly enforced by the cluster.

kubectl apply -f test-deployment.yaml
kubectl get pods -n production
kubectl describe deployment nginx-test -n production

Create resource monitoring script

Set up a monitoring script to track resource usage across namespaces and identify potential quota violations or resource pressure.

#!/bin/bash

echo "=== Namespace Resource Quotas ==="
for namespace in production development testing; do
    echo "\n--- $namespace ---"
    kubectl describe quota -n $namespace 2>/dev/null || echo "No quota found"
done

echo "\n\n=== Namespace Resource Usage ==="
for namespace in production development testing; do
    echo "\n--- $namespace ---"
    kubectl top pods -n $namespace --no-headers 2>/dev/null | \
    awk '{cpu+=$2; mem+=$3} END {printf "Total CPU: %.0fm, Total Memory: %.0fMi\n", cpu, mem}' || echo "No pods running"
done

echo "\n\n=== Limit Ranges ==="
for namespace in production development testing; do
    echo "\n--- $namespace ---"
    kubectl get limitrange -n $namespace -o wide 2>/dev/null || echo "No limit ranges"
done
chmod 755 monitor-resources.sh

Monitor and troubleshoot resource constraints

Monitor resource quota usage

Check current resource consumption against configured quotas to identify namespaces approaching their limits and plan capacity accordingly.

kubectl describe quota production-quota -n production
kubectl describe quota development-quota -n development
kubectl get resourcequota --all-namespaces

Analyze pod resource consumption

Monitor actual resource usage by pods to optimize resource requests and limits based on real consumption patterns.

kubectl top pods --all-namespaces
kubectl top pods -n production --sort-by=cpu
kubectl top pods -n production --sort-by=memory

Test quota enforcement

Create a deployment that exceeds namespace quotas to verify that resource constraints are properly enforced by the cluster.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: resource-hog
  namespace: testing
spec:
  replicas: 10
  selector:
    matchLabels:
      app: resource-hog
  template:
    metadata:
      labels:
        app: resource-hog
    spec:
      containers:
      - name: stress
        image: nginx:1.25
        resources:
          requests:
            cpu: "200m"
            memory: "512Mi"
kubectl apply -f quota-test.yaml
kubectl get events -n testing --sort-by=.metadata.creationTimestamp

Verify your setup

# Check all resource quotas
kubectl get resourcequota --all-namespaces

Check all limit ranges

kubectl get limitrange --all-namespaces

Verify quota status

kubectl describe quota --all-namespaces

Check pod resource usage

kubectl top pods --all-namespaces

Run monitoring script

./monitor-resources.sh
Note: Resource quotas only take effect for new pods created after the quota is applied. Existing pods are not affected by quota changes.

Common issues

SymptomCauseFix
Pods stuck in Pending stateResourceQuota exceededCheck quota usage with kubectl describe quota -n namespace and adjust limits or delete unused resources
"exceeded quota" error when creating podsNamespace resource quota limits reachedIncrease quota limits or reduce resource requests in pod specifications
Containers using default resource limitsNo resource requests/limits specified in pod specLimitRange applies defaults automatically, or explicitly set resources in deployment
Resource quota not enforcedQuota applied after pod creationResource quotas only affect new pods, restart existing deployments to apply quotas
OOMKilled container restartsMemory limit set too lowIncrease memory limits or optimize application memory usage
CPU throttling affects performanceCPU limit set too restrictivelyIncrease CPU limits or optimize application CPU usage patterns

Next steps

Automated install script

Run this to automate the entire setup

Need help?

Don't want to manage this yourself?

We handle managed devops services for businesses that depend on uptime. From initial setup to ongoing operations.