在 CentOS 上部署 Kubernetes(k8s) 时,有一些系统层面和 Kubernetes 本身的注意点,否则很容易踩坑。下面按部署前 → 部署中 → 部署后给你梳理。
Kubernetes 要求 swap 必须关闭
swapoff -a
sed -i '/swap/s/^/#/' /etc/fstab
两种方式二选一:
✅ 测试环境(简单)
systemctl stop firewalld
systemctl disable firewalld
✅ 生产环境(建议放行端口)
setenforce 0
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
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
EOF
sysctl --system
节点时间不一致会导致 etcd 异常
yum install -y chrony
systemctl enable --now chronyd
可选:
yum install -y containerd
systemctl enable --now containerd
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
EOF
yum install -y kubelet kubeadm kubectl
systemctl enable kubelet
kubeadm init \
--pod-network-cidr=10.244.0.0/16 \
--image-repository=registry.aliyuncs.com/google_containers
常见选择:
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
原因通常是:
检查:
kubectl get nodes
journalctl -u kubelet -f
containerd 与 kubelet 必须一致:
# /etc/containerd/config.toml
SystemdCgroup = true
并配置 kubelet:
--cgroup-driver=systemd
✅ 使用 高可用 Master(3 节点)
✅ 使用 负载均衡(VIP / HAProxy / Nginx)
✅ 使用 Rocky / AlmaLinux 替代 CentOS
✅ 定期备份 etcd
✅ 限制 Kubernetes 版本升级策略
CentOS 部署 k8s 的核心:关 swap、关防火墙、配内核、选对容器运行时、网络插件不能少。
如果你是:
可以告诉我,我可以给你一套针对性部署方案。