Introduction
Welcome to our comprehensive guide on building a new Kubernetes cluster from scratch. In this blog, we’ll take you through the entire process, step by step, and provide detailed explanations along the way. By the end of this tutorial, you’ll have gained the skills and knowledge necessary to create your very own Kubernetes clusters in real-world scenarios.
Kubernetes is a powerful container orchestration platform that allows you to manage and scale containerized applications seamlessly. Whether you’re a seasoned IT professional or just starting your journey into containerization and orchestration, building a Kubernetes cluster from scratch is a valuable skill to have.
In this guide, we’ll cover everything from setting up the virtual machine servers to initializing the cluster and adding worker nodes. Let’s dive in!
Video Solution
Step 1: Log in to the Virtual Machine Servers
The first step in building your Kubernetes cluster is to log in to your virtual machine servers using SSH. Replace <PUBLIC_IP_ADDRESS>
with the actual IP address of your servers.
ssh user@<PUBLIC_IP_ADDRESS>
Step 2: Install Packages
Before we start setting up Kubernetes, we need to ensure that our servers have all the necessary packages and configurations. These steps must be performed on all three nodes: the control plane node and the two worker nodes.
2.1 Create the Configuration File for Containerd
We’ll begin by creating a configuration file for Containerd, a container runtime that Kubernetes uses. This file defines the modules required by Containerd.
cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF
2.2 Load the Modules
Next, we load the required kernel modules for Overlay and Bridge networking.
sudo modprobe overlay
sudo modprobe br_netfilter
2.3 Set System Configurations for Kubernetes Networking
We set some sysctl configurations to ensure proper networking for Kubernetes.
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
Apply the new settings:
sudo sysctl --system
2.4 Install Containerd
Now, let’s install Containerd.
#Install dependencies
sudo apt install -y curl gnupg software-properties-common apt-transport-https ca-certificates
#Enable docker repo
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/docker.gpg
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
#install containerd
sudo apt-get update && sudo apt-get install -y containerd.io
2.5 Create the Default Configuration File for Containerd
We configure containerd so that it starts using systemd as cgroup
containerd config default | sudo tee /etc/containerd/config.toml >/dev/null 2>&1
sudo sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.toml
2.6 Restart and Enable Containerd
Restart Containerd to apply the new configuration and enable it for reboot.
sudo systemctl restart containerd
sudo systemctl enable containerd
2.7 Verify Containerd
Make sure Containerd is running without issues.
sudo systemctl status containerd
2.8 Disable Swap
Kubernetes doesn’t work well with swap enabled, so let’s turn it off.
sudo swapoff -a
2.9 Install Dependency Packages
Install some necessary packages for Kubernetes.
sudo apt-get update && sudo apt-get install -y apt-transport-https curl
2.10 Download and Add the GPG Key
Download and add the GPG key for Kubernetes.
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
2.11 Add Kubernetes to the Repository List
Add the Kubernetes repository to your package manager.
Please note that while Xenial is the most recent Kubernetes repository at the time of writing, this will change to Ubuntu 22.04 (Jammy Jellyfish) when the repository becomes available, at which point you will need to replace the term ‘xenial’ with ‘jammy’ in the ‘apt-add-repository’ command.
cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF
2.12 Update Package Listings
Update the package listings to include Kubernetes packages.
sudo apt-get update
2.13 Install Kubernetes Packages
Finally, install the Kubernetes packages. Use the version you prefer (in this example, we use version 1.28.0).
sudo apt-get install -y kubelet=1.28.0-00 kubeadm=1.28.0-00 kubectl=1.28.0-00
2.14 Turn Off Automatic Updates
Prevent automatic updates for Kubernetes packages.
sudo apt-mark hold kubelet kubeadm kubectl
Step 3: Log in to Kubernetes Worker Nodes
Log in to both worker nodes to perform the previous steps.
Step 4: Initialize the Cluster
Now that we have all the prerequisites in place, it’s time to initialize the Kubernetes cluster on the control plane node using kubeadm
.
sudo kubeadm init --pod-network-cidr 192.168.0.0/16 --kubernetes-version 1.28.0
4.1 Set kubectl Access
To interact with the cluster, we need to configure kubectl
. Create the .kube
directory, copy the admin configuration, and adjust permissions.
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
4.2 Test Kubernetes Cluster Access
Test that you can access the cluster using kubectl
.
kubectl get nodes
Step 5: Install the Calico Network Add-On
On the control plane node, we’ll install the Calico network add-on, which provides network policies for our cluster.
kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.25.0/manifests/calico.yaml
5.1 Check Kubernetes cluster Node Status
Ensure that the control plane node is up and running.
kubectl get nodes
Step 6: Join the Kubernetes Worker Nodes in the Cluster
Now, it’s time to add the worker nodes to the cluster. On the control plane node, create a token for joining and copy the kubeadm join
command.
kubeadm token create --print-join-command
Copy the entire kubeadm join
command from the control plane node and execute it on both worker nodes using sudo
.
6.1 Check Kubernetes Cluster Status
After adding the worker nodes, check the cluster status.
kubectl get nodes
Conclusion
Congratulations! You’ve successfully completed the installation of a Kubernetes cluster from scratch. Building a Kubernetes cluster is a fundamental skill for managing containerized applications at scale. Now that you have your cluster up and running, you can easily start deploying and managing your container workloads. Stay tuned for more Kubernetes-related tutorials and advanced topics in our future blogs. Happy Kubernetes orchestrating!