Ultimate Guide: Setting Up a Kubernetes Cluster with Master and Worker Nodes on Ubuntu 24.04 for Optimal Performance

Kubernetes is a powerful open-source platform designed to automate the deployment, scaling, and operation of application containers. In this comprehensive guide, we’ll explore how to set up a Kubernetes cluster using three nodes, leveraging the flexibility and scalability of Amazon EC2 instances. We’ll ensure each step is clear and detailed, making it easy for anyone to follow along, even a fifth-grader!

Before we start, let’s go over the requirements and setup for creating our Kubernetes cluster.

  1. Two EC2 Instances: We will use one instance as the master node and one instances as worker node.
  2. Instance Type: Each instance should have at least 2GB of RAM. We recommend using the t2.medium instance type for its balance of performance and cost.

Caution: The t2.medium instance costs. Make sure to monitor your usage to avoid unexpected costs.

To begin, we need to create our EC2 instances. Follow these steps to configure your instances:

  1. Select the OS Version: Choose Ubuntu 24.04 for its stability and latest features.
  2. Select an Instance Type: Ensure each instance has at least 2GB of RAM and 2 CPUs. The t2.medium instance type is a suitable choice.
  1. Open the AWS Management Console: Navigate to the EC2 Dashboard.
  2. Launch Instance: Click on the “Launch Instance” button.
  3. Choose an Amazon Machine Image (AMI): Select “Ubuntu Server 24.04 LTS (HVM), SSD Volume Type”.
  4. Choose an Instance Type: Select “t2.medium”.
  5. Configure Instance Details:
    • Number of instances: 2
    • Network: Default VPC
    • Subnet: Choose an available subnet
    • Auto-assign Public IP: Enable
  6. Add Storage: Use the default 16GB for the root volume.
  7. Add Tags: (Optional) Add tags to identify your instances.
  8. Configure Security Group:
    • Create a new security group or use an existing one. ( Open all Ports for Learning Purpose Only )
    • Add rules to allow SSH access (port 22) from your IP address.
  9. Review and Launch: Review your settings and click “Launch”. Select an existing key pair or create a new one for SSH access, then click “Launch Instances”.

After launching, you will have three instances ready to be configured as your Kubernetes cluster.

You need to connect to each instance via SSH. Use the following command in your terminal (replace <instance-ip> with the actual public IP address of your instance):

ssh -i <your-key-pair.pem> ubuntu@<instance-ip>

Run the following commands on two instances (master and worker nodes):

Update and Upgrade Packages:

sudo apt update
sudo apt upgrade -y

Disable Swap:

    sudo swapoff -a
    sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

    Load Kernel Modules:

    sudo tee /etc/modules-load.d/containerd.conf <<EOF
    overlay
    br_netfilter
    EOF
    sudo modprobe overlay
    sudo modprobe br_netfilter

    Set Up Required sysctl Parameters:

    sudo tee /etc/sysctl.d/kubernetes.conf <<EOF
    net.bridge.bridge-nf-call-ip6tables = 1
    net.bridge.bridge-nf-call-iptables = 1
    net.ipv4.ip_forward = 1
    EOF
    sudo sysctl --system

    Install Docker

    sudo apt install -y curl gnupg2 software-properties-common apt-transport-https ca-certificates
    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"
    sudo apt update
    sudo apt install -y containerd.io

    Configure Containerd:

    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
    sudo systemctl restart containerd
    sudo systemctl enable containerd

      Add Kubernetes Repository:

      curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/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.28/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
      sudo apt update
      sudo apt install -y kubeadm=1.28.1-1.1 kubelet=1.28.1-1.1 kubectl=1.28.1-1.1

        if the above command doesn’t work, use the Snap method:

        sudo snap install kubeadm=1.28.1-1.1 --classic
        sudo snap install kubectl=1.28.1-1.1 --classic
        sudo snap install kubelet=1.28.1-1.1 --classic
        

        On the master node, run the following command to initialize the Kubernetes cluster:

        sudo kubeadm init

        Post-Initialization Setup on the Master Node

        After initialization, set up the kubeconfig for the user on the master node:

        mkdir -p $HOME/.kube
        sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
        sudo chown $(id -u):$(id -g) $HOME/.kube/config

        To enable communication between pods, you need to deploy a pod network. Here’s how to deploy Calico, a popular networking solution for Kubernetes:

        kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

        Use the kubeadm join command provided during the master node initialization on each worker node. This command will look something like this (replace the placeholders with your actual values):

        sudo kubeadm join &lt;master-node-ip&gt;:6443 --token &lt;token&gt; --discovery-token-ca-cert-hash sha256:&lt;hash&gt;
        # Dont forget to use SUDO

        To verify that the worker nodes have joined the cluster, run the following command on the master node:

        kubectl get nodes

        Congratulations! You have successfully set up a Kubernetes cluster on Ubuntu 24.04 using AWS EC2 instances. You can now start deploying your applications and exploring the powerful features of Kubernetes.

        1. Created EC2 Instances: Launched two instances for the master and worker nodes.
        2. Installed and Configured Docker: Set up Docker and containerd on all nodes.
        3. Installed Kubernetes Components: Installed kubeadm, kubelet, and kubectl on all nodes.
        4. Initialized the Cluster: Initialized Kubernetes on the master node and joined worker nodes to the cluster.
        5. Deployed a Pod Network: Set up Calico for pod communication.
        • Monitoring and Logging: Set up monitoring and logging for better cluster management.
        • Security: Regularly update and patch your nodes to ensure security.

        With these steps, setting up a Kubernetes cluster is straightforward and manageable, even for someone new to the process. Happy clustering!

        mrcloudbook.com avatar

        Ajay Kumar Yegireddi is a DevSecOps Engineer and System Administrator, with a passion for sharing real-world DevSecOps projects and tasks. Mr. Cloud Book, provides hands-on tutorials and practical insights to help others master DevSecOps tools and workflows. Content is designed to bridge the gap between development, security, and operations, making complex concepts easy to understand for both beginners and professionals.

        Comments

        Leave a Reply

        Your email address will not be published. Required fields are marked *