Kubernetes在Debian上的性能调优需围绕节点基础配置、内核参数、容器运行时、调度策略、Pod配置、网络与存储、监控日志等核心环节展开,以下是具体优化措施:
sudo swapoff -a关闭Swap,并修改/etc/fstab文件注释掉Swap条目,永久禁用。通过调整内核参数优化网络、内存、文件系统的性能,创建/etc/sysctl.d/99-kubernetes.conf文件,添加以下配置:
# 网络优化:减少TIME_WAIT连接占用,提升并发处理能力
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_max_syn_backlog = 8096
net.core.somaxconn = 32768
net.core.netdev_max_backlog = 16384
# 内存优化:禁用Swap,允许内存过量使用(需配合Pod limits使用)
vm.swappiness = 0
vm.overcommit_memory = 1
vm.panic_on_oom = 0
# 文件系统优化:增加文件句柄和inotify限制,避免高并发下资源耗尽
fs.file-max = 2097152
fs.inotify.max_user_watches = 524288
fs.inotify.max_user_instances = 8192
执行sudo sysctl --system使配置生效。
/etc/containerd/config.toml配置文件:[plugins."io.containerd.grpc.v1.cri"]
max_concurrent_downloads = 20 # 并行下载镜像数量
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
runtime_type = "io.containerd.runc.v2"
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
SystemdCgroup = true # 使用systemd作为cgroup驱动
重启containerd使配置生效:sudo systemctl restart containerd。修改kubelet配置文件(通常位于/var/lib/kubelet/config.yaml),调整以下参数:
# 资源预留:为kubelet和系统进程预留资源,避免节点资源耗尽
systemReserved:
cpu: "1000m"
memory: "2Gi"
kubeReserved:
cpu: "1000m"
memory: "2Gi"
# 垃圾回收:定期清理未使用的镜像和容器,释放磁盘空间
imageGCHighThresholdPercent: 85 # 镜像使用率达到85%时触发GC
imageGCLowThresholdPercent: 70 # 镜像使用率降至70%时停止GC
serializeImagePulls: false # 并行拉取镜像,提升启动速度
# Pod管理:调整最大Pod数量(根据节点规格调整,如200个/节点)
maxPods: 200
# 日志管理:限制日志文件大小和数量
maxOpenFiles: 1000000
重启kubelet使配置生效:sudo systemctl restart kubelet。
scheduler-config.yaml配置文件调整调度策略,优先将Pod调度到资源利用率低的节点:apiVersion: kubescheduler.config.k8s.io/v1beta1
kind: KubeSchedulerConfiguration
profiles:
- schedulerName: performance-scheduler
plugins:
score:
enabled:
- name: NodeResourcesBalancedAllocation # 资源均衡分配
weight: 1
- name: NodeResourcesLeastAllocated # 优先选择资源使用率低的节点
weight: 2
应用配置:kubectl apply -f scheduler-config.yaml。podAntiAffinity将同一应用的Pod分散到不同节点,避免单节点负载过高:affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- high-performance-app
topologyKey: kubernetes.io/hostname
resources.requests(调度依据)和resources.limits(防止资源争用),例如:resources:
requests:
memory: "512Mi"
cpu: "500m"
limits:
memory: "1Gi"
cpu: "1000m"
JAVA_OPTS环境变量优化内存使用:env:
- name: JAVA_OPTS
value: "-XX:MaxRAMPercentage=75.0 -XX:InitialRAMPercentage=50.0 -XX:+UseG1GC -XX:MaxGCPauseMillis=100"
livenessProbe和readinessProbe,及时清理异常Pod,避免资源浪费:livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
wget https://raw.githubusercontent.com/projectcalico/calico/v3.26.1/manifests/calico.yaml
kubectl apply -f calico.yaml
provisioner和parameters(如type=ssd)。以上优化措施需根据实际业务负载和环境进行调整,建议在测试环境中验证后再应用于生产环境。