How to Set Resource Limits in Kubernetes Deployment
To set resource limits in a Kubernetes
Deployment, define resources.limits and resources.requests under the container spec in the deployment YAML. This controls the maximum and guaranteed CPU and memory your container can use.Syntax
Resource limits are set inside the containers section of a deployment YAML under resources. You specify requests for guaranteed minimum resources and limits for maximum allowed resources.
- requests: Minimum CPU and memory reserved for the container.
- limits: Maximum CPU and memory the container can use.
yaml
apiVersion: apps/v1 kind: Deployment metadata: name: example-deployment spec: replicas: 1 selector: matchLabels: app: example template: metadata: labels: app: example spec: containers: - name: example-container image: nginx resources: requests: memory: "64Mi" cpu: "250m" limits: memory: "128Mi" cpu: "500m"
Example
This example shows a deployment with one container that requests 64Mi memory and 250m CPU, and limits usage to 128Mi memory and 500m CPU. Kubernetes uses these values to schedule and control container resources.
yaml
apiVersion: apps/v1 kind: Deployment metadata: name: resource-limit-demo spec: replicas: 1 selector: matchLabels: app: resource-demo template: metadata: labels: app: resource-demo spec: containers: - name: demo-container image: nginx resources: requests: memory: "64Mi" cpu: "250m" limits: memory: "128Mi" cpu: "500m"
Output
deployment.apps/resource-limit-demo created
Common Pitfalls
Common mistakes when setting resource limits include:
- Not setting
requests, which can cause scheduling issues. - Setting
limitslower thanrequests, which is invalid. - Using incorrect units like missing "m" for CPU millicores or wrong memory units.
- Forgetting to apply the deployment after changes.
yaml
apiVersion: apps/v1 kind: Deployment metadata: name: bad-deployment spec: replicas: 1 selector: matchLabels: app: bad-app template: metadata: labels: app: bad-app spec: containers: - name: bad-container image: nginx resources: requests: memory: "128Mi" cpu: "500m" limits: memory: "64Mi" # Incorrect: limit less than request cpu: "250m" # Incorrect: limit less than request --- # Corrected version apiVersion: apps/v1 kind: Deployment metadata: name: good-deployment spec: replicas: 1 selector: matchLabels: app: good-app template: metadata: labels: app: good-app spec: containers: - name: good-container image: nginx resources: requests: memory: "64Mi" cpu: "250m" limits: memory: "128Mi" cpu: "500m"
Quick Reference
| Field | Description | Example |
|---|---|---|
| resources.requests.cpu | Minimum CPU guaranteed | "250m" (250 millicores) |
| resources.requests.memory | Minimum memory guaranteed | "64Mi" (64 Mebibytes) |
| resources.limits.cpu | Maximum CPU allowed | "500m" (500 millicores) |
| resources.limits.memory | Maximum memory allowed | "128Mi" (128 Mebibytes) |
Key Takeaways
Always set both resource requests and limits in your deployment containers.
Requests reserve resources for scheduling; limits cap maximum usage.
Use correct units: CPU in millicores (m) and memory in Mi or Gi.
Limits must not be lower than requests to avoid errors.
Apply changes with kubectl to enforce resource limits.