# This script is for setting up a simple kubeadm Kubernetes cluster
## 1. Create machines
```
# Create 1 controle plane and 1 worker machine in your infrastructure of choice.
# This example is for AWS Ubuntu 22.04 (replace with own amis, sgids, sids)
# Create control plane instance
aws ec2 run-instances \
--image-id ami-04a5bacc58328233d \
--count 1 \
--instance-type m5.large \
--key-name arch-kube \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=KubeCPNode}]' \
--security-group-ids sg-0441b23c7207c187e \
--subnet-id subnet-0c115241 \
--associate-public-ip-address
# Create worker instance
aws ec2 run-instances \
--image-id ami-04a5bacc58328233d \
--count 1 \
--instance-type m5.xlarge \
--key-name arch-kube \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=KubeWorkerNode}]' \
--security-group-ids sg-0441b23c7207c187e \
--subnet-id subnet-0c115241 \
--associate-public-ip-address
# get the instance IPs
CP_PUBLIC_IPS=$(aws ec2 describe-instances \
--filters "Name=tag:Name,Values=KubeCPNode" \
--query "Reservations[].Instances[].PublicIpAddress" \
--output text | tr '\t' ',' | tr ' ' ',')
WORKER_PUBLIC_IPS=$(aws ec2 describe-instances \
--filters "Name=tag:Name,Values=KubeWorkerNode" \
--query "Reservations[].Instances[].PublicIpAddress" \
--output text | tr '\t' ',' | tr ' ' ',')
```
## 2. Update OS (do this on all instances)
```
# SSH into the instance
sudo apt update && sudo DEBIAN_FRONTEND=noninteractive apt upgrade -y && sudo reboot
```
## 3. Install kubeadm and its dependencies (do this on all instances)
```
# SSH into the instance
sudo apt install -y apt-transport-https ca-certificates curl
# Install dependencies
sudo modprobe overlay
sudo modprobe br_netfilter
# Enable required sysctl params
sudo tee /etc/modules-load.d/containerd.conf <<EOF
overlay
br_netfilter
EOF
sudo tee /etc/sysctl.d/99-kubernetes-cri.conf <<EOF
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF
sudo sysctl --system
# Install containerd
sudo apt install -y containerd
# Configure containerd
sudo mkdir -p /etc/containerd
sudo containerd config default | sudo tee /etc/containerd/config.toml
# Restart containerd
sudo systemctl restart containerd
sudo systemctl enable containerd
# Disable swap
sudo swapoff -a
sudo sed -i '/ swap / s/^/#/' /etc/fstab
# Add Kubernetes APT repository key
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.32/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
# Add Kubernetes APT repository
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.32/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
# Update the package index
sudo apt update
# list available versions
sudo apt-cache madison kubeadm
# Install specific version (v1.32.0 — adjust patch version if needed)
KUBE_VERSION=1.32.4-1.1
sudo apt install -y kubelet=$KUBE_VERSION kubeadm=$KUBE_VERSION kubectl=$KUBE_VERSION
# Prevent them from being upgraded automatically
sudo apt-mark hold kubelet kubeadm kubectl
# Check versions
kubeadm version
kubectl version --client
kubelet --version
# Configure containerd for systemd
sudo sed -i 's/SystemdCgroup *= *false/SystemdCgroup = true/' /etc/containerd/config.toml
# Restart containerd
sudo systemctl restart containerd
```
## 4. Initialize the control plane (do this on the first control plane node only)
```
sudo kubeadm init --pod-network-cidr=192.168.0.0/16
sudo systemctl enable --now kubelet
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
source <(kubectl completion bash) # set up autocomplete in bash into the current shell, bash-completion package should be installed first.
echo "source <(kubectl completion bash)" >> ~/.bashrc # add autocomplete permanently to your bash shell.
alias k=kubectl
complete -o default -F __start_kubectl k
# Install Calico CNI
kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.29.3/manifests/calico.yaml
# Copy the join command that kubeadm generated for the worker node (see kubeadm output above)
# or you can run the following command to generate a new token
# Create a token for the worker node
# sudo kubeadm token create --print-join-command
```
## 5. Join a worker node
```
# Paste your own join command from end of step 4
sudo kubeadm join 172.20.55.87:6443 --token dr4ver.rkgx1j1f6h2zjuy0 \
--discovery-token-ca-cert-hash sha256:d797ae33c281a9cc0911533bb3d0f152d0f91fefe804092f5658cb8f22e9aba2
```