在 CentOS 上部署 Kubernetes(k8s),目前最常用、最稳定的方式是:kubeadm + containerd(或 docker)。下面我给你一套 生产可用、实战型 的部署流程(基于 CentOS 7 / 8,Kubernetes 1.28+)。
| 角色 | IP | 主机名 |
|---|---|---|
| master | 192.168.10.10 | k8s-master |
| node1 | 192.168.10.11 | k8s-node1 |
| node2 | 192.168.10.12 | k8s-node2 |
✅ 建议:
- master ≥ 2C4G
- node ≥ 2C4G
- 所有节点时间同步
systemctl disable firewalld --now
setenforce 0
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
swapoff -a
sed -i '/swap/d' /etc/fstab
hostnamectl set-hostname k8s-master
所有节点:
cat >> /etc/hosts <<EOF
192.168.10.10 k8s-master
192.168.10.11 k8s-node1
192.168.10.12 k8s-node2
EOF
cat <<EOF | tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF
modprobe br_netfilter
cat <<EOF | tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
sysctl --system
yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install -y containerd.io
mkdir -p /etc/containerd
containerd config default > /etc/containerd/config.toml
修改:
[plugins."io.containerd.grpc.v1.cri"]
SystemdCgroup = true
(搜索 SystemdCgroup 改为 true)
systemctl enable containerd --now
cat <<EOF | tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://pkgs.k8s.io/core:/stable:/v1.28/rpm/
enabled=1
gpgcheck=1
gpgkey=https://pkgs.k8s.io/core:/stable:/v1.28/rpm/repodata/repomd.xml.key
EOF
yum install -y kubelet kubeadm kubectl
systemctl enable kubelet
kubeadm init \
--apiserver-advertise-address=192.168.10.10 \
--pod-network-cidr=10.244.0.0/16
✅ 成功后会提示 join 命令,例如:
kubeadm join 192.168.10.10:6443 --token xxx --discovery-token-ca-cert-hash sha256:xxx
mkdir -p $HOME/.kube
cp /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
检查:
kubectl get pods -n kube-system
kubectl get nodes
在 node 节点执行 master 输出的 join 命令:
kubeadm join 192.168.10.10:6443 --token xxx \
--discovery-token-ca-cert-hash sha256:xxx
kubectl get nodes
kubectl get pods -A
✅ 状态全部 Ready 即成功
kubectl taint nodes --all node-role.kubernetes.io/control-plane-
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml
| 问题 | 解决方案 |
|---|---|
| kubelet 起不来 | 检查 containerd / cgroup |
| node NotReady | 网络插件未安装 |
| join 失败 | token 过期,重新生成 |
✅ CentOS 部署 k8s 标准流程
如果你愿意,我可以:
你现在是 学习环境 还是 生产环境?