Feedback

Chat Icon

Cloud-Native Microservices With Kubernetes - 2nd Edition

A Comprehensive Guide to Building, Scaling, Deploying, Observing, and Managing Highly-Available Microservices in Kubernetes

Understanding Resource Management in Kubernetes
42%

Resource Quotas and Limit Ranges

The LimitRange is a Kubernetes resource that defines default CPU and memory requests and limits for all containers in a given Namespace. It helps ensure consistent resource allocation and prevents containers from running without defined limits.

Below is an example LimitRange for the Namespace resource-management. Create it using the following configuration:

kubectl apply -f - << EOF
apiVersion: v1
kind: LimitRange
metadata:
  name: resource-limits
  namespace: resource-management
spec:
  limits:
  - type: Container
    # "default" specifies the default limits
    # for containers that do not have limits set.
    default:
      cpu: "500m"
      memory: "256Mi"
    # "defaultRequest" specifies the default requests
    # for containers that do not have requests set.
    defaultRequest:
      cpu: "250m"
      memory: "128Mi"
EOF

This configuration means that any container created in the resource-management Namespace without explicit resource settings will automatically:

  • Request 256Mi of memory and 250m of CPU
  • Be limited to 512Mi of memory and 500m of CPU

For example, if you create a Pod without specifying resource requests and limits:

# Create a Pod without resource requests and limits
kubectl -n resource-management run demo --image=busybox --restart=Never -- sleep 3600

# Check the resource requests and limits applied to the Pod
kubectl -n resource-management get pod demo \
  -o jsonpath='{.spec.containers[0].resources}' | jq

If you create a Pod in the resource-management Namespace that requests more resources than allowed by the LimitRange, Kubernetes will reject the Pod:

Cloud-Native Microservices With Kubernetes - 2nd Edition

A Comprehensive Guide to Building, Scaling, Deploying, Observing, and Managing Highly-Available Microservices in Kubernetes

Enroll now to unlock all content and receive all future updates for free.