Setting up a Kubernetes Cluster with Kubeadm on AWS EC2 Instances

Setting up a Kubernetes Cluster with Kubeadm on AWS EC2 Instances

ยท

5 min read

Kubernetes has emerged as a powerful platform for orchestrating containerized applications, providing scalability, fault tolerance, and ease of management. In this step-by-step guide, we will walk you through the process of setting up a Kubernetes cluster using Kubeadm, specifically on AWS EC2 instances. We will utilize an Ubuntu-based t2.medium instance for the master node and a t2.micro instance for the worker nodes.

๐Ÿ”ถ Prerequisites:

Before we dive into the cluster setup, make sure you have the following prerequisites in place:

  1. AWS Account: Access to an AWS account with appropriate permissions to create EC2 instances.

  2. SSH Key Pair: Generate an SSH key pair to securely access the EC2 instances.

  3. AWS EC2 Instances: Launch an Ubuntu-based t2.medium instance for the master node and t2.micro instances for the worker nodes.

  4. Security Group Configuration: Set up inbound rules to allow SSH (port 22) and Kubernetes communication (ports 6443, 2379-2380, and 10250-10252).

๐Ÿ”ถ Setting up the Master Node:

  1. Launch an EC2 instance:

    • Choose the Ubuntu 23.10 LTS AMI and the t2.medium instance type.

    • Configure security groups to allow SSH and Kubernetes communication ports.

  2. Connect to the master node:

    Use SSH with the generated key pair to access the EC2 instance.

  3. Update the system packages:

     sudo apt-get update
    
  4. Install Docker:

     sudo apt-get install docker.io -y
    
  5. Start and enable Docker service:

     sudo systemctl start docker
     sudo systemctl enable docker
    
  6. Add your user to the Docker group:

     sudo usermod -aG docker ubuntu
    
  7. Restart Docker:

     sudo systemctl restart docker
    
  8. Disable swap memory:

     sudo swapoff -a
    
  9. Comment out the swap entry in /etc/fstab:

     sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
    
  10. Enable bridged traffic to pass through iptables:

    sudo sysctl net.bridge.bridge-nf-call-iptables=1
    
  11. Import the Kubernetes repository signing key:

    curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
    
  12. Add the Kubernetes repository:

    cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
    deb https://apt.kubernetes.io/ kubernetes-xenial main
    EOF
    
  13. Update the package list:

    sudo apt-get update -y
    
  14. Install specific versions of Kubeadm, Kubectl, and Kubelet:

    sudo apt install kubeadm=1.20.0-00 kubectl=1.20.0-00 kubelet=1.20.0-00 -y
    

  15. Initialize the Kubernetes cluster using Kubeadm:

    sudo kubeadm init
    

  16. Set the KUBECONFIG environment variable:

    export KUBECONFIG=/etc/kubernetes/admin.conf
    
  17. Deploy the Weave network plugin for pod networking:

    sudo kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml
    

๐Ÿ”ถ Setting up the Worker Nodes

  1. Launch EC2 instances:

    • Choose the Ubuntu 23.10 LTS AMI and the t2.micro instance type.

    • Configure security groups to allow SSH and Kubernetes communication ports.

  2. Connect to each worker node:

    • Use SSH with the generated key pair to access the EC2 instances.
  3. Follow steps 3-10 from the "Setting up the Master Node" section to configure Docker and system settings.

  4. Install specific versions of Kubeadm, Kubectl, and Kubelet:

     sudo apt install kubeadm=1.20.0-00 kubectl=1.20.0-00 kubelet=1.20.0-00 -y
    
  5. Perform pre-flight checks to ensure the worker node is ready to join the cluster:

     sudo kubeadm reset pre-flight checks
    

๐Ÿ”ถ Joining Worker Nodes to the Cluster

  1. On the master node, generate the join command by executing:

     sudo kubeadm token create --print-join-command
    

  2. Copy the generated join command.

  3. On each worker node, paste and run the join command obtained from the master node don't forget to add --v=5 at the end of the token.

๐Ÿ”ถ Verifying the Cluster

  1. Switch back to the master node.

  2. Verify that all nodes have successfully joined the cluster:

     kubectl get nodes
    

    The output should display all the nodes in the cluster, including the master node and worker nodes.

In conclusion, this guide has provided a step-by-step walkthrough of setting up a Kubernetes cluster using Kubeadm on AWS EC2 instances. Kubernetes has become a fundamental platform for orchestrating containerized applications, offering scalability, fault tolerance, and effective management.

Here's a summary of the key steps and achievements in this process:

Prerequisites: You ensured that the necessary prerequisites were in place, including an AWS account, SSH key pair, appropriate EC2 instances, and the configuration of security groups. Additionally, you installed the AWS CLI on your local machine.

Setting up the Master Node: You started by launching the master node and performed the following tasks:

  1. Installed Docker and enabled it as a service.

  2. Added your user to the Docker group for permissions.

  3. Disabled swap memory.

  4. Enabled bridged traffic to pass through iptables.

  5. Added the Kubernetes repository and installed specific versions of Kubeadm, Kubectl, and Kubelet.

  6. Initialized the Kubernetes cluster with Kubeadm.

  7. Deployed the Weave network plugin for pod networking.

Setting up the Worker Nodes: The process for setting up the worker nodes was almost identical to that of the master node. You launched worker nodes, configured Docker and system settings, and installed specific versions of Kubeadm, Kubectl, and Kubelet.

Joining Worker Nodes to the Cluster: On the master node, you generated a join command and copied it. On each worker node, you pasted and executed this command to join them to the cluster.

Verifying the Cluster: Finally, you confirmed the successful formation of the cluster by checking the status of all nodes. The output included the master node and worker nodes, indicating a properly functioning Kubernetes cluster.

This guide equips you with the knowledge and hands-on experience needed to create a Kubernetes cluster on AWS, a fundamental skill for managing containerized applications efficiently and effectively. The Kubernetes cluster offers a robust platform for container orchestration, enabling you to deploy and manage applications at scale with reliability and flexibility.


Happy Learning :)

Stay in the loop with my latest insights and articles on cloud โ˜๏ธ and DevOps โ™พ๏ธ by following me on Hashnode, LinkedIn (https://www.linkedin.com/in/chandreshpatle28/), and GitHub (https://github.com/Chandreshpatle28).

Thank you for reading! Your support means the world to me. Let's keep learning, growing, and making a positive impact in the tech world together.

#Git #Linux Devops #Devopscommunity #python #docker #Jenkins #Kubernetes #Terraform #AWS #Grafana

Did you find this article valuable?

Support Chandresh Patle's Blog by becoming a sponsor. Any amount is appreciated!

ย