upskilldevops.com

How to Install kubeadm on Ubuntu 24.04 LTS via Shell Script Method – Secure & Step-by-Step Kubernetes Cluster Setup

Introduction

Kubernetes has become the go-to orchestration tool for managing containerized applications in production. One of the fastest ways to set up a Kubernetes cluster is by using kubeadm.
In this tutorial, we will set up a Kubernetes cluster on Ubuntu 24.04 LTS using automated shell scripts — making the process faster, error-free, and repeatable for both lab and production environments.

Whether you are a DevOps student, system administrator, or cloud engineer, this guide will help you deploy Kubernetes quickly on AWS EC2, Virtual Machines, or bare metal servers.


Prerequisites for Kubernetes Cluster Setup

RequirementDescription
Number of Nodes1 Master Node, 1 or more Worker Nodes
OS VersionUbuntu 24.04 LTS (64-bit)
CPUMinimum 2 vCPUs (Master), 1 vCPU (Worker)
RAMMinimum 4GB RAM (Master), 2GB RAM (Worker)
Disk SpaceMinimum 20GB free space
NetworkAll nodes should be in the same network/subnet
Accesssudo privileges and internet access on all nodes

Security Ports for Kubernetes Setup

When setting up Kubernetes, make sure the following ports are opened in your AWS Security Group or cloud provider firewall.
No need to run commands on the server — these are network-level configurations.

PortProtocolPurpose
6443TCPKubernetes API server
2379-2380TCPetcd server client API
10250TCPKubelet API
10251TCPkube-scheduler
10252TCPkube-controller-manager
10255TCPRead-only Kubelet API
30000-32767TCPNodePort services

Step-by-Step Installation via Shell Script

We will use three scripts:

  1. Master Node Installation Script
  2. Worker Node Installation Script
  3. Uninstall Script

1. Master Node Installation Script

Run this on Master Node Only

Create the file:

nano master-install.sh

Paste the following script:

#!/bin/bash
set -e

echo "[1] Updating system packages..."
sudo apt update && sudo apt upgrade -y
# Explanation: Ensures all system packages are up to date for stability.

echo "[2] Disabling swap..."
sudo swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/' /etc/fstab
# Explanation: Kubernetes requires swap to be disabled for performance reasons.

echo "[3] Setting hostname..."
sudo hostnamectl set-hostname master-node
# Explanation: Assigns a clear hostname to identify the master node.

echo "[4] Loading kernel modules..."
sudo modprobe overlay
sudo modprobe br_netfilter
echo -e "overlay\nbr_netfilter" | sudo tee /etc/modules-load.d/k8s.conf
# Explanation: Required for container networking in Kubernetes.

echo "[5] Configuring sysctl parameters..."
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
sudo sysctl --system
# Explanation: Enables packet forwarding and bridge networking.

echo "[6] Installing containerd..."
sudo apt install -y containerd
sudo mkdir -p /etc/containerd
containerd config default | sudo tee /etc/containerd/config.toml
sudo systemctl restart containerd
sudo systemctl enable containerd
# Explanation: containerd is the container runtime for Kubernetes.

echo "[7] Installing kubeadm, kubelet, kubectl..."
sudo apt install -y apt-transport-https ca-certificates curl gpg
sudo mkdir -p /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
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
sudo apt update
sudo apt install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
# Explanation: Installs Kubernetes components and prevents accidental upgrades.

echo "[8] Initializing Kubernetes..."
sudo kubeadm init --pod-network-cidr=192.168.0.0/16
# Explanation: Sets up Kubernetes control plane with Calico-compatible network range.

echo "[9] Configuring kubectl for the current user..."
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
# Explanation: Allows running kubectl commands without sudo.

echo "[10] Installing Calico CNI..."
kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.25.0/manifests/calico.yaml
# Explanation: Installs Calico for pod networking.

echo "[DONE] Master Node setup complete. Save the kubeadm join command for Worker Nodes."

Run the script:

chmod +x master-install.sh
./master-install.sh

2. Worker Node Installation Script

Run this on Worker Node Only

Create the file:

nano worker-install.sh

Paste the following script:

#!/bin/bash
set -e

echo "[1] Updating system packages..."
sudo apt update && sudo apt upgrade -y
# Explanation: Keeps system packages updated.

echo "[2] Disabling swap..."
sudo swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/' /etc/fstab
# Explanation: Disables swap for Kubernetes compatibility.

echo "[3] Setting hostname..."
sudo hostnamectl set-hostname worker-node
# Explanation: Assigns a clear hostname for the worker node.

echo "[4] Loading kernel modules..."
sudo modprobe overlay
sudo modprobe br_netfilter
echo -e "overlay\nbr_netfilter" | sudo tee /etc/modules-load.d/k8s.conf
# Explanation: Required for container networking.

echo "[5] Configuring sysctl parameters..."
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
sudo sysctl --system
# Explanation: Enables packet forwarding and bridge networking.

echo "[6] Installing containerd..."
sudo apt install -y containerd
sudo mkdir -p /etc/containerd
containerd config default | sudo tee /etc/containerd/config.toml
sudo systemctl restart containerd
sudo systemctl enable containerd
# Explanation: Installs container runtime.

echo "[7] Installing kubeadm, kubelet, kubectl..."
sudo apt install -y apt-transport-https ca-certificates curl gpg
sudo mkdir -p /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
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
sudo apt update
sudo apt install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
# Explanation: Installs Kubernetes components.

echo "[8] Join the cluster..."
echo "Run the kubeadm join command from the Master Node here."
# Explanation: This connects the worker to the Kubernetes control plane.

Run the script:

chmod +x worker-install.sh
./worker-install.sh

3. Uninstall Script (All Nodes)

Create the file:

nano uninstall-k8s.sh

Paste:

#!/bin/bash
set -e

echo "[1] Resetting Kubernetes..."
sudo kubeadm reset -f
# Explanation: Resets Kubernetes cluster configuration.

echo "[2] Removing packages..."
sudo apt purge -y kubeadm kubectl kubelet kubernetes-cni containerd
sudo apt autoremove -y
# Explanation: Removes Kubernetes components and dependencies.

echo "[3] Cleaning configuration..."
sudo rm -rf ~/.kube
# Explanation: Deletes Kubernetes config for current user.

echo "[DONE] Kubernetes uninstalled successfully."

Run the script:

chmod +x uninstall-k8s.sh
./uninstall-k8s.sh

Conclusion

By using these shell scripts, you can automate the entire Kubernetes installation process on Ubuntu 24.04 LTS — saving time and reducing manual errors. This method is perfect for classroom labs, AWS setups, or production environments.

If you have questions, feedback, or need support, feel free to reach out at: 📧 devopsbyrushi@gmail.com


If you want, I can now add diagrams for Master-Worker communication & port usage so your blog looks even more professional and SEO-rich.
Do you want me to prepare those visuals?

Leave a Comment