资源限制与 QoS
约 582 字大约 2 分钟
布欧-Lewyon
2026-05-15
首页 › K8s › 调度与资源(在新窗口打开) › 资源限制与 QoS
requests 与 limits
| 字段 | 用途 | 调度依据 | 运行时限制 |
|---|---|---|---|
requests | 调度时确保节点有足够资源 | ✅ | ❌ |
limits | 运行时限制最大使用 | ❌ | ✅(OOM Kill / CPU 节流) |
spec:
containers:
- name: app
image: nginx:alpine
resources:
requests:
cpu: 100m # 调度时至少需要 0.1 核
memory: 128Mi # 调度时至少需要 128MB
limits:
cpu: 500m # 最多使用 0.5 核(超过则节流)
memory: 256Mi # 最多使用 256MB(超过则 OOM Kill)CPU 单位
| 写法 | 等价 | 说明 |
|---|---|---|
1 | 1000m | 1 个 CPU 核心 |
0.5 | 500m | 半核 |
100m | 0.1 | 0.1 核 |
内存单位
| 写法 | 字节 |
|---|---|
128Mi | 128 × 1024 × 1024 |
256M | 256 × 1000 × 1000 |
1Gi | 1024³ |
512Ki | 512 × 1024 |
QoS 等级
K8s 根据 Pod 的资源请求和限制将 Pod 分为三个 QoS 等级:
Guaranteed
所有容器都设置了 requests == limits:
resources:
requests:
cpu: 500m
memory: 256Mi
limits:
cpu: 500m
memory: 256MiBurstable
至少一个容器设置了 requests 或 limits,但不满足 Guaranteed:
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 256MiBestEffort
没有设置任何 requests 和 limits:
# 没有 resources 字段资源回收顺序
当节点资源不足时,按此顺序终止 Pod:
- BestEffort 先被驱逐
- Burstable 中超出 requests 的 Pod
- Guaranteed 最后被驱逐
查看资源使用
# 安装 Metrics Server
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
# 查看节点资源
kubectl top nodes
# NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
# minikube 250m 12% 1024Mi 52%
# 查看 Pod 资源
kubectl top pods
kubectl top pods --containers # 按容器查看LimitRange
LimitRange 限制 Namespace 内 Pod/容器的资源默认值:
apiVersion: v1
kind: LimitRange
metadata:
name: mem-limit-range
spec:
limits:
- default:
memory: 512Mi
cpu: 500m
defaultRequest:
memory: 256Mi
cpu: 100m
type: ContainerResourceQuota
ResourceQuota 限制 Namespace 的总资源上限:
apiVersion: v1
kind: ResourceQuota
metadata:
name: compute-quota
spec:
hard:
requests.cpu: 2
requests.memory: 4Gi
limits.cpu: 4
limits.memory: 8Gi
pods: "10"kubectl describe quota compute-quota小结
requests用于调度,limits用于运行时限制。- CPU 是压缩资源(超额时节流),内存是非压缩资源(超额时 OOM Kill)。
- QoS 等级:Guaranteed > Burstable > BestEffort。
kubectl top查看实时资源使用(需 Metrics Server)。- LimitRange 设默认值,ResourceQuota 设 Namespace 总上限。
上一节:Volume 与 PV/PVC 下一节:调度策略
