Files
k8s/init-deploy.sh

49 lines
1.4 KiB
Bash
Executable File

#!/bin/bash
KUBE_USER="ubuntu"
PRIVATE_KEY_PATH="$HOME/.ssh/id_ed25519"
KUBE_CP="k8s-control-plane"
KUBE_W1="k8s-worker1"
KUBE_W2="k8s-worker2"
ANSIBLE_DIR="ansible"
POD_NET_CIDR="192.168.0.0/16"
CNI_VER="v3.30.2"
API_ADDR=$(getent hosts "$KUBE_CP" | awk '{ print $1 }')
## Prepare all of the nodes with k8s using the ansible playbooks I prepared
ansible-playbook ./"$ANSIBLE_DIR"/master-k8s.yaml \
-i "$KUBE_CP,$KUBE_W1,$KUBE_W2," \
-e "k8s-control-plane=["$KUBE_CP"] k8s_nodes=["$KUBE_W1","$KUBE_W2"]" \
-u "$KUBE_USER" \
--private-key "$PRIVATE_KEY_PATH"
## Bootstrap the cluster
ssh "$KUBE_USER@$KUBE_CP" \
"sudo kubeadm init \
--pod-network-cidr=$POD_NET_CIDR \
--apiserver-advertise-address=$API_ADDR \
--cri-socket unix:///run/containerd/containerd.sock"
ssh "$KUBE_USER@$KUBE_CP" \
"sudo cp /etc/kubernetes/admin.conf /tmp/config && sudo chown $KUBE_USER:$KUBE_USER /tmp/config"
## Fetch the configuration from the freshly installed cluster. BEWARE THAT ANY EXISTING CONFIG WILL BE OVERWRITTEN
scp "$KUBE_USER@$KUBE_CP":/tmp/config "$HOME"/.kube/config
## Install the CNI
kubectl apply -f \
"https://raw.githubusercontent.com/projectcalico/calico/$CNI_VER/manifests/calico.yaml"
## Now join the workers to the cluster
JOIN_TOKEN=$(ssh "$KUBE_USER@$KUBE_CP" \
"sudo kubeadm token create --print-join-command")
for NODE in "$KUBE_W1" "$KUBE_W2"; do
echo "Joining $NODE"
ssh "$KUBE_USER@$NODE" "sudo $JOIN_TOKEN"
done