Kubernetes requests and limits¶
Kubernetes requests and limits are the resource management mechanisms used to control the compute resources (CPU and memory) allocated to [[Pods|Pods]].^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md]
These definitions are configured within the resources field of a container's specification.^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md]
Requests¶
A request is the amount of resources guaranteed to a container.^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md]
The kube-scheduler uses the request value to determine which Node has sufficient resources to place the Pod^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md]. This practice follows the Borg philosophy, which assumes that jobs often use fewer resources than their maximum allowed capacity, allowing for higher cluster bin-packing density based on these lower request values^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md].
Limits¶
A limit is the maximum amount of resources a container is allowed to use^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md].
The kubelet enforces this value by configuring the container's control groups (cgroups)^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md].
Resource Types¶
Kubernetes categorizes resources based on how they handle resource exhaustion:
- CPU (Compressible Resources): If a container exceeds its CPU limit or if CPU resources are scarce, the Pod's performance degrades ("starvation"), but the process is not terminated^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md].
- Memory (Incompressible Resources): If a container exceeds its memory limit or if the Node runs out of memory, the Pod may be terminated by the kernel via OOM (Out-Of-Memory) killing^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md].
Quality of Service (QoS)¶
Based on the values set for requests and limits, Kubernetes assigns a QoS class to the Pod^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md]. This classification determines the Pod's priority during [[Node]] memory pressure and Eviction processes^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md].
Guaranteed¶
A Pod is classified as Guaranteed if every container in the Pod has both requests and limits set, and the values are identical for both CPU and memory^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md].
This is the highest priority tier. These Pods are only evicted if they exceed their limits or if the Node is under extreme Memory Pressure^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md].
CPU Isolation: When a Pod is Guaranteed and its CPU request equals an integer value, the kubelet may use cpuset to bind the Pod to exclusive CPU cores^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md]. This eliminates CPU context switching overhead and can significantly improve application performance^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md].
Burstable¶
A Pod is classified as Burstable if it does not meet the criteria for Guaranteed, but at least one container has set a request^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md].
BestEffort¶
A Pod is classified as BestEffort if no containers in the Pod have set any requests or limits^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md].
Eviction Priority¶
When the kubelet must reclaim resources, it targets Pods in the following order:
BestEffort < Burstable < Guaranteed^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md]
Configuration Example¶
Resources are specified per container^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md].
apiVersion: v1
kind: Pod
metadata:
name: frontend
spec:
containers:
- name: db
image: mysql
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
Related Concepts¶
- [[Kubernetes Scheduling]]
- [[Eviction]]
- [[cgroups]]
Sources¶
^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes调度机制.md]