在 CentOS 上部署 Kubernetes(k8s)失败,原因往往比较集中,下面按最常见失败原因 + 排查思路给你一个系统梳理,你可以对照看自己是哪一种。
Kubernetes 已经逐步放弃对 CentOS 7 的完整支持。
| 系统 | 情况 |
|---|---|
| CentOS 7 | ❌ 官方已不推荐,很多组件无法安装 |
| CentOS 8 | ❌ CentOS 8 已 EOL |
| Rocky / AlmaLinux 8/9 | ✅ 推荐 |
| Ubuntu 20.04/22.04 | ✅ 最稳定 |
✅ 结论:
CentOS 7 部署 k8s 1.24+ 基本都会失败
Kubernetes 明确要求关闭 swap
free -m
如果有 swap:
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
否则:
failed to run Kubelet: running with swap on is not supported
CentOS 默认 SELinux = enforcing
getenforce
如果不是 disabled:
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
防火墙:
systemctl stop firewalld
systemctl disable firewalld
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
否则会出现:
Kubernetes 1.24 以后不再支持 docker shim
✅ 正确做法:
检查:
systemctl status containerd
常见原因:
kubeadm init --apiserver-advertise-address=你的IP
建议加:
--pod-network-cidr=10.244.0.0/16
初始化后必须装 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
你可以直接把 报错信息贴出来,或者告诉我:
我可以直接帮你 一步步修到能跑。