一、基础资源限制:requests与limits
在CentOS上运行的Kubernetes集群中,Pod级别的资源限制是最核心的控制手段,通过requests(资源请求)和limits(资源限额)实现。
restartPolicy: Always,则会重启)。示例配置(YAML):
apiVersion: v1
kind: Pod
metadata:
name: resource-limited-pod
spec:
containers:
- name: nginx-container
image: nginx:latest
resources:
requests:
cpu: "250m" # 请求0.25核CPU
memory: "64Mi" # 请求64MB内存
limits:
cpu: "500m" # 限额0.5核CPU
memory: "128Mi" # 限额128MB内存
通过kubectl apply -f pod.yaml应用配置,使用kubectl describe pod resource-limited-pod可验证限制是否生效。
二、命名空间级资源管控:ResourceQuota与LimitRange
为避免单个命名空间过度占用集群资源,需通过ResourceQuota(资源配额)和LimitRange(限制范围)进行集群级管控。
限制命名空间中所有资源的使用总量,包括:
requests/limits总额);示例配置:
apiVersion: v1
kind: ResourceQuota
metadata:
name: ns-resource-quota
namespace: default
spec:
hard:
pods: "10" # 最多运行10个Pod
requests.cpu: "2" # 所有Pod的CPU requests总额不超过2核
requests.memory: "2Gi" # 所有Pod的内存 requests总额不超过2GiB
limits.cpu: "4" # 所有Pod的CPU limits总额不超过4核
limits.memory: "4Gi" # 所有Pod的内存 limits总额不超过4GiB
persistentvolumeclaims: "5" # 最多创建5个PVC
应用后,命名空间内的资源使用将严格受限于此配置。
为命名空间内的单个容器设置默认资源请求/限额及最小/最大边界,解决“未指定资源时Pod无限使用”的问题。
requests/limits,自动注入default字段的值;示例配置:
apiVersion: v1
kind: LimitRange
metadata:
name: ns-limit-range
namespace: default
spec:
limits:
- type: Container # 作用于容器
default:
cpu: "500m" # 默认CPU限额0.5核
memory: "512Mi" # 默认内存限额512MB
defaultRequest:
cpu: "250m" # 默认CPU请求0.25核
memory: "256Mi" # 默认内存请求256MB
min:
cpu: "100m" # 单个容器最小CPU0.1核
memory: "64Mi" # 单个容器最小内存64MB
max:
cpu: "1" # 单个容器最大CPU1核
memory: "1Gi" # 单个容器最大内存1GiB
应用后,命名空间内的容器将自动遵守这些规则。
三、节点级资源调度优化
除Pod/命名空间级限制外,还可通过节点亲和性和污点/容忍优化资源调度,提升集群资源利用率。
将Pod调度到满足特定资源条件的节点,例如“仅调度到有GPU的节点”:
apiVersion: v1
kind: Pod
metadata:
name: gpu-pod
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname # 根据节点名称筛选
operator: In
values:
- gpu-node-01
containers:
- name: cuda-container
image: nvidia/cuda:11.2.0-base
resources:
limits:
nvidia.com/gpu: 1 # 请求1个GPU
通过污点标记节点(如“此节点为系统节点,禁止普通Pod调度”),再通过容忍允许特定Pod调度到该节点:
kubectl taint nodes gpu-node-01 dedicated=gpu:NoSchedule
apiVersion: v1
kind: Pod
metadata:
name: gpu-toleration-pod
spec:
tolerations:
- key: "dedicated"
operator: "Equal"
value: "gpu"
effect: "NoSchedule"
containers:
- name: gpu-container
image: nvidia/cuda:11.2.0-base
这些策略可避免资源竞争,确保关键应用获得足够的资源。
四、自动伸缩优化
结合水平Pod自动伸缩(HPA)和垂直Pod自动伸缩(VPA),根据资源使用情况动态调整Pod资源,提升集群弹性。
根据CPU/内存利用率自动调整Pod副本数,例如“当CPU利用率超过50%时,自动扩容Pod至10个”:
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: nginx-hpa
namespace: default
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx-deployment
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
根据Pod历史资源使用情况,自动调整Pod的requests/limits,例如“将某Pod的CPU限额从500m调整至1核”:
# 安装VPA控制器(需提前部署)
kubectl apply -f https://github.com/kubernetes/autoscaler/releases/download/vertical-pod-autoscaler-0.13.0/vertical-pod-autoscaler.yaml
# 创建VPA对象
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
name: nginx-vpa
namespace: default
spec:
targetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx-deployment
updatePolicy:
updateMode: "Auto" # 自动更新requests/limits
自动伸缩可有效应对流量波动,避免资源浪费或不足。
五、资源监控与告警
通过Prometheus+Grafana监控集群资源使用情况,设置告警规则(如“节点内存使用率超过80%时告警”),及时发现资源瓶颈。
监控是资源管理的重要环节,可帮助管理员快速定位问题并调整资源策略。