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.
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 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 (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 command 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 all the 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 Google Cloud public signing key (masters + workers)
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.30/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
Step 6: Add K8s apt Repository (masters + workers)
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.30/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
Step 7: Update repo & install kubelet, kubeadm, kubectl (masters + workers)
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-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 CRI: (masters + workers)
Add docker repo:
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 8: 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 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 ( just Master nodes)
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 ( just Master nodes)
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 ( just Master nodes)
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.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 12: Join Worker Nodes (only 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 (only master node)
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.
A great article. A suggestion is to create shell scripts for installation and insert its link into this post. I hope that will help a lot of people having less time available to setup k8s cluster.
I am working on it. Thanks for your feedback.
Great documentation. very helpful. I have done easily setup k8s clustors.
Thank you for your kind words! I’m glad you found my documentation helpful. Setting up a Kubernetes cluster can be a daunting task, so I’m happy to hear that my guide made it easier for you.
Thank you brother. Finally i solved my errors
Nice to hear, What was the errors?