Kubernetes Cluster Setup Guide with Kubeadm

This guide provides instructions on how to set up a Kubernetes cluster using Kubeadm on a control plane and optionally add 1 or 2 nodes. Please follow these steps carefully to ensure a successful installation.

Prerequisites

Before you begin, ensure you have the following:

  1. Access to a virtual machine with admin permissions.
  2. The system's firewall or restrictions should be disabled or configured to allow Kubernetes traffic.

Step 1: Prepare the Environment

Load Necessary Kernel Modules

Run the following commands to load the overlay and br_netfilter modules, which are required for Kubernetes networking:

cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF

sudo modprobe overlay
sudo modprobe br_netfilter

Configure sysctl

Set up the required network settings:

cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf
net.bridge.bridge-nf-call-iptables  = 1
net.ipv4.ip_forward                 = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF

sudo sysctl --system

Step 2: Install Container Runtime

Update your package index and install containerd as the container runtime:

sudo apt-get update
sudo apt-get install containerd -y
sudo mkdir -p /etc/containerd
sudo containerd config default | sudo tee /etc/containerd/config.toml
sudo systemctl restart containerd
sudo systemctl status containerd

Ensure to disable swap:

sudo swapoff -a

Step 3: Add Kubernetes Repositories

Update your package index, install packages to allow apt to use a repository over HTTPS, and add Kubernetes' apt repository:

sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl

curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/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:/v1.28/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list

sudo apt update

Step 4: Install Kubeadm, Kubelet, and Kubectl

Install the specific versions of Kubeadm, Kubelet, and Kubectl:

sudo apt install -y kubeadm=1.28.1-1.1 kubelet=1.28.1-1.1 kubectl=1.28.1-1.1
sudo apt-mark hold kubelet kubeadm kubectl

Setting up the Control Plane

Initialize the control plane node using kubeadm:

sudo kubeadm init --pod-network-cidr=192.168.0.0/16 --kubernetes-version=1.28.0

Configure kubectl

Set up the kubeconfig file for access to your cluster:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Apply a Pod Network

Install Calico for network policies and services:

kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

Joining Nodes to the Cluster

Generate a token to join your nodes to the cluster:

kubeadm token create --print-join-command

Run the displayed kubeadm join command on each node you wish to add. Here's an example command:

sudo kubeadm join 10.0.1.101:6443 --token <token> --discovery-token-ca-cert-hash sha256:<hash>

Label Nodes (Optional)

If you have worker nodes, label them for organization:

kubectl label nodes <node-name> node-role.kubernetes.io/worker=worker

Replace <node-name> with the name of your node, such as k8s-worker1 or k8s-worker2.