在 CentOS 上部署 Kubernetes(k8s) 时,优化通常从 系统层、容器运行时、k8s 组件、网络、存储、资源调度 等多个层面来进行。下面给你一套 生产环境常用、可落地的优化方案,按重要程度递进。
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
swapoff -a
sed -i '/swap/s/^/#/' /etc/fstab
setenforce 0
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
yum install -y chrony
systemctl enable --now chronyd
# /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"]
kubeadm init \
--pod-network-cidr=10.244.0.0/16 \
--kubernetes-version=v1.28.x \
--control-plane-endpoint=LOAD_BALANCER:6443
# /var/lib/kubelet/config.yaml
evictionHard:
memory.available: "200Mi"
nodefs.available: "10%"
imagefs.available: "10%"
maxPods: 110
systemctl restart kubelet
--max-requests-inflight=3000
--max-mutating-requests-inflight=1000
--watch-cache-sizes=services,pods=1000
| 场景 | 推荐 |
|---|---|
| 性能优先 | Calico(BGP) |
| 简单稳定 | Flannel |
| 大规模 | Cilium |
# 开启 IPIP 或 BGP
CALICO_IPV4_POOL_IPIP=Always
# 关闭 MTU 探测异常
MTU=1450
hostPath / local PV
rbd cachemount -o nfsvers=4.1,proto=tcp,rsize=1048576,wsize=1048576
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 512Mi
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
value: 1000000
preemptionPolicy: PreemptLowerPriority
| 组件 | 作用 |
|---|---|
| Prometheus | 监控 |
| Grafana | 可视化 |
| HPA | 自动扩缩容 |
| VPA | 资源推荐 |
✅ 强烈建议
✅ swap off
✅ SELinux disabled
✅ 内核参数优化
✅ containerd + systemd cgroup
✅ 镜像加速
✅ kubelet 资源限制
✅ Request / Limit 设置
✅ 网络方案稳定
✅ 监控部署
你可以直接告诉我:
1️⃣ k8s 版本
2️⃣ 节点规模(几 master / 几 worker)
3️⃣ 是否生产环境
4️⃣ 主要跑什么业务(Java / Web / AI / 大数据)
我可以直接给你一份 针对你环境的 k8s 优化配置文件模板 ✅