1、CentOS7.9 单机部署
#!/bin/bash
#set -e
# ==============================
# CentOS7.9 单机 K8s 一键脚本
# ==============================
echo "==============================================="
echo " CentOS 7.9 单机 K8s 一键部署(纯净无坑) "
echo "==============================================="
# 1. 强制清空旧环境(彻底清理)
echo -e "\n===== 1. 清空旧环境 ====="
kubeadm reset -f &>/dev/null
systemctl stop kubelet containerd &>/dev/null
rm -rf /etc/kubernetes /var/lib/etcd /var/lib/kubelet /root/.kube
rm -rf /run/kube* /var/lib/rancher /opt/containerd
iptables -F && iptables -t nat -F
# 2. 关闭防火墙、swap
echo -e "\n===== 2. 关闭防火墙 & Swap ====="
systemctl stop firewalld && systemctl disable firewalld &>/dev/null
swapoff -a
sed -i '/swap/s/^/#/' /etc/fstab
# 3. 内核网络配置
echo -e "\n===== 3. 配置内核网络 ====="
cat <<EOF > /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter
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 &>/dev/null
echo 1 > /proc/sys/net/ipv4/ip_forward
# 4. 安装 containerd(阿里云源)
# 1. 安装依赖
yum install -y yum-utils device-mapper-persistent-data lvm2
# 2. 添加阿里云 Docker 源(containerd 从这里安装)
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 3. 安装 containerd
yum install -y containerd.io
# 4. 生成默认配置
mkdir -p /etc/containerd
containerd config default > /etc/containerd/config.toml
# 5. 关键配置:替换 pause 镜像 + 开启 systemd cgroup
sed -i "s#registry.k8s.io/pause#registry.aliyuncs.com/google_containers/pause#g" /etc/containerd/config.toml
sed -i "s#SystemdCgroup = false#SystemdCgroup = true#g" /etc/containerd/config.toml
# 6. 启动并开机自启
systemctl daemon-reload
systemctl enable --now containerd
systemctl restart containerd
# 7. 验证
containerd --version
# 5. 安装 K8s 1.28.0(阿里云源)
echo -e "\n===== 5. 安装 K8s 组件 ====="
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
repo_gpgcheck=0
EOF
yum install -y kubelet-1.28.0 kubeadm-1.28.0 kubectl-1.28.0 --disableexcludes=kubernetes &>/dev/null
systemctl enable --now kubelet
# 6. 初始化集群
echo -e "\n===== 6. 初始化 K8s 集群 ====="
MASTER_IP=$(hostname -I | awk '{print $1}')
kubeadm init \
--apiserver-advertise-address=${MASTER_IP} \
--pod-network-cidr=10.244.0.0/16 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version=v1.28.0
# 7. 配置 kubectl
echo -e "\n===== 7. 配置命令工具 ====="
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
# 8. 安装网络插件
echo -e "\n===== 8. 安装网络 ====="
kubectl apply -f https://mirrors.aliyun.com/k8s/flannel/kube-flannel.yml
# 9. 允许 Master 运行 Pod
echo -e "\n===== 9. 单机模式配置 ====="
kubectl taint nodes --all node-role.kubernetes.io/control-plane- 2>/dev/null
echo -e "\n==============================================="
echo -e "✅ K8s 安装完成!"
echo -e "查看节点:kubectl get nodes"
echo -e "==============================================="