DEV Community

Cover image for Create basic cluster with Kubeadm on AWS EC2 Instance

Create basic cluster with Kubeadm on AWS EC2 Instance

Installing Kubernetes 1.31 and create a cluster using kubeadm (with Containerd and Calico CNI), here's for setting up a basic cluster (1 master + N workers):

πŸ–₯️ EC2 Instance Setup for Kubeadm

Follow these steps to launch and configure EC2 instances for setting up a Kubernetes cluster using kubeadm.

βœ… Step 1: Launch EC2 Instances

  1. Login to AWS Console
  2. Navigate to EC2 > Instances > Launch Instance
  3. Configure the instance as below:

| Setting | Value |
| ------------------ | -------------------------------- |
| Name | Kubernetes |
| OS | Ubuntu 24.04 LTS |
| Instance Type | t3.medium |
| Key Pair | Create or select an existing |
| Security Group | Create or select one (see below) |


πŸ” Step 2: Create Security Group

  1. Go to VPC > Security > Security Groups
  2. Click Create Security Group
  3. Configure like below:
    • Security Group Name: kubernetes-security

πŸ”½ Inbound Rules

Type Protocol Port Range Source Description
SSH TCP 22 Anywhere (0.0.0.0/0) For SSH access
All Traffic All All Custom (your VPC CIDR) Allow all communication between nodes

πŸ“ Note: If you're testing, you can temporarily use Anywhere for "All Traffic" but limit it for production.

πŸ”Ό Outbound Rules

Type Protocol Port Range Destination Description
All Traffic All All Anywhere (0.0.0.0/0) Allow all outgoing traffic

πŸ› οΈ Step 3: Finalize Instance Launch

  1. On the Launch Instance page, under Number of Instances of summary section, set it to 2

πŸ”Έ One will be used as Control Plane, the other as Worker Node

  1. Select the kubernetes-security group you just created.
  2. Use the same key pair for both instances.
  3. Once the instances are launched, rename them for clarity:
    • controlplane
    • workernode

example:

Image description


Prerequisites for Using Kubeadm

Before using Kubeadm to initialize your Kubernetes cluster, ensure that the following requirements are available:

  • Operating System: Ubuntu, CentOS, or other Linux distributions (with a supported kernel version).
  • At least 2 GB of RAM for the master node.
  • At least 1 CPU (for both the master and worker nodes).

Connect both instances and walkthrough below detailed guide.

πŸš€ Setup Kubeadm on EC2 instances

Ensure these are done on all nodes (control plane and workers):

🧱 1. Update System Packages

sudo apt-get update
sudo apt-get upgrade -y
Enter fullscreen mode Exit fullscreen mode

πŸ“¦ 2. Install Required Packages

sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common gnupg2
Enter fullscreen mode Exit fullscreen mode

πŸ”§ 3. Disable Swap (Required for K8s)

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

βœ… Why? Kubernetes requires swap to be disabled for optimal memory management.


πŸ“¦ 4. Install and Configure containerd

sudo apt-get install -y containerd
sudo mkdir -p /etc/containerd
sudo containerd config default | sudo tee /etc/containerd/config.toml > /dev/null
Enter fullscreen mode Exit fullscreen mode

Enable SystemdCgroup:

sudo sed -i 's/SystemdCgroup = false/SystemdCgroup = true/g' /etc/containerd/config.toml
Enter fullscreen mode Exit fullscreen mode

Restart and enable service:

sudo systemctl restart containerd
sudo systemctl enable containerd
Enter fullscreen mode Exit fullscreen mode

πŸ“¦ 5. Add Kubernetes v1.31 APT Repository

curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.31/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.31/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
Enter fullscreen mode Exit fullscreen mode

πŸ“¦ 6. Install Kubernetes Components

sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
Enter fullscreen mode Exit fullscreen mode

βœ… apt-mark hold ensures these packages aren’t upgraded unintentionally.


🧠 7. Load Required Kernel Modules

cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
Enter fullscreen mode Exit fullscreen mode
sudo modprobe overlay
sudo modprobe br_netfilter
Enter fullscreen mode Exit fullscreen mode

🌐 8. Configure Network Settings for Kubernetes

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

sudo sysctl --system
Enter fullscreen mode Exit fullscreen mode

[!NOTE] Kubernetes Setup Script
The above can be saved as kubernetes-setup.sh file.
You can run this on worker nodes to avoid redundancy.


🧭 Next Steps (Master Node)

1️⃣ Initialize Kubernetes Control Plane

sudo kubeadm init --pod-network-cidr=192.168.0.0/16 --apiserver-advertise-address=$PRIVATE_IP
Enter fullscreen mode Exit fullscreen mode

2️⃣ Set up kubectl for your user

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

3️⃣ Install Calico CNI (For v1.31 Compatibility)

kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.28.1/manifests/custom-resources.yaml
kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.28.1/manifests/calico.yaml
Enter fullscreen mode Exit fullscreen mode

4️⃣ Check pods Status

kubectl get pods -A
Enter fullscreen mode Exit fullscreen mode

5️⃣ Verify Kubernetes Cluster Status

kubectl get nodes
Enter fullscreen mode Exit fullscreen mode

🧩 Join Worker Nodes

πŸ“ 1. Run the same setup script on all worker nodes.

[!NOTE]
Like mentioned above, once you create kubernetes-setup.sh file on worker node. Use below command to make script ready to run and use.

chmod +x kubernetes-setup.sh

./kubernetes-setup.sh
Enter fullscreen mode Exit fullscreen mode

πŸ“ 2. On master node instance, get the join command:

kubeadm token create --print-join-command
Enter fullscreen mode Exit fullscreen mode

πŸ“ 3. Run the join command on worker node

Copy paste the join command generated on MasterNode

sudo kubeadm join <master-ip>:6443 --token <token> --discovery-token-ca-cert-hash sha256:<hash>
Enter fullscreen mode Exit fullscreen mode

πŸ“ 4. Verify from Master

kubectl get nodes
Enter fullscreen mode Exit fullscreen mode

[!Seperate instances for control plane and worker node]
If you're intend to have separate instance for control plane and workernode with separate security groups for your nodes make sure these ports are added as inbound rules.

πŸ” AWS EC2 Security Group Settings

Ensure the following ports are open between your EC2 nodes:

Port Purpose
6443 Kubernetes API Server
2379-2380 etcd
10250 Kubelet API
10251 kube-scheduler
10252 kube-controller-manager
179 Calico BGP

βœ… Wrapping Up

That’s it!!! your kubeadm setup on EC2 is ready!

You now have a basic Kubernetes cluster with a control plane and a worker node. This setup is great for getting hands-on experience and understanding how Kubernetes works under the hood.

Feel free to explore more, try deploying apps, and break things to learn.

Thanks for following along. I really hope this guide helped! πŸ™Œ


🀝🏻 Stay Connected

If you find the content helpful, consider:

Top comments (0)