CentOS 上 Kubernetes 资源限制设置方法
一 核心概念与单位
二 Pod 与容器级配置
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-demo
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.25
resources:
requests:
cpu: "250m"
memory: "128Mi"
limits:
cpu: "500m"
memory: "256Mi"
apiVersion: v1
kind: Pod
metadata:
name: gpu-demo
spec:
containers:
- name: cuda
image: nvidia/cuda:12.2-base
resources:
limits:
nvidia.com/gpu: 1
三 命名空间级管控
apiVersion: v1
kind: ResourceQuota
metadata:
name: ns-quota
namespace: default
spec:
hard:
pods: "10"
requests.cpu: "4"
requests.memory: "8Gi"
limits.cpu: "8"
limits.memory: "16Gi"
persistentvolumeclaims: "5"
apiVersion: v1
kind: LimitRange
metadata:
name: ns-limit-range
namespace: default
spec:
limits:
- type: Container
default:
cpu: "500m"
memory: "512Mi"
defaultRequest:
cpu: "250m"
memory: "256Mi"
min:
cpu: "100m"
memory: "64Mi"
max:
cpu: "2"
memory: "2Gi"
四 服务质量 QoS 与驱逐优先级
五 验证与常见问题
kubectl get pod <pod> -o yaml | grep -A5 resourceskubectl describe pod <pod>(关注 Events、Last State.Reason 等)kubectl top pod <pod> --containers