Setting Up a Kubernetes Cluster on AWS EC2 with Ubuntu 22.04 LTS and Kubeadm

Varun Kumar Manik
6 min readJan 29, 2024

Welcome to our blog, where we dive into the world of Kubernetes and cloud computing! Today, we’re going to walk through the process of setting up a Kubernetes cluster on an AWS EC2 instance running Ubuntu 22.04 LTS, using kubeadm. Whether you're a DevOps professional, a software developer, or just a tech enthusiast, this guide will help you get your Kubernetes cluster up and running on AWS.

Understanding Kubeadm and Its Benefits in Development Environments

What is Kubeadm?

kubeadm is a tool built by the Kubernetes community as a part of the Kubernetes project. It is designed to provide a simple, efficient way to set up and manage Kubernetes clusters. The primary goal of kubeadm is to simplify the setup process of Kubernetes clusters to such an extent that it becomes accessible to a wider range of users, from beginners to experienced system administrators.

Key Features of Kubeadm:

  • Simplified Cluster Setup: kubeadm automates several tasks involved in setting up a Kubernetes cluster. This includes the creation of necessary certificates, configuration of the API server, and installation of essential components like etcd, kubelet, and apiserver.
  • Standardized Cluster Creation: It follows best practices and standard configurations for setting up a Kubernetes cluster, ensuring consistency and reliability.
  • Modular and Configurable: While it simplifies setup, kubeadm also offers flexibility. You can customize various aspects of your Kubernetes cluster, such as networking, storage, and more.
  • Upgrades and Maintenance: kubeadm provides straightforward commands to upgrade your Kubernetes clusters and manage their lifecycle effectively.

Benefits of Kubeadm in Development Environments:

  1. Ease of Use: Developers can quickly set up a Kubernetes environment without delving deep into the details of Kubernetes internals. This is particularly beneficial for those new to Kubernetes.
  2. Focus on Development: By reducing the complexity of the setup process, developers can focus more on developing applications and services rather than spending time on cluster setup and configuration.
  3. Rapid Prototyping: kubeadm enables quick creation and teardown of clusters, making it ideal for testing and experimenting with different configurations or versions of Kubernetes.
  4. Learning and Experimentation: For those learning Kubernetes, kubeadm provides a straightforward path to understanding the core components of a Kubernetes cluster and how they interact with each other.
  5. Consistency with Production: Although primarily beneficial in development, the clusters set up by kubeadm are similar to production-grade clusters. This consistency helps in understanding production issues and eases the transition of applications from development to production.
  6. Community Support: Being a part of the Kubernetes project, kubeadm has strong community support, ensuring that it stays updated with the latest Kubernetes features and best practices.

Prerequisites

Before we start, make sure you have the following:

  1. An AWS Account: You’ll need an AWS account. If you don’t have one, you can sign up for a free tier account here.
  2. AWS CLI: Installed and configured on your machine. AWS CLI Installation Guide.
  3. An EC2 Instance: We’ll be using Ubuntu Server 22.04 LTS for our Kubernetes master node.
  4. Security Group Settings: Ensure your EC2 instance’s security group has the following inbound rules:
  • SSH (Port 22)
  • All traffic for Kubernetes control plane (Port 6443)
  • NodePort Services (30000–32767)

Step 1: Setting Up Your EC2 Instance

Log into your AWS console, navigate to EC2, and launch a new instance. Select Ubuntu Server 22.04 LTS and choose an instance type (t2.medium or higher is recommended). Set up your key pair and security group as per the prerequisites.

Step 2: Connecting to Your Instance

Once your instance is up and running, connect to it using SSH:

ssh -i "your-key-pair.pem" ubuntu@your-instance-public-dns

Step 3: Installing Docker

Update your package index and install Docker:

sudo apt update
sudo apt install docker.io -y
sudo systemctl start docker
sudo systemctl enable docker

Step 4: Installing Kubeadm, Kubelet, and Kubectl

Add the Kubernetes signing key and repository:

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /usr/share/keyrings/kubernetes-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list

Then, install kubeadm, kubectl, and kubelet:

sudo apt update
sudo apt install kubeadm kubelet kubectl -y
sudo apt-mark hold kubeadm kubelet kubectl

Step 5: Initializing Your Kubernetes Cluster

Disable swap memory to satisfy the kubelet prerequisites:

sudo swapoff -a

Initialize the cluster with kubeadm:

sudo kubeadm init

Follow the on-screen instructions to set up the cluster configuration.

Step 6: Setting Up Kubectl

As a regular user, set up the kubeconfig:

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

Step 7: Applying a CNI Plugin

Apply a network plugin like Calico:

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

Step 8: Verifying the Cluster

Check if the master node is up:

kubectl get nodes

Your output should show your node status as Ready.

Utilizing the Kubernetes Repository

Whether you’re a beginner or an experienced Kubernetes user, this repository is a valuable asset. Here’s how you can make the most of it:

  1. Clone the Repository: Start by cloning the repository to your local machine or cloud environment where you have Kubernetes running.
git clone https://github.com/manikcloud/k8s.git
  1. Explore and Experiment: Navigate through the various directories, read the README.md files for guidance, and try applying the configurations to your Kubernetes cluster.
  2. Customize for Your Needs: Feel free to modify and customize the YAML files and scripts according to your project requirements or personal learning objectives.
  3. Stay Updated: Keep an eye on the repository for updates, as Kubernetes is an ever-evolving platform, and we aim to keep our resources current with the latest best practices.

Navigating the Kubernetes Repository: A Guide to Key Resources

Our GitHub repository is a comprehensive resource for anyone diving into the world of Kubernetes and Docker. Here’s a sequential guide to what you’ll find in each section, complete with links to direct you to the relevant parts of the repository.

  1. Docker: Explore Docker with Dockerfiles and related scripts. This section provides the groundwork for building and managing Docker containers, a core component for running applications on Kubernetes.
  2. Pods: Delve into various Pod configurations. Understanding Pods, the smallest deployable units in Kubernetes, is essential for effectively hosting and managing Kubernetes applications
  3. ReplicaSet: Start by understanding Kubernetes ReplicaSets with YAML configurations in this folder. ReplicaSets ensure that a specified number of pod replicas are running at any given time. This is foundational for maintaining application availability and scalability.
  4. Deployment: Progress to more complex Kubernetes deployments. This section includes configurations for deploying, scaling, and updating sets of Pods, crucial for managing the lifecycle of your applications.
  5. Resources: This section is a compilation of various Kubernetes resource configurations. It’s key to grasping the broader architectural components and capabilities of Kubernetes.
  6. Services: Learn about Kubernetes Services, which allow you to expose applications running on a set of Pods as network services. This is crucial for application accessibility and network routing.
  7. Installation: Find scripts that facilitate the installation of Kubernetes and Docker. This mirrors the steps discussed in our blog and provides a practical approach to setting up your Kubernetes environment.
  8. https://github.com/manikcloud/k8s/tree/main/installation
#!/bin/bash

echo "Updating and upgrading the package index..."
sleep 2
sudo apt update

echo "Installing necessary packages for transport over HTTPS..."
sleep 2
sudo apt-get install -y apt-transport-https ca-certificates curl

echo "Installing Docker..."
sleep 2
sudo apt install docker.io -y

echo "Starting Docker and enabling it to run on boot..."
sleep 2
sudo systemctl start docker
sudo systemctl enable docker

echo "Adding the Kubernetes signing key..."
sleep 2
curl -fsSL "https://packages.cloud.google.com/apt/doc/apt-key.gpg" | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/kubernetes-archive-keyring.gpg

echo "Adding the Kubernetes APT repository..."
sleep 2
echo 'deb https://packages.cloud.google.com/apt kubernetes-xenial main' | sudo tee /etc/apt/sources.list.d/kubernetes.list

echo "Updating the package index after adding Kubernetes repository..."
sleep 2
sudo apt update

echo "Installing Kubernetes components (kubeadm, kubectl, kubelet)..."
sleep 2
sudo apt install kubeadm=1.20.0-00 kubectl=1.20.0-00 kubelet=1.20.0-00 -y

echo "Initializing Kubernetes cluster with kubeadm..."
sleep 2
sudo kubeadm init

echo "Setting up local kubeconfig..."
sleep 2
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

echo "Applying Weave Net CNI plugin..."
sleep 2
kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml


echo "Displaying the status of the nodes in the Kubernetes cluster..."
sleep 2
kubectl get nodes

echo "The Kubernetes cluster setup is complete."
  1. K8s-Kops-Installation: For those interested in kops, a widely-used tool for Kubernetes cluster management, this section offers invaluable resources and guidance. https://github.com/manikcloud/k8s/tree/main/k8s-kops-installation

Conclusion

Congratulations! You now have a single-node Kubernetes cluster running on an AWS EC2 instance. This setup is perfect for development, testing, or getting a feel for Kubernetes in a cloud environment.

Remember, for production environments, you should consider a multi-node setup for high availability and scalability.

Happy Kubernetes-ing! 🚀

--

--

Varun Kumar Manik

AWS APN Ambassador | SME of DevOps DevSecOps | Cloud Architect & Trainer | Blogger | Youtuber |Chef