KubernetesCluster with kubeadm and calico (for networking) on Ubuntu 22.04

KubernetesCluster with kubeadm and calico (for networking) on Ubuntu 22.04

An open-source platform called Kubernetes, or k8s, is used to manage containerized apps. Developers can build, deploy, and run their applications more easily since it automates container deployment, scaling, and administration. To setup and maintain Kubernetes clusters, two tools are needed: Calico and Kubeadm.

We'll walk you through each step of installing Kubernetes using Calico and kubeadm in this guide.

Prerequisites:

Please make sure you have the following before we start:

Hardware requirements: To establish a Kubernetes cluster, at least two nodes are required. Each node needs to have two CPU cores and at least two gigabytes of RAM.

Software requirements: Make sure the Linux distribution your nodes are running supports Kubernetes. It is advised to use Ubuntu 20.04. Additionally, Docker needs to be installed on every node.

Network requirements: A network connection is necessary for the nodes to be able to communicate with one another. Make that all of the nodes are connected to a dependable network.

Installation of Kubeadm and Calico: Step By Step

Step 1: Setup Hostnames

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.0.1        master-node
192.168.0.2        worker-node1
192.168.0.3        worker-node2

Save and close the file.

Step 2: Turn off Swap Memory

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

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

Step 3: Set up the IPV4 bridge on all nodes

To set up the IPV4 bridge on all nodes, run the following commands on each node:

Explaincat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF

sudo modprobe overlay
sudo modprobe br_netfilter

# sysctl params required by setup, params persist across reboots
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables  = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward                 = 1
EOF

# Apply sysctl params without reboot
sudo sysctl --system

These commands will set up the necessary network bridge and kernel parameters required by Kubernetes.

Step 4: Update the repo and download all the required packages

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

Step 5: Download the Google Cloud public signing key

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

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

Step 6: Add K8s apt Repository

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

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

Step 7: Update repo & install kubelet, kubeadm, kubectl, and docker.io

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

sudo apt-get update
sudo apt install -y kubelet=1.28.1-1.1 kubeadm=1.28.1-1.1 kubectl=1.28.1-1.1 docker.io
sudo apt-mark hold kubelet kubeadm kubectl docker.io

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

Step 8: Configure containerd on all nodes

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 an entry that sets the value of “SystemdCgroup” to false. You need to change this value to true, save the file and then restart containerd with the following command:

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 9: Initialize Kubeadm

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

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

Step 10: Copy Configuration File to User’s Directory

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 11: Setup Calico Network

After initializing kubeadm, you need to setup 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.26.1/manifests/tigera-operator.yaml
curl https://raw.githubusercontent.com/projectcalico/calico/v3.26.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 12: Join Worker Nodes

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 13: Verify the Cluster

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.

Note: If you face any issue after follow my blog you can comment on post I'll reply and you can text me on LinkdIn (www.linkedin.com/in/muhammad-ahmad-devops).