All checks were successful
Build Docker Image / Explore-Gitea-Actions (push) Successful in 1m55s
108 lines
2.4 KiB
Bash
108 lines
2.4 KiB
Bash
#!/bin/bash
|
|
|
|
curl -sSL https://git.martin98.com/MartinFarm/init/raw/branch/main/init-apt.sh | bash
|
|
|
|
current_ip=$(hostname -I | awk '{print $1}')
|
|
|
|
# 镜像加速
|
|
mkdir /etc/rancher/k3s -p
|
|
|
|
cat <<EOF > /etc/rancher/k3s/registries.yaml
|
|
mirrors:
|
|
docker.io:
|
|
endpoint:
|
|
- https://docker.martin98.com
|
|
registry-1.docker.io:
|
|
endpoint:
|
|
- https://docker.martin98.com
|
|
registry.k8s.io:
|
|
endpoint:
|
|
- https://docker.martin98.com
|
|
k8s.gcr.io:
|
|
endpoint:
|
|
- https://docker.martin98.com
|
|
gcr.io:
|
|
endpoint:
|
|
- https://docker.martin98.com
|
|
ghcr.io:
|
|
endpoint:
|
|
- https://docker.martin98.com
|
|
quay.io:
|
|
endpoint:
|
|
- https://docker.martin98.com
|
|
EOF
|
|
|
|
# systemctl restart k3s
|
|
|
|
# 安装 keepalived
|
|
apt install -y keepalived
|
|
|
|
# 检查是否为 Master-01
|
|
if [ "$current_ip" == "$master" ]; then
|
|
state=MASTER
|
|
priority=100
|
|
else
|
|
state=BACKUP
|
|
priority=100
|
|
fi
|
|
|
|
# 初始化 VIP
|
|
mkdir /etc/keepalived
|
|
cat <<EOF | sudo tee /etc/keepalived/keepalived.conf
|
|
vrrp_instance VI_1 {
|
|
state $state
|
|
interface eth0
|
|
virtual_router_id 51
|
|
priority $priority
|
|
advert_int 1
|
|
virtual_ipaddress {
|
|
$vip
|
|
}
|
|
}
|
|
EOF
|
|
|
|
sudo systemctl restart keepalived
|
|
systemctl status keepalived &
|
|
|
|
|
|
|
|
# 检查是否为 Master-01
|
|
if [ "$current_ip" == "$master" ]; then
|
|
# 开始部署
|
|
curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -s - server \
|
|
--cluster-init \
|
|
--cluster-cidr $pod_subnet \
|
|
--service-cidr $service_subnet \
|
|
--node-external-ip=$vip_ip \
|
|
|
|
|
|
TOKEN=$(cat /var/lib/rancher/k3s/server/node-token)
|
|
cat <<EOF
|
|
------------------------------------------------------------------------------------
|
|
初始化 master VIP[$state] $current_ip 成功
|
|
|
|
export pod_subnet=$pod_subnet
|
|
export service_subnet=$service_subnet
|
|
export vip_ip=$vip_ip
|
|
export vip=$vip_ip/16
|
|
export master=$master
|
|
export TOKEN=$TOKEN
|
|
curl -sSL https://git.martin98.com/MartinFarm/init/raw/branch/main/init-k3s.sh | bash
|
|
|
|
------------------------------------------------------------------------------------
|
|
EOF
|
|
|
|
else
|
|
curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -s - server \
|
|
--cluster-cidr $pod_subnet \
|
|
--service-cidr $service_subnet \
|
|
--node-external-ip=$vip_ip \
|
|
--token $TOKEN \
|
|
--server https://$master:6443
|
|
watch kubectl get nodes
|
|
fi
|
|
|
|
|
|
|
|
|