温馨提示×

centos部署k8s如何优化

小樊
44
2026-07-21 08:04:27
栏目: 智能运维

CentOS 上部署 Kubernetes(k8s) 时,优化通常从 系统层、容器运行时、k8s 组件、网络、存储、资源调度 等多个层面来进行。下面给你一套 生产环境常用、可落地的优化方案,按重要程度递进。


一、系统层优化(必做)

1️⃣ 内核参数优化(网络 & 性能)

cat > /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-iptables  = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward                 = 1
net.ipv4.tcp_tw_reuse               = 1
net.ipv4.tcp_fin_timeout            = 30
net.core.somaxconn                  = 65535
net.ipv4.ip_local_port_range        = 1024 65535
vm.swappiness                       = 0
vm.overcommit_memory                = 1
EOF

sysctl --system

2️⃣ 关闭 swap(k8s 强制要求)

swapoff -a
sed -i '/swap/s/^/#/' /etc/fstab

3️⃣ 关闭 SELinux

setenforce 0
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

4️⃣ 配置时间同步(非常重要)

yum install -y chrony
systemctl enable --now chronyd

二、容器运行时优化(containerd / docker)

✅ 推荐:containerd(性能更好)

开启 SystemdCgroup

# /etc/containerd/config.toml
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
  SystemdCgroup = true
systemctl restart containerd

镜像加速(国内环境)

# /etc/containerd/config.toml
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
  endpoint = ["https://mirror.ccs.tencentyun.com"]

三、Kubernetes 部署优化

1️⃣ 使用 kubeadm 最佳实践

kubeadm init \
  --pod-network-cidr=10.244.0.0/16 \
  --kubernetes-version=v1.28.x \
  --control-plane-endpoint=LOAD_BALANCER:6443

2️⃣ kubelet 参数优化(核心)

# /var/lib/kubelet/config.yaml
evictionHard:
  memory.available: "200Mi"
  nodefs.available: "10%"
  imagefs.available: "10%"

maxPods: 110
systemctl restart kubelet

3️⃣ 调整 kube-apiserver 性能

--max-requests-inflight=3000
--max-mutating-requests-inflight=1000
--watch-cache-sizes=services,pods=1000

四、网络优化(常见瓶颈)

✅ 推荐网络方案

场景 推荐
性能优先 Calico(BGP)
简单稳定 Flannel
大规模 Cilium

Calico 优化示例

# 开启 IPIP 或 BGP
CALICO_IPV4_POOL_IPIP=Always
# 关闭 MTU 探测异常
MTU=1450

五、存储优化

1️⃣ 使用本地 SSD(高 IO)

hostPath / local PV

2️⃣ Ceph / NFS 调优

  • Ceph:开启 rbd cache
  • NFS:
mount -o nfsvers=4.1,proto=tcp,rsize=1048576,wsize=1048576

六、资源调度 & QoS 优化

1️⃣ 设置 Request / Limit(必须)

resources:
  requests:
    cpu: 100m
    memory: 128Mi
  limits:
    cpu: 500m
    memory: 512Mi

2️⃣ 使用 PriorityClass

apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
value: 1000000
preemptionPolicy: PreemptLowerPriority

七、监控 & 自动伸缩

必装组件

组件 作用
Prometheus 监控
Grafana 可视化
HPA 自动扩缩容
VPA 资源推荐

八、CentOS 特定注意事项

⚠️ CentOS 7 已 EOL

强烈建议

  • 升级到 AlmaLinux / Rocky Linux
  • 或至少使用 CentOS Stream 8/9

九、快速优化检查清单 ✅

✅ swap off
✅ SELinux disabled
✅ 内核参数优化
✅ containerd + systemd cgroup
✅ 镜像加速
✅ kubelet 资源限制
✅ Request / Limit 设置
✅ 网络方案稳定
✅ 监控部署


十、我可以继续帮你

你可以直接告诉我:

1️⃣ k8s 版本
2️⃣ 节点规模(几 master / 几 worker)
3️⃣ 是否生产环境
4️⃣ 主要跑什么业务(Java / Web / AI / 大数据)

我可以直接给你一份 针对你环境的 k8s 优化配置文件模板

0