How to limit application resources in Kubernetes?
sobes.tech AI
Answer from AI
To limit application resources in Kubernetes, requests (requests) and limits (limits) are used in the Pod or container specification.
The fields resources.requests and resources.limits are used in the container specification:
-
Requests: The minimum amount of resources (CPU or Memory) that the Kubernetes scheduler guarantees to allocate to the container. If the request cannot be fulfilled, the pod will not be scheduled on the node.
-
Limits: The maximum amount of resources (CPU or Memory) that the container can use. If the container exceeds the memory limit, it will be terminated (OOMKill). If the container exceeds the CPU limit, its execution will be limited.
Example of a container specification with requests and limits:
resources:
requests:
memory: "64Mi" # Request 64 megabytes of memory
cpu: "250m" # Request 0.25 CPU (250 millicores)
limits:
memory: "128Mi" # Limit 128 megabytes of memory
cpu: "500m" # Limit 0.5 CPU (500 millicores)
- CPU units: Defined in millicores (m). 1000m = 1 CPU.
- Memory units: Can be in bytes, kilobytes (K, Ki), megabytes (M, Mi), gigabytes (G, Gi), etc. It is recommended to use units with the
iprefix (Ki, Mi, Gi), which denote powers of 2 (Kibibytes, Mebibytes) for consistency.
Without specifying requests and limits, the container can use all available resources on the node, which may lead to stability issues for other pods on that node. Setting requests and limits helps the Kubernetes scheduler efficiently distribute pods across cluster nodes and prevents resource-intensive pods from affecting others.
It is also possible to use ResourceQuotas at the namespace level to limit the total resource consumption of all pods in that namespace.
Example of a ResourceQuota:
apiVersion: v1
kind: ResourceQuota
metadata:
name: pod-limit
spec:
hard:
cpu: "10" # Total CPU limit for the namespace - 10 CPUs
memory: 20Gi # Total Memory limit for the namespace - 20 Gi
pods: "10" # Maximum number of pods in the namespace