Understanding Resource Requirements in Kubernetes
🗼Introduction
Managing resources efficiently is crucial in Kubernetes to ensure optimal performance and stability of your applications. Kubernetes provides mechanisms to define resource requests and limits for containers, allowing you to control how much CPU and memory a pod can consume. This blog will dive into the concepts of resource requests, limits, and what happens when these limits are exceeded.
🗼Resource Requests
Resource Request is the amount of CPU and memory that a container is guaranteed to have. When you create a pod, Kubernetes uses the resource requests to schedule the pod on a node that has sufficient resources available. By default, a container may be assigned a CPU request of 0.5 and a memory request of 256Mi, but these defaults must be set up in the namespace using a LimitRange
.
Setting Default Resource Requests
To set default values for resource requests and limits in a namespace, you need to create a LimitRange
. This ensures that any pod created in that namespace will automatically pick up the specified default values.
Example of a LimitRange
:
apiVersion: v1
kind: LimitRange
metadata:
name: default-limits
namespace: your-namespace
spec:
limits:
- default:
cpu: 500m
memory: 256Mi
defaultRequest:
cpu: 250m
memory: 128Mi
type: Container
🗼Resource Limits
Resource Limits define the maximum amount of CPU and memory a container can use. Setting limits prevents a container from consuming too many resources and impacting other containers running on the same node.
Example of setting resource limits in a pod specification:
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: example-container
image: example-image
resources:
requests:
memory: "128Mi"
cpu: "250m"
limits:
memory: "256Mi"
cpu: "500m"
🗼Exceeded Limits
When a pod exceeds its resource limits, Kubernetes handles CPU and memory differently:
CPU Limits
A pod cannot use more CPU resources than its specified limit. If a container tries to exceed its CPU limit, Kubernetes throttles the CPU usage to stay within the defined limit.
Memory Limits
For memory, the behavior is different. A container can use more memory than its request but not more than its limit. If a container tries to consume more memory than its limit, Kubernetes will terminate the pod. This termination occurs with an Out of Memory (OOM) error, which will be recorded in the pod's logs.
🗼Conclusion
Setting appropriate resource requests and limits is essential for ensuring that your Kubernetes cluster runs efficiently and reliably. By configuring these values, you can prevent resource contention and ensure fair resource distribution among all running containers. Proper resource management helps in maintaining application performance and stability, ultimately leading to a more resilient Kubernetes environment.
Subscribe to my newsletter
Read articles from Ashutosh Mahajan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Ashutosh Mahajan
Ashutosh Mahajan
Proficient in variety of DevOps technologies, including AWS, Linux, Shell Scripting, Python, Docker, Terraform, Jenkins and Computer Networking. They have strong ability to troubleshoot and resolve issues and are consistently motivated to expand their knowledge and skills through expantion of new technologies.