CentOS 上 Kubernetes 资源分配实操指南
一 核心概念与 QoS
二 节点与 kubelet 资源配置
swapoff -a 并注释 /etc/fstab 中的 swap 行)、设置 vm.swappiness=0;启用桥接流量进入 iptables/IP6tables(net.bridge.bridge-nf-call-iptables=1、net.bridge.bridge-nf-call-ip6tables=1);时钟同步(chrony/timedatectl set-ntp true)。三 工作负载资源配置示例
apiVersion: v1
kind: Pod
metadata:
name: app
spec:
containers:
- name: app
image: nginx:1.25
resources:
requests:
memory: "128Mi"
cpu: "250m"
limits:
memory: "256Mi"
cpu: "500m"
apiVersion: v1
kind: ResourceQuota
metadata:
name: team-a-quota
namespace: team-a
spec:
hard:
requests.cpu: "4"
requests.memory: "8Gi"
limits.cpu: "8"
limits.memory: "16Gi"
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: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: app-hpa
namespace: team-a
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: app
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 60
apiVersion: v1
kind: Pod
metadata:
name: gpu-demo
spec:
containers:
- name: cuda
image: nvidia/cuda:12.2.0-base
resources:
limits:
nvidia.com/gpu: 1 # 申请 1 张 GPU
前置:部署 NVIDIA Device Plugin(如 kubectl apply -f https://raw.githubusercontent.com/NVIDIA/k8s-device-plugin/v0.13.0/nvidia-device-plugin.yml)。
四 调度与拓扑优化
maxSkew)让关键服务副本在节点间更均衡,降低单点风险。affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: app
operator: In
values: ["nginx"]
topologyKey: kubernetes.io/hostname
五 容量规划 监控与排障
kubectl describe node <node-name>kubectl top nodes / kubectl top pods -Akubectl describe pod <pod-name> -n <ns>kubectl get resourcequota -n <ns> -o yaml、kubectl get limitrange -n <ns> -o yamlkubectl get hpa <hpa-name> -n <ns>