Install Kubernetes on Focal

Disable swap

{
systemctl disable swap.img.swap --now
sed -e '/swap/ s/^#*/#/' -i /etc/fstab
rm /swap.img
}

Set some sysctl’s

{
cat << EOF > /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter
cat << EOF > /etc/sysctl.d/99-kubernetes-cri.conf
net.bridge.bridge-nf-call-iptables  = 1
net.ipv4.ip_forward                 = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF
sysctl --system
}

Add containerd and k8s repos, install packages

{
echo "deb [arch=amd64] https://apt.kubernetes.io/ kubernetes-xenial main" > /etc/apt/sources.list.d/kubernetes.list
echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable" > /etc/apt/sources.list.d/docker.list
curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
apt-get update && apt-get -y install containerd.io kubelet kubeadm kubectl && apt-mark hold kubelet kubeadm kubectl
containerd config default > /etc/containerd/config.toml
sed -i s'/SystemdCgroup = false/SystemdCgroup = true/g' /etc/containerd/config.toml
systemctl restart containerd
}

Install Kubernetes control plane

For a single master:

kubeadm init --pod-network-cidr=10.244.0.0/16

For multi-master:

kubeadm init --pod-network-cidr=10.244.0.0/16 --control-plane-endpoint "k8s-lb:6443" --upload-certs

Make a note of the output for joining other nodes to the cluster

Copy the kubeadmin config

{
mkdir ~/.kube
cp /etc/kubernetes/admin.conf ~/.kube/config
}

Install network plugin

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

Join other nodes

Control plane nodes:

kubeadm join <LoadBalancer IP>:6443 \
    --token <token> \
    --discovery-token-ca-cert-hash sha256:<hash> \
    --control-plane \
    --certificate-key <key>

Worker nodes:

kubeadm join <Master Node IP>:6443 \
    --token <token> \
    --discovery-token-ca-cert-hash sha256:<hash>