config/scripts/2.first_start_k8s.sh

107 lines
2.6 KiB
Bash
Raw Normal View History

2023-05-24 18:12:07 +00:00
#!/bin/bash
#
# Starting control plane
prereqs_control_plane() {
for com in kubectl ; do
command -v $com 1>/dev/null || perror "Command $com is not installed, aborting..."
done
}
prereqs_addons() {
for com in curl kubectl ; do
command -v $com 1>/dev/null || perror "Command $com is not installed, aborting..."
done
cluster=$(basename $ctxdir/clusters/*.yaml|sed 's/.yaml//')
}
checkup() {
for host in ${!hosts[*]}; do
tries=3
while :
do
pinfo "Checking availability of node $host..."
2023-06-25 17:47:27 +00:00
if true | sshcmd -q root@${hosts[$host]};
2023-06-14 21:17:00 +00:00
then
2023-05-24 18:12:07 +00:00
pinfo "VM $host is up!"
break
else
((tries--))
2023-06-13 16:56:02 +00:00
if [ "$tries" -lt "1" ]; then
2023-05-24 18:12:07 +00:00
pinfo "Timeout waiting for node detection, please investigate why node $host is not up by now"
break
fi
sleep 30
fi
done
done
}
start_control_plane() {
for host in ${!hosts[*]}; do
2023-06-25 17:47:27 +00:00
sshcmd -q root@${hosts[$host]} << EOF
2023-05-24 18:12:07 +00:00
if ls /etc/kubernetes/manifests.static/* &>/dev/null ; then
mv /etc/kubernetes/manifests.static/* /var/lib/kubelet/manifests/
fi
EOF
done
}
wait_for_apiserver() {
vip=$(extract_var clusters public_vip)
vip_api_port=$(extract_var clusters api_port)
while :
do
pinfo "Waiting for apiserver availability ($vip:$vip_api_port). Images may still being pulled... "
if kctl get node &>/dev/null ; then
pinfo "API is up!"
break
else
2023-06-25 17:47:27 +00:00
sleep 20
fi
done
}
2023-05-24 18:12:07 +00:00
install_addons() {
body='{"Kind":"cluster","Name":"'$cluster'","Assets":["addons"]}'
download_id=$(dls /authorize-download -d "$body"|tr -d \")
dls /public/downloads/${download_id}/addons |\
kctl apply -f -
}
approve_kubelet_certificates() {
2023-06-25 17:47:27 +00:00
tries=10
2023-05-24 18:12:07 +00:00
nodes_num=$(kctl get node -oname|wc -l)
2023-06-25 17:47:27 +00:00
while [ "$nodes_num" -lt "${#hosts[*]}" ] ; do
pinfo "Waiting for certificates requests to be created by Kubelet when it's ready... ($tries tries out of 10)"
sleep 20s
2023-05-24 18:12:07 +00:00
csrs="$(kctl get csr|awk '/Pending/ {print $1}')"
if [ "$csrs" != "" ]; then
kctl certificate approve $csrs
fi
((tries--))
2023-06-14 21:07:03 +00:00
if [ "$tries" -lt "1" ]; then
2023-06-25 17:47:27 +00:00
perror "Timeout waiting for kubelet certificates creation, please investigate why all nodes are not up by now"
2023-05-24 18:12:07 +00:00
fi
2023-06-25 17:47:27 +00:00
nodes_num=$(kctl get node -oname|wc -l)
2023-05-24 18:12:07 +00:00
done
pinfo "All kubelets ($nodes_num) are up, enjoy !"
}
source $(dirname $0)/.common
prereqs
prereqs_control_plane
prereqs_addons
check_conf
unlock_store
declare -A hosts && get_hosts
checkup
start_control_plane
create_kubeconfig
2023-06-25 17:47:27 +00:00
wait_for_apiserver
2023-05-24 18:12:07 +00:00
install_addons
approve_kubelet_certificates # clients and serving certs