Information Source: OpenSourceEducation.net By Babar Zahoor
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.
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 20.04 is recommended. You will also need to install Docker on each node.
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
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:
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:
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
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
# 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.