CentOS 下 Kubernetes 资源分配实践指南
一 核心概念与 QoS
二 在 Pod 与容器上设置资源
apiVersion: v1
kind: Pod
metadata:
name: demo
spec:
containers:
- name: app
image: nginx:1.25
resources:
requests:
cpu: "250m"
memory: "128Mi"
limits:
cpu: "500m"
memory: "256Mi"
# 默认请求/限制(未显式设置时使用)
apiVersion: v1
kind: LimitRange
metadata:
name: default-lr
namespace: default
spec:
limits:
- default:
cpu: "200m"
memory: "256Mi"
defaultRequest:
cpu: "100m"
memory: "128Mi"
type: Container
# 命名空间总配额(防止团队/项目超额)
apiVersion: v1
kind: ResourceQuota
metadata:
name: ns-quota
namespace: default
spec:
hard:
pods: "20"
requests.cpu: "4"
requests.memory: "8Gi"
limits.cpu: "8"
limits.memory: "16Gi"
kubectl describe node <node-name>kubectl get resourcequota -n <ns> -o yamlkubectl get pod <pod> -o yaml三 调度与拓扑优化
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: disk
operator: In
values: ["ssd"]
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: app
operator: In
values: ["nginx"]
topologyKey: kubernetes.io/hostname
四 弹性伸缩与集群规模
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: hpa-nginx
namespace: default
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 60
kubectl top 与 HPA 指标不可用。五 监控 容量规划与 GPU 特殊资源
kubectl top nodes 与 kubectl describe node 校准 requests/limits 与实际使用。resources.limits: { nvidia.com/gpu: "1" }。