2025-11-24 18:37:08 +08:00

108 lines
3.3 KiB
Django/Jinja
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
source ./common.sh
echo "[INFO] === 初始化 Master 节点 ==="
cat <<CFG > kubeadm-config.yaml
apiVersion: kubeadm.k8s.io/v1beta3
kind: ClusterConfiguration
kubernetesVersion: v{{ cluster.kubernetes_version }}
controlPlaneEndpoint: "{{ cluster.api_server_ip }}:6443"
networking:
podSubnet: "{{ cluster.pod_cidr }}"
serviceSubnet: "{{ cluster.service_cidr }}"
imageRepository: {{ registry.ip }}:{{ registry.port }}
---
apiVersion: kubelet.config.k8s.io/v1beta1
kind: KubeletConfiguration
cgroupDriver: systemd
CFG
# 预先检查
kubeadm init phase preflight --config kubeadm-config.yaml --ignore-preflight-errors=all
# 正式初始化
# 注意:因为我们已经手动导入了镜像,不需要 kubeadm pull
kubeadm init --config kubeadm-config.yaml --upload-certs | tee kubeadm-init.log
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
echo "[INFO] 部署网络插件 (Calico)..."
kubectl apply -f "$BUNDLE_ROOT/manifests/calico.yaml"
NAMESPACE="default"
LOCAL_REGISTRY="{{ registry.ip }}:{{ registry.port }}"
echo "[INFO] 5. 导入离线镜像..."
if [ -d "$IMAGES_DIR" ]; then
for tarfile in "$IMAGE_DIR"/*.tar; do
[ -e "$tarfile" ] || continue
echo ""
echo ">>> Processing $tarfile"
# 1⃣ 导入镜像
ctr -n "$NAMESPACE" images import "$tarfile"
# 2⃣ 获取最新导入镜像(兼容老版本 ctr
ORIGIN_IMG=$(ctr -n "$NAMESPACE" images ls -q | head -n1)
if [[ -z "$ORIGIN_IMG" ]]; then
echo "❌ Failed to detect original image name, skipping..."
continue
fi
echo "Original image: $ORIGIN_IMG"
# 3⃣ 根据 tar 文件名生成本地 registry 镜像名
# 文件名示例docker.io_calico_cni_v3.26.1.tar
BASENAME=$(basename "$tarfile" .tar)
BASENAME=${BASENAME#*_} # 去掉 registry 前缀: calico_cni_v3.26.1
NAME_TAG=${BASENAME}
NAME=${NAME_TAG%_*} # calico_cni
TAG=${NAME_TAG##*_} # v3.26.1
NEW_IMG="${LOCAL_REGISTRY}/${NAME}:${TAG}"
echo "Retag as: $NEW_IMG"
# 4⃣ 打 tag
ctr -n "$NAMESPACE" images tag "$ORIGIN_IMG" "$NEW_IMG"
# 5⃣ 推送到本地 registry
ctr -n "$NAMESPACE" images push --plain-http "$NEW_IMG"
echo "✅ Done: $NEW_IMG"
done
fi
echo "[INFO] 部署本地 Registry 容器..."
mkdir -p /opt/registry-data
ctr images import $IMAGES_DIR/registry_2.tar
ctr container create \
--net-host \
--mount type=bind,src=/opt/registry-data,dst=/var/lib/registry,options=rbind:rw \
docker.io/library/registry:2 \
registry-local
nohup ctr task start registry-local &
echo "[INFO] 部署 Storage & Virtualization..."
# 安装 Helm
cp "$BUNDLE_ROOT/bin/helm" /usr/local/bin/
# NFS
"$BUNDLE_ROOT/bin/helm" install nfs-subdir-provisioner "$BUNDLE_ROOT/charts/nfs-subdir-external-provisioner" \
--set nfs.server={{ storage.nfs_server }} \
--set nfs.path={{ storage.nfs_path }} \
--set storageClass.defaultClass=true
# KubeVirt
kubectl apply -f "$BUNDLE_ROOT/manifests/kubevirt-operator.yaml"
kubectl apply -f "$BUNDLE_ROOT/manifests/kubevirt-cr.yaml"
# Multus
kubectl apply -f "$BUNDLE_ROOT/manifests/multus-daemonset.yaml"
echo "[INFO] 生成 Worker 加入脚本..."
kubeadm token create --print-join-command > ../../output/join_cluster.sh
chmod +x ../../output/join_cluster.sh
echo "Master 部署完成!请检查 kubectl get nodes"