Created
July 12, 2021 18:52
-
-
Save vitobotta/96fe543d36102e6ccb49cf2cd5cf9199 to your computer and use it in GitHub Desktop.
Create a K3s cluster very quickly in Hezner cloud
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
POSITIONAL=() | |
while [[ $# -gt 0 ]] | |
do | |
key="$1" | |
case $key in | |
--context) | |
CONTEXT_NAME="$2" | |
shift # past argument | |
shift # past value | |
;; | |
--kubeconfig-path) | |
KUBECONFIG_PATH="$2" | |
shift # past argument | |
shift # past value | |
;; | |
--token) | |
TOKEN="$2" | |
shift # past argument | |
shift # past value | |
;; | |
--master) | |
MASTER="$2" | |
shift | |
shift | |
;; | |
--workers) | |
IFS=',' | |
read -ra WORKERS <<<"$2" | |
shift | |
shift | |
;; | |
--flannel-interface) | |
FLANNEL_INTERFACE="$2" | |
shift | |
shift | |
;; | |
--cluster-cidr) | |
FLANNEL_INTERFACE="$2" | |
shift | |
shift | |
;; | |
--ssh-user) | |
SSH_USER="$2" | |
shift | |
shift | |
;; | |
*) | |
POSITIONAL+=("$1") | |
shift | |
;; | |
esac | |
done | |
set -- "${POSITIONAL[@]}" # restore positional parameters | |
[[ -z "$CONTEXT_NAME" ]] && { echo "Please specify the context name with '--context <name>'" ; exit 1; } | |
[[ -z "$KUBECONFIG_PATH" ]] && { echo "Please specify the path where to save the kubeconfig with '--kubeconfig-path <path>'" ; exit 1; } | |
[[ -z "$TOKEN" ]] && { echo "Please specify the token with '--token <token>'" ; exit 1; } | |
[[ -z "$MASTER" ]] && { echo "Please specify the public ip of the master with '--master <public-ip1>,<public-ip2>,...'" ; exit 1; } | |
[[ -z "$FLANNEL_INTERFACE" ]] && { echo "Please specify the Flannel interface with '--flannel-interface <interfacce>'" ; exit 1; } | |
if [ ${#WORKERS[@]} -eq 0 ]; then | |
echo "Please specify one or more workers with '--workers <public-ip>'" | |
exit 1 | |
fi | |
CLUSTER_CIDR=${CLUSTER_CIDR:-"10.244.0.0/16"} | |
SSH_USER=${SSH_USER:-"root"} | |
MASTER_PRIVATE_IP=$(ssh $SSH_USER@$MASTER "hostname -I" | awk '{print $2}') | |
cat << EOF > /tmp/master.sh | |
FLANNEL_INTERFACE="$FLANNEL_INTERFACE" | |
CLUSTER_CIDR="$CLUSTER_CIDR" | |
K3S_TOKEN="$TOKEN" | |
EOF | |
cat << 'EOF' >> /tmp/master.sh | |
curl -sfL https://get.k3s.io | K3S_TOKEN=$K3S_TOKEN INSTALL_K3S_EXEC="server \ | |
--disable-cloud-controller \ | |
--disable servicelb \ | |
--disable traefik \ | |
--disable local-storage \ | |
--write-kubeconfig-mode=644 \ | |
--node-name="$(hostname -f)" \ | |
--cluster-cidr=$CLUSTER_CIDR \ | |
--tls-san=$(hostname -I | awk '{print $1}') \ | |
--kubelet-arg="cloud-provider=external" \ | |
--node-ip=$(hostname -I | awk '{print $2}') \ | |
--node-external-ip=$(hostname -I | awk '{print $1}') \ | |
--flannel-iface=$FLANNEL_INTERFACE" sh - | |
EOF | |
echo | |
echo "Setting up master..." | |
scp /tmp/master.sh $SSH_USER@$MASTER:/tmp/master.sh > /dev/null 2>&1 | |
ssh $SSH_USER@$MASTER /bin/bash /tmp/master.sh | |
echo "...master up and running." | |
ssh $SSH_USER@$MASTER "cat /etc/rancher/k3s/k3s.yaml" | sed "s/default/$CONTEXT_NAME/g" | sed "s/127.0.0.1/$MASTER/g" > $KUBECONFIG_PATH | |
for WORKER in "${WORKERS[@]}" | |
do | |
: | |
cat << EOF > /tmp/worker.sh | |
MASTER_PRIVATE_IP="$MASTER_PRIVATE_IP" | |
FLANNEL_INTERFACE="$FLANNEL_INTERFACE" | |
CLUSTER_CIDR="$CLUSTER_CIDR" | |
K3S_TOKEN="$TOKEN" | |
EOF | |
cat << 'EOF' >> /tmp/worker.sh | |
curl -sfL https://get.k3s.io | K3S_TOKEN=$K3S_TOKEN K3S_URL=https://$MASTER_PRIVATE_IP:6443 INSTALL_K3S_EXEC="agent \ | |
--node-name="$(hostname -f)" \ | |
--kubelet-arg="cloud-provider=external" \ | |
--node-ip=$(hostname -I | awk '{print $2}') \ | |
--node-external-ip=$(hostname -I | awk '{print $1}') \ | |
--flannel-iface=$FLANNEL_INTERFACE" sh - | |
EOF | |
echo | |
echo "Setting up worker on $WORKER..." | |
scp /tmp/worker.sh $SSH_USER@$WORKER:/tmp/worker.sh > /dev/null 2>&1 | |
ssh $SSH_USER@$WORKER /bin/bash /tmp/worker.sh | |
echo "...worker $WORKER up and running." | |
echo | |
done | |
echo | |
echo "Finished building k3s cluster." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment