Last active
January 29, 2021 03:39
-
-
Save wintersolutions/7bfa0a4878cf4cbac0a6cac734409025 to your computer and use it in GitHub Desktop.
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 | |
# This script has been tested on Ubuntu 20.04 | |
# For other versions of Ubuntu, you might need some tweaking | |
echo "[TASK 1] Install containerd runtime" | |
apt update -qq >/dev/null 2>&1 | |
apt install -qq -y containerd apt-transport-https >/dev/null 2>&1 | |
mkdir /etc/containerd | |
containerd config default > /etc/containerd/config.toml | |
systemctl restart containerd | |
systemctl enable containerd >/dev/null 2>&1 | |
echo "[TASK 2] Add apt repo for kubernetes" | |
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - >/dev/null 2>&1 | |
apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main" >/dev/null 2>&1 | |
echo "[TASK 3] Install Kubernetes components (kubeadm, kubelet and kubectl)" | |
apt install -qq -y kubeadm=1.20.0-00 kubelet=1.20.0-00 kubectl=1.20.0-00 >/dev/null 2>&1 | |
echo 'KUBELET_EXTRA_ARGS="--fail-swap-on=false"' > /etc/default/kubelet | |
systemctl restart kubelet | |
echo "[TASK 4] Enable ssh password authentication" | |
sed -i 's/^PasswordAuthentication .*/PasswordAuthentication yes/' /etc/ssh/sshd_config | |
echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config | |
systemctl reload sshd | |
echo "[TASK 5] Set root password" | |
echo -e "KUBEADMIN\nKUBEADMIN" | passwd root >/dev/null 2>&1 | |
echo "export TERM=xterm" >> /etc/bash.bashrc | |
echo "[TASK 6] Install additional packages" | |
apt install -qq -y net-tools >/dev/null 2>&1 | |
# Hack required to provision K8s v1.15+ in LXC containers | |
mknod /dev/kmsg c 1 11 | |
echo 'mknod /dev/kmsg c 1 11' >> /etc/rc.local | |
chmod +x /etc/rc.local | |
echo 'L /dev/kmsg - - - - /dev/console' > /etc/tmpfiles.d/kmsg.conf | |
####################################### | |
# To be executed only on master nodes # | |
####################################### | |
if [[ $(hostname) =~ .*master.* ]] | |
then | |
echo "[TASK 7] Pull required containers" | |
kubeadm config images pull >/dev/null 2>&1 | |
echo "[TASK 8] Initialize Kubernetes Cluster" | |
kubeadm init --pod-network-cidr=10.69.0.0/16 --ignore-preflight-errors=all >> /root/kubeinit.log 2>&1 | |
echo "[TASK 9] Copy kube admin config to root user .kube directory" | |
mkdir /root/.kube | |
cp /etc/kubernetes/admin.conf /root/.kube/config | |
echo "[TASK 10] Deploy Flannel network" | |
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml > /dev/null 2>&1 | |
echo "[TASK 11] Generate and save cluster join command to /joincluster.sh" | |
joinCommand=$(kubeadm token create --print-join-command 2>/dev/null) | |
echo "$joinCommand --ignore-preflight-errors=all" > /joincluster.sh | |
fi | |
####################################### | |
# To be executed only on worker nodes # | |
####################################### | |
if [[ $(hostname) =~ .*worker.* ]] | |
then | |
echo "[TASK 7] Join node to Kubernetes Cluster" | |
apt install -qq -y sshpass >/dev/null 2>&1 | |
sshpass -p "KUBEADMIN" scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no kmaster1.lxd:/joincluster.sh /joincluster.sh 2>/tmp/joincluster.log | |
bash /joincluster.sh >> /tmp/joincluster.log 2>&1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment