Deploy Kubernetes with Kubeadm 1.30.x & Calico on Ubuntu 22.04

July 15, 2024 (1y ago)

Deploy Kubernetes with Kubeadm and Calico on Ubuntu

Kubernetes, also known as k8s, is an open-source platform used for managing containerized applications. It automates container deployment, scaling, and management, making it easier for developers to build, deploy, and run their applications. Kubeadm and Calico are tools used to install and manage Kubernetes clusters.

In this guide, we will walk you through the steps required to install Kubernetes using kubeadm and Calico.

Prerequisites

Before we begin, ensure that you have the following prerequisites:

  • Hardware requirements: You will need a minimum of two nodes to create a Kubernetes cluster. Each node should have at least 2GB of RAM and 2 CPU cores.
  • Software requirements: Ensure that your nodes are running a Linux distribution that supports Kubernetes. Ubuntu 22.04 is recommended. You will also need a container runtime on each node (we install containerd below).
  • Network requirements: The nodes should be able to communicate with each other over a network. Ensure that you have a reliable network connection between the nodes.

Installation of Kubeadm and Calico: Step By Step

Step 1: Setup Hostnames (masters + workers)

To ensure that nodes can communicate with each other using their hostnames, we need to modify the /etc/hosts file on all nodes by adding the necessary host entries. You can do this by running the following command on each node:

sudo nano /etc/hosts

This will open the file in the nano text editor. Add the following lines to the end of the file, replacing the IP addresses with those of your own nodes:

192.168.100.1        master-node
192.168.100.2        worker-node1
192.168.100.3        worker-node2

Save and close the file.

Step 2: Turn off Swap Memory (masters + workers)

Kubernetes requires that swap memory be disabled on all nodes. You can turn off swap memory by running the following commands on each node:

sudo swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

Step 3: Enable IPv4 packet forwarding (masters + workers)

To manually enable IPv4 packet forwarding:

# sysctl params required by setup, params persist across reboots
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.ipv4.ip_forward = 1
EOF
 
# Apply sysctl params without reboot
sudo sysctl --system

Verify that net.ipv4.ip_forward is set to 1 with:

sysctl net.ipv4.ip_forward

Step 4: Update the repo and download required packages (masters + workers)

Next, you need to update the package repository and download all the necessary packages. Run the following commands on each node:

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

Step 5: Download the Kubernetes public signing key (masters + workers)

To download the Kubernetes public signing key, run the following commands on each node:

sudo mkdir /etc/apt/keyrings
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.30/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg

Step 6: Add the Kubernetes apt repository (masters + workers)

To add the Kubernetes apt repository, run the following command on each node:

echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.30/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list

Step 7: Install kubelet, kubeadm, and kubectl (masters + workers)

Now you’re ready to install Kubernetes on all nodes. Run the following commands on each node:

sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

This will install the necessary Kubernetes packages and mark them as held so that they don’t get automatically upgraded.

Step 8: Install containerd as the CRI (masters + workers)

Remove any conflicting Docker packages, then add the Docker repository:

for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
 
# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install containerd.io

Step 9: Configure containerd (masters + workers)

In this step, you need to create a directory for containerd, a container runtime, and copy its default configuration to a file named config.toml. You can do this with the following commands:

sudo mkdir /etc/containerd
sudo sh -c "containerd config default > /etc/containerd/config.toml"

After running these commands, you need to edit the config.toml file and find the entry that sets the value of SystemdCgroup to false. You need to change this value to true, save the file, and then restart containerd:

sudo sed -i 's/            SystemdCgroup = false/            SystemdCgroup = true/' /etc/containerd/config.toml
sudo systemctl restart containerd.service
sudo systemctl restart kubelet.service
sudo systemctl enable kubelet.service

Note that these commands need to be run as root on all nodes in your Kubernetes cluster.

Step 10: Initialize Kubeadm (master node only)

On the master node, run the command below. This will initialize the Kubernetes control plane.

sudo kubeadm config images pull
sudo kubeadm init --pod-network-cidr=10.10.0.0/16

Step 11: Copy Configuration File to User’s Directory (master node only)

On the master node, copy the configuration file to the user’s directory by running the following commands:

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

This will allow the user to interact with the Kubernetes cluster.

Step 12: Setup Calico Network (master node only)

After initializing kubeadm, you need to set up the Calico network in order to enable pod-to-pod communication. Run the following commands on the master node:

kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.28.1/manifests/tigera-operator.yaml
curl https://raw.githubusercontent.com/projectcalico/calico/v3.28.1/manifests/custom-resources.yaml -O
sed -i 's/cidr: 192\.168\.0\.0\/16/cidr: 10.10.0.0\/16/g' custom-resources.yaml
kubectl create -f custom-resources.yaml

Step 13: Join Worker Nodes (worker nodes only)

Once the master node is set up, you can join worker nodes to the cluster. When you initialize kubeadm on the master node, you will receive a token that you can use to join worker nodes to the cluster. The token should look something like this:

kubeadm join 10.0.0.2:6443 --token ndi3ae.ujwfcuais8zm2cyn --discovery-token-ca-cert-hash sha256:fc6c1094159833bf95a3fcb7d49960026e4ddad56f8648b94240cd1c867b2f6b

Copy the token and run it on all worker nodes to join them to the cluster.

Step 14: Verify the Cluster (master node only)

After all nodes have joined the cluster, you can verify that it is up and running by running the following commands on the master node:

kubectl get no
kubectl get po -A

This will display a list of all the nodes in the cluster and all the pods running in the cluster, respectively.