Kubernetes 在 CentOS 上的资源管理技巧
一 基础配置与节点规划
swapoff -a,并在 /etc/fstab 中注释 swap 行,避免 kubelet 异常。net.bridge.bridge-nf-call-ip6tables=1、net.bridge.bridge-nf-call-iptables=1,执行 sysctl -p。二 工作负载资源配置
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: app
image: nginx:alpine
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
kubectl describe pod <pod-name> 查看 Limits/Requests 是否生效。三 命名空间与准入控制
apiVersion: v1
kind: ResourceQuota
metadata:
name: ns-quota
namespace: prod
spec:
hard:
requests.cpu: "4"
requests.memory: "8Gi"
limits.cpu: "8"
limits.memory: "16Gi"
apiVersion: v1
kind: LimitRange
metadata:
name: default-limits
namespace: prod
spec:
limits:
- default:
memory: "512Mi"
cpu: "500m"
defaultRequest:
memory: "256Mi"
cpu: "250m"
type: Container
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
name: high-priority
value: 1000000
globalDefault: false
description: "高优先级业务"
在 Pod 中引用:spec.priorityClassName: high-priority。四 调度与弹性伸缩
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/e2e-az-name
operator: In
values: ["e2e-az1","e2e-az2"]
# 节点打污点
kubectl taint nodes <node> key=value:NoSchedule
# Pod 容忍
tolerations:
- key: "key"
operator: "Equal"
value: "value"
effect: "NoSchedule"
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: app-hpa
namespace: prod
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: app
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 60
updatePolicy.updateMode: "Auto" 实现自动更新(建议与滚动更新配合)。五 监控 日志 与 GPU 管理
resources:
limits:
nvidia.com/gpu: 1
确保节点已安装 NVIDIA 驱动 与容器运行时 GPU 支持。