Skip to content

Instantly share code, notes, and snippets.

@eeganlf
Created June 21, 2023 03:00
Show Gist options
  • Save eeganlf/8f7a1dd5c14b55d9a47fafb4fdb7f8da to your computer and use it in GitHub Desktop.
Save eeganlf/8f7a1dd5c14b55d9a47fafb4fdb7f8da to your computer and use it in GitHub Desktop.
#!/bin/bash
################# LFD459:1.25.1 s_02/k8scp.sh ################
# The code herein is: Copyright the Linux Foundation, 2022
#
# This Copyright is retained for the purpose of protecting free
# redistribution of source.
#
# URL: https://training.linuxfoundation.org
# email: [email protected]
#
# This code is distributed under Version 2 of the GNU General Public
# License, which you should have received with the source.
#Version 1.26.1
#
# This script is intended to be run on an Ubuntu 20.04,
# 2cpu, 8G.
# By Tim Serewicz, 05/2022 GPL
# Note there is a lot of software downloaded, which may require
# some troubleshooting if any of the sites updates their code,
# which should be expected
# 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 run on control plane."
# 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
# twice on same node
# sudo touch /k8scp_run
# Update the system
sudo apt-get update ; sudo apt-get upgrade -y
# Install necessary software
sudo apt-get install curl apt-transport-https vim git wget gnupg2 software-properties-common apt-transport-https ca-certificates -y
# Add repo for Kubernetes
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
# Install the Kubernetes software, and lock the version
sudo apt-get update
sudo apt-get -y install kubelet=1.27.1-00 kubeadm=1.27.1-00 kubectl=1.27.1-00
sudo apt-mark hold kubelet kubeadm kubectl
# Ensure Kubelet is running
sudo systemctl enable --now kubelet
# Disable swap just in case
sudo swapoff -a
# Ensure Kernel has modules
sudo modprobe overlay
sudo modprobe br_netfilter
# Update networking to allow 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
# Configure containerd settings
cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF
sudo sysctl --system
# Install the containerd software
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-get update
sudo apt-get install containerd.io -y
# Configure containerd and restart
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
# Create the config file so no more errors
# Install and configure crictl
export VER="v1.26.0"
wget https://github.com/kubernetes-sigs/cri-tools/releases/download/$VER/crictl-$VER-linux-amd64.tar.gz
tar zxvf crictl-$VER-linux-amd64.tar.gz
sudo mv crictl /usr/local/bin
# Set the endpoints to avoid the deprecation error
sudo crictl config --set \
runtime-endpoint=unix:///run/containerd/containerd.sock \
--set image-endpoint=unix:///run/containerd/containerd.sock
# Configure the cluster
sudo kubeadm init --pod-network-cidr=192.168.0.0/16 | sudo tee /var/log/kubeinit.log
# Configure the non-root user to use kubectl
mkdir -p $HOME/.kube
sudo cp -f /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
# Use Calico as the network plugin
kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.25.0/manifests/calico.yaml
# Add Helm to make our life easier
wget https://get.helm.sh/helm-v3.9.0-linux-amd64.tar.gz
tar -xf helm-v3.9.0-linux-amd64.tar.gz
sudo cp linux-amd64/helm /usr/local/bin/
sleep 15
# Output the state of the cluster
kubectl get node
# Ready to continue
sleep 3
echo
echo
echo '***************************'
echo
echo "Continue to the next step"
echo
echo '***************************'
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment