🏷️ Prepare underlying infrastructure for installing a Kubernetes cluster

🏷️ Prepare underlying infrastructure for installing a Kubernetes cluster#

prepare_node.sh

#!/bin/bash

# Verify this version is what your cluster is currently at
export SUBVER=1.33
export VER="${SUBVER}".1-1.1

# Check to see if the script has been run before. Exit out if so.
FILE=/k8scp_run
if [ -f "$FILE" ]; then
    echo "WARNING!"
    echo "$FILE exists. Script has already been. Do not run on control plane."
    echo "This should be run on the worker node."
    echo
    exit 1
else
    echo "$FILE does not exist. Running  script"
fi


# Create a file when this script is started to keep it from running
# on the control plane node.
sudo touch /k8scp_run

# Update the system
sudo apt update ; sudo apt upgrade -y

# Install required pkgs
sudo apt install curl apt-transport-https vim \
    git wget gnupg2 software-properties-common \
    apt-transport-https ca-certificates socat -y

# Add Kubernetes pkgs repo gpg key and source file
sudo mkdir -m 755 -p /etc/apt/keyrings
curl -fsSL https://pkgs.k8s.io/core:/stable:/v"${SUBVER}"/deb/Release.key | \
    sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v"${SUBVER}"/deb/ /" | \
    sudo tee /etc/apt/sources.list.d/kubernetes.list

# Install Kubernetes packages (kubeadm, kubelet, kubectl) and lock package version
sudo apt update
sudo apt -y install kubelet=$VER kubeadm=$VER kubectl=$VER
sudo apt-mark hold kubelet kubeadm kubectl

# Ensure Kubelet service is running and enabled
sudo systemctl enable --now kubelet

# Disable swap
sudo swapoff -a
sudo sed  -i 's/\(.*swap\s*sw\)/#\1/' /etc/fstab

# Ensure Kernel has modules overlay and br_netfilter
sudo modprobe overlay
sudo modprobe br_netfilter

# Ensure required modules will be loaded at boot
cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF

# Update kernel params to allow network traffic
cat <<EOF | sudo tee /etc/sysctl.d/kubernetes.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF

sudo sysctl --system

# Install containerd
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
sudo apt install containerd.io -y

# Configure (Cgroup driver systemd) and restart containerd service
sudo mkdir -p /etc/containerd
containerd config default | sudo tee /etc/containerd/config.toml
sudo sed -e 's/SystemdCgroup = false/SystemdCgroup = true/g' -i /etc/containerd/config.toml
sudo systemctl restart containerd
sudo systemctl enable containerd

# Ready to continue
sleep 3
echo
echo
echo '***************************'
echo
echo "Continue to the next step"
echo
echo "Use sudo and copy over or build a new"
echo "kubeadm join command from control plane."
echo
echo "Don't forget to edit /etc/hosts to use the alias IP"
echo
echo '***************************'
echo
echo

prepare_node.sh actions:

  • Update the system

  • Install required packages

  • Add Kubernetes packages repository gpg key and source file

  • Install Kubernetes packages (kubeadm, kubelet, kubectl) and lock package version

  • Ensure Kubelet service is running and enabled

  • Disable swap

  • Ensure Kernel has modules overlay and br_netfilter

  • Ensure required modules will be loaded at boot

  • Update kernel params to allow network traffic

  • Install containerd

  • Configure (Cgroup driver => systemd) and restart containerd service

Create a local DNS record.

 awk 'END{print}' /etc/hosts
192.168.94.73 cp-01