温馨提示×

为什么centos部署k8s失败

小樊
44
2026-09-27 12:57:15
栏目: 智能运维

在 CentOS 上部署 Kubernetes(k8s)失败 是非常常见的问题,原因通常集中在系统环境、网络、版本兼容性和配置步骤几个方面。下面我按最常见失败原因 → 排查思路 → 解决建议来系统说明。


一、最常见失败原因(90% 都在这)

1️⃣ 系统版本 / 内核不满足要求

Kubernetes 对 CentOS 版本有明确要求:

Kubernetes 版本 推荐 CentOS
1.24+ CentOS 7.9 / 8 Stream / Rocky / Alma
1.28+ 不建议 CentOS 7(已接近 EOL)

✅ 检查:

cat /etc/centos-release
uname -a

❌ 常见错误:

  • CentOS 7 + 新版本 kubeadm
  • 内核 < 3.10(docker/containerd 不支持)

2️⃣ 防火墙 / SELinux 没关或没配置

这是部署失败第一杀手。

✅ 正确做法:

systemctl stop firewalld
systemctl disable firewalld

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

❌ 错误现象:

  • Node NotReady
  • kube-apiserver 端口不通
  • etcd 无法通信

3️⃣ Swap 没关闭(必错)

Kubernetes 严格要求关闭 swap

swapoff -a
sed -i '/swap/d' /etc/fstab

❌ 报错示例:

running with swap on is not supported

4️⃣ 容器运行时问题(Docker / containerd)

从 K8s 1.24 起已移除 dockershim

❌ 常见错误:

  • 还在用 docker + 新版本 kubeadm
  • containerd 没配置 SystemdCgroup

✅ 推荐方案(2024+):

  • containerd
  • 或 docker + cri-dockerd

检查:

systemctl status containerd
crictl info

5️⃣ 网络不通(镜像 / 拉取失败)

在国内部署 几乎必踩

❌ 典型错误:

failed to pull image k8s.gcr.io/...

✅ 解决方案:

  • 使用国内镜像源
kubeadm init --image-repository registry.aliyuncs.com/google_containers

或提前拉镜像:

docker pull registry.aliyuncs.com/google_containers/...

6️⃣ kubeadm init 参数错误

常见错误:

  • 忘加 --pod-network-cidr
  • 多网卡没指定 --apiserver-advertise-address

✅ 示例(Calico):

kubeadm init \
  --pod-network-cidr=192.168.0.0/16 \
  --image-repository registry.aliyuncs.com/google_containers

7️⃣ CNI 网络插件没装 / 装错

没有 CNI → Node 永远 NotReady

✅ 常见选择:

  • Calico
  • Flannel
  • Cilium

示例(Calico):

kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

8️⃣ 时间不同步

节点时间差 > 5 分钟 → etcd / TLS 失败

✅ 解决:

timedatectl set-timezone Asia/Shanghai
systemctl enable --now chronyd

二、快速自检清单(部署前必看 ✅)

✅ CentOS 版本支持
✅ 关闭防火墙
✅ 关闭 SELinux
✅ 关闭 Swap
✅ 容器运行时正常
✅ 时间同步
✅ 镜像可拉取
✅ CNI 已安装

三、如果你愿意,我可以帮你精确定位

你可以直接贴出:

  1. CentOS 版本
  2. Kubernetes 版本
  3. 容器运行时(docker / containerd)
  4. 完整报错日志(kubeadm / kubelet)

我可以一步一步帮你找出 具体是哪一步失败。

0 踩