温馨提示×

centos部署k8s为什么失败

小樊
44
2026-09-09 09:36:51
栏目: 智能运维

在 CentOS 上部署 Kubernetes(k8s)失败,原因往往比较集中,下面按最常见失败原因 + 排查思路给你一个系统梳理,你可以对照看自己是哪一种。


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

1️⃣ 系统版本不兼容(非常常见)

Kubernetes 已经逐步放弃对 CentOS 7 的完整支持。

系统 情况
CentOS 7 ❌ 官方已不推荐,很多组件无法安装
CentOS 8 ❌ CentOS 8 已 EOL
Rocky / AlmaLinux 8/9 ✅ 推荐
Ubuntu 20.04/22.04 ✅ 最稳定

✅ 结论:

CentOS 7 部署 k8s 1.24+ 基本都会失败


2️⃣ 未关闭 swap(必踩坑)

Kubernetes 明确要求关闭 swap

free -m

如果有 swap:

swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab

否则:

failed to run Kubelet: running with swap on is not supported

3️⃣ 防火墙 / SELinux 没关

CentOS 默认 SELinux = enforcing

getenforce

如果不是 disabled:

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

防火墙:

systemctl stop firewalld
systemctl disable firewalld

4️⃣ 内核参数 & 桥接未配置(网络起不来)

cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1
EOF

sysctl --system

否则会出现:

  • Pod 无法通信
  • coredns 一直 CrashLoopBackOff

5️⃣ 容器运行时问题(containerd / docker)

Kubernetes 1.24 以后不再支持 docker shim

✅ 正确做法:

  • 使用 containerd
  • Docker 需要额外配置(不推荐)

检查:

systemctl status containerd

6️⃣ kubeadm init 失败(最常见报错)

常见原因:

  • 端口被占用(10250、6443)
  • 主机名不合法
  • IP 不匹配
kubeadm init --apiserver-advertise-address=你的IP

建议加:

--pod-network-cidr=10.244.0.0/16

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

初始化后必须装 CNI:

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

否则:

The connection to the server localhost:8080 was refused

二、快速自检清单 ✅

# 1. 系统
cat /etc/redhat-release

# 2. swap
free -m

# 3. SELinux
getenforce

# 4. 防火墙
systemctl status firewalld

# 5. 容器运行时
systemctl status containerd

# 6. kubelet
systemctl status kubelet
journalctl -xu kubelet

三、如果你愿意,我可以直接帮你定位

你可以直接把 报错信息贴出来,或者告诉我:

  1. CentOS 版本
  2. Kubernetes 版本
  3. 用的什么方式(kubeadm / 二进制 / k3s)
  4. 报错截图或日志

我可以直接帮你 一步步修到能跑。

0 踩