Last active
September 4, 2020 10:54
-
-
Save fristonio/ea6c696be7dd20e7225d8af8e8e378fb to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env bash | |
# Script to configure a Kubeadm cluster on Ubuntu machine | |
set -eu | |
version_regex='^[0-9]+\.[0-9]+\.[0-9]+$' | |
KERNEL_VERSION="5.8.1" | |
K8S_VERSION="1.19.0" | |
START_TIME=$(date +"%T") | |
kernel_ppa_host="kernel.ubuntu.com" | |
kernel_ppa_index="/~kernel-ppa/mainline/" | |
kernel_dir="/tmp/kubeadm-${START_TIME}/kernel/" | |
function validate_permissions() { | |
if ! [[ $EUID -eq 0 ]]; then | |
echo "ERROR: Script is required to run as root" | |
exit 1 | |
fi | |
} | |
function usage() { | |
echo "Usage: $0 [ -k KERNEL_VERSION ] [ -v KUBERNETES_VERSION ]" 1>&2 | |
echo "For example use the script as - $0 -v 1.19.0 -k 5.8.1" | |
} | |
function exit_usage() { | |
usage | |
exit 1 | |
} | |
function parse_opts() { | |
while getopts :v:k: flag | |
do | |
case "${flag}" in | |
v) | |
K8S_VERSION=${OPTARG} | |
;; | |
k) | |
KERNEL_VERSION=${OPTARG} | |
;; | |
:) | |
echo "Error: -${OPTARG} requires an argument." | |
exit_usage | |
;; | |
*) | |
exit_usage | |
;; | |
esac | |
done | |
if [[ $K8S_VERSION =~ $version_regex ]]; then | |
echo "INFO: Kuberentes Version: ${K8S_VERSION}" | |
else | |
echo "ERROR: Invalid Kubernetes version: '${K8S_VERSION}'" | |
exit 1 | |
fi | |
echo "INFO: Kernel Version: ${KERNEL_VERSION}" | |
} | |
function set_req_sysconfig() { | |
echo "INFO: Setting required system config" | |
sysctl -w net.ipv6.conf.all.forwarding=1 | |
} | |
function set_req_repositories() { | |
echo "INFO: Setting required apt repositories" | |
apt update | |
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - | |
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list | |
deb http://apt.kubernetes.io/ kubernetes-xenial main | |
EOF | |
apt update | |
} | |
function setup_download_utils() { | |
apt install curl apt-transport-https -y | |
} | |
function download_initramfs_tools() { | |
apt update && apt install -y initramfs-tools | |
} | |
function download_required_kernel() { | |
mkdir -p "${kernel_dir}" | |
downloads_uri="https://${kernel_ppa_host}${kernel_ppa_index}v${KERNEL_VERSION}/amd64/" | |
# Download only generic kernel components from the directory | |
# listing | |
packages=$(curl -s "${downloads_uri}" | \ | |
grep -Eoi '<a [^>]+>' | \ | |
grep -Eo 'href="[^\"]+"' | \ | |
grep -Eo 'linux[^\"]+' | \ | |
grep -v 'lowlatency') | |
IFS=$'\n' | |
for pkg in $packages | |
do | |
package_uri="${downloads_uri}${pkg}" | |
curl -fsSL "${package_uri}" -o "${kernel_dir}/${pkg}" | |
done | |
download_initramfs_tools | |
echo "INFO: Downloaded kernel packages to ${kernel_dir}" | |
} | |
function setup_container_runtime() { | |
if ! [[ -x "$(command -v docker)" ]]; then | |
echo "INFO: Installing Docker as container runtime" | |
apt install docker.io -y | |
else | |
echo "INFO: Docker is already installed" | |
fi | |
} | |
function setup_k8s_components() { | |
setup_container_runtime | |
echo "INFO: Installing kubectl on the node" | |
apt install -y "kubectl=${K8S_VERSION}-00" | |
echo "INFO: Installing kubeadm on the node" | |
apt install -y "kubeadm=${K8S_VERSION}-00" | |
echo "INFO: Installing kubelet on the node" | |
apt install -y "kubelet=${K8S_VERSION}-00" | |
} | |
function install_new_kernel() { | |
cd "${kernel_dir}" && dpkg -i *.deb | |
local kv | |
local regex | |
regex="${KERNEL_VERSION}-[^\ ]+-generic" | |
kv=$(dpkg --list | grep "linux-image" | grep -Eo "${regex}") | |
update-initramfs -u -k "${kv}" | |
update-grub | |
} | |
validate_permissions | |
parse_opts | |
set_req_sysconfig | |
set_req_repositories | |
download_required_kernel | |
setup_k8s_components | |
echo "INFO: Installation of required component finished" | |
install_new_kernel | |
read -p "Reboot Machine(Y/N)? :" -n 1 -r | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
reboot | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment