Setting up a Kubernetes cluster on ubuntu server
Table of contents
Hey there,
i tried to create a kubernetes cluster and i faced some difficulties along the way. First of all i tried to create it using two laptops in our office. But the issue i faced after everything i tried still the versions of kubeadm , kubectl and kubelet were different on both of the laptops.
So, i ditched the plan for using physical laptops and used AWS EC2 instances
The 2nd issue i faced with EC2 is i used 2GB ram which is not ideal for kubernetes.
I finally used two EC2 with 4GB ram one for master node and one for worker node.
Table of Contents
Prerequisites
Installation Steps
Update & Upgrade
Install Docker
Install Kubernetes Components
Disable Swap
Initialize Master Node
Configure kubectl
Install a Pod Network
Join Worker Nodes (Up to this point)
Conclusion
The Prerequisites
Before we jump into the installation, make sure you’ve got the following ready:
Ubuntu 24.04 LTS installed and running
At least 2 GB of RAM (4 GB or more recommended)
A user with sudo privileges
Access to the terminal
Got it all? Great! Let’s get started.
Installation Steps
Step 1: Update & Upgrade
First things first, let’s update your system to make sure everything is current:
sudo apt update && sudo apt upgrade -y
This command updates your package list and upgrades all installed packages to their latest versions.
Step 2: Install Docker
Kubernetes relies on Docker to manage containers. Let’s install Docker:
sudo apt install -y docker.io
After installation, verify Docker is up and running:
sudo docker --version
Step 3: Install Kubernetes Components
We need to add the Kubernetes repository and install some essential components. First, add the Kubernetes repository:
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
Add the GPG key for the repository:
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.30/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
Next, update your package lists and install the Kubernetes components:
sudo apt update
sudo apt install -y kubelet kubeadm kubectl
To prevent these packages from being automatically updated, lock their versions:
sudo apt-mark hold kubelet kubeadm kubectl
Step 4: Disable Swap
Kubernetes prefers to have swap disabled. Here’s how to turn it off:
sudo swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
Step 5: Initialize the Master Node
With swap disabled, it’s time to initialize your Kubernetes master node. Run:
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
This command sets up the Kubernetes control plane. After it completes, you’ll see instructions for the next steps. Follow these to set up kubectl
:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Step 6: Configure kubectl
Ensure kubectl
is properly configured for your user by running:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
You can now check the status of your cluster nodes:
kubectl get nodes
Step 7: Install a Pod Network
For your cluster to function properly, you need to set up a pod network. We’ll use Flannel for this:
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
This step ensures your pods can communicate with each other.
Step 8: Join Worker Nodes (Until Here)
To add worker nodes to your cluster, you’ll need to install Docker and Kubernetes components on each worker node, just like you did on the master node. Then, execute the join command on each worker node. This command is provided during the master node initialization process and looks something like this:
sudo kubeadm join <master-ip>:<master-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>
Replace <master-ip>
, <master-port>
, <token>
, and <hash>
with the actual values from your master node’s output.
Conclusion
Congratulations! 🎉 You’ve just set up a Kubernetes cluster on ubuntu server and are ready to add worker nodes. This is a huge step towards mastering container orchestration. Next, you can dive into deploying applications, scaling your services, and exploring more advanced features.
If you run into any issues or have questions, feel free to drop a comment. Happy clustering! 🖥️
Subscribe to my newsletter
Read articles from Pratyukt Writes directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by