Resource Requirements and Limits
Concept of resource requirements and limits
Every node has its own capacity of resources like CPU and memory. When you deploy a pod to a node, the pod is consuming the resources of the node. Now, we know that kube-scheduler is responsible for scheduling the pods to the nodes. Therefore, kube-scheduler will identify the node with the required resources to deploy the pod, if the node does not have the required resources, the pod will not be scheduled (pending state) to that node.
# See the events of the pod --> it will show insufficient resources
kubectl describe pods <pod-name>
Resource requests
The resource requests of a container are the minimum amount of CPU or memory requested by the container. That means, the pod will guarantee get the amount of resources that requested. By default, every pod has a default request of 0.5 CPU and 256MB of memory.
For the CPU request, you can specify the value in milli (m) or CPU. For example, 0.1 CPU = 100m (milli). Now remember, 1 CPU = 1 vCPU (AWS) = 1 Core (GCP, Azure) = 1 Hyperthread
For the memory request, you can specify the value in bytes or binary (Ki, Mi, Gi, Ti).
Binary | Bytes |
---|---|
1G (Gigabyte) | 1,000,000,000 |
1M (Megabyte) | 1,000,000 |
1K (Kilobyte) | 1,000 |
1Gi (Gibibyte) | 1,073,741,824 |
1Mi (Mebibyte) | 1,048,576 |
1Ki (Kibibyte) | 1,024 |
apiVersion: v1
kind: Pod
metadata:
name: sample-pod
spec:
containers:
- name: sample-container
image: ubuntu
resources:
requests:
memory: "2Gi"
cpu: 2
Resource limits
Resource limits of a container are the maximum amount of CPU or memory that the container can use. That means, the pod will not be able to consume more resources than the specified limit. If you do not specify the limit, the pod will be able to consume as much as it wants. By default, every pod has a default limit of 1 CPU and 512MB of memory.
If the pod tries to consume more resources than the specified limit, what will happen?
- For the CPU, the system will throttle (control) the CPU usage of the container so that it does not go beyond the specified limit.
- For the memory, the system will kill the container if it consumes more memory (OOM - Out of memory error) than the specified limit.
apiVersion: v1
kind: Pod
metadata:
name: sample-pod
spec:
containers:
- name: sample-container
image: ubuntu
resources:
requests:
memory: "2Gi"
cpu: 2
limits:
memory: "4Gi"
cpu: 4
Resource quota
Refer to Resource quota for namespace for more information.
Limit range
Refer to Limit range for namespace for more information.