Skip to content

Instantly share code, notes, and snippets.

@wintersolutions
Last active January 30, 2021 02:21
Show Gist options
  • Save wintersolutions/f181c93724dbcda7d985116c91e0752a to your computer and use it in GitHub Desktop.
Save wintersolutions/f181c93724dbcda7d985116c91e0752a to your computer and use it in GitHub Desktop.
#!/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
# https://github.com/justmeandopensource/kubernetes/issues/54
cat <<EOF> /etc/systemd/system/rc-local.service
[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
[Install]
WantedBy=multi-user.target
EOF
cat <<EOF> /etc/rc.local
#!/bin/bash
mknod /dev/kmsg c 1 11
EOF
chmod +x /etc/rc.local
systemctl enable rc-local > /dev/null 2>&1
systemctl start rc-local
#######################################
# 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.244.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