在 CentOS 上部署 Kubernetes(k8s)经常报错,通常不是 Kubernetes 本身的问题,而是 CentOS 的系统特性 + 环境配置不当 导致的。下面我按「最常见原因 → 对应报错现象 → 解决办法」给你梳理一下。
报错示例
dial tcp: connect: no route to host
failed to connect to apiserver
原因
firewalld解决
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
报错示例
failed to run Kubelet: running with swap on is not supported
解决
swapoff -a
sed -i '/swap/s/^/#/' /etc/fstab
报错示例
iptables: No chain/target/match by that name
pod 无法访问外网
解决
cat > /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
sysctl --system
报错示例
failed to create runtime: failed to start container
docker: unknown flag
常见坑
解决
yum install -y containerd
systemctl enable --now containerd
报错示例
x509: certificate has expired or is not yet valid
解决
yum install -y chrony
systemctl enable --now chronyd
chronyc sources
报错示例
node not found
kubectl get nodes 一直 NotReady
要求
_/etc/hosts 要能互相解析hostnamectl set-hostname master
cat >> /etc/hosts <<EOF
192.168.1.10 master
192.168.1.11 node1
EOF
| 系统 | 情况 |
|---|---|
| CentOS 7 | 老,但稳定 |
| CentOS 8 | 已 EOL |
| CentOS Stream | k8s 官方推荐 |
建议
常见错误
kubeadm init
# 没指定 --pod-network-cidr
正确示例
kubeadm init \
--pod-network-cidr=10.244.0.0/16 \
--image-repository=registry.aliyuncs.com/google_containers
# 1. SELinux
getenforce # 应为 Disabled
# 2. swap
free -m # swap 应为 0
# 3. 防火墙
systemctl status firewalld
# 4. 容器运行时
crictl info
你可以直接贴:
我可以 逐行帮你定位是哪一步炸的。