Skip to content

Instantly share code, notes, and snippets.

@dnegi-dev
Last active February 27, 2023 00:11
Show Gist options
  • Select an option

  • Save dnegi-dev/27a65a4417601d9900d32b092613d8ec to your computer and use it in GitHub Desktop.

Select an option

Save dnegi-dev/27a65a4417601d9900d32b092613d8ec to your computer and use it in GitHub Desktop.
Create a HA Kubernetes Cluster on Ubuntu 22.04
#!/usr/bin/env bash
# This script is used to install k8s on ubuntu 22.04 amd64 server with CRI-O as container runtime and kubeadm as k8s installer.
# To run this script, use the following command:
# curl -sSL https://gist.githubusercontent.com/dnegi-dev/27a65a4417601d9900d32b092613d8ec/raw/48d4e41789ac43eec5064f4a0651ab8392d5d5de/create_k8s_ha_ubuntu.sh -o create_k8s_ha_ubuntu.sh && sudo bash create_k8s_ha_ubuntu.sh
LOG_FILE=/var/log/k8s-install.log
if [ $EUID != 0 ]; then
echo "This script must be run as root."
exit 1
fi
# Redirect all output to log file
exec > >(tee -i $LOG_FILE)
exec 2>&1
# Install Kubeadm, Kubelet and Kubectl
echo "Installing kubeadm, kubelet, and kubectl..."
apt-get update
apt-get install -y apt-transport-https ca-certificates curl
curl -fsSLo /etc/apt/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
echo "deb [signed-by=/etc/apt/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
apt-get update
apt-get install -y kubelet kubeadm kubectl
apt-mark hold kubelet kubeadm kubectl
# Install CRI-O
echo "Installing CRI-O..."
echo "deb [signed-by=/usr/share/keyrings/libcontainers-archive-keyring.gpg] https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_22.04/ /" > /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list
echo "deb [signed-by=/usr/share/keyrings/libcontainers-crio-archive-keyring.gpg] https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable:/cri-o:/1.26/xUbuntu_22.04/ /" > /etc/apt/sources.list.d/devel:kubic:libcontainers:stable:cri-o:1.26.list
mkdir -p /usr/share/keyrings
curl -L https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_22.04/Release.key | gpg --dearmor -o /usr/share/keyrings/libcontainers-archive-keyring.gpg
curl -L https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable:/cri-o:/1.26/xUbuntu_22.04/Release.key | gpg --dearmor -o /usr/share/keyrings/libcontainers-crio-archive-keyring.gpg
apt-get update
apt-get install -y cri-o cri-o-runc
systemctl daemon-reload
systemctl enable --now crio
# Disable swap
echo "Disabling swap..."
swapoff -a
sed -i '/swap/d' /etc/fstab
# Preparing the os accrding to https://kubernetes.io/docs/setup/production-environment/container-runtimes/
echo "Preparing the OS for CRI-O and k8s..."
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
sysctl --system
echo "k8s installation complete."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment