cd ..
CLOUD

Preparing AWS EC2 instances for a Kubernetes cluster

Before running kubeadm init, we need to prepare the machines that will form the cluster. They must be able to communicate, download packages, and run containers.

This article details the infrastructure preparation for the Kubernetes lab on AWS with kubeadm, following the studies of PICK – Programa Intensivo de Containers e Kubernetes (Intensive Container and Kubernetes Program), from LINUXtips.

The environment we will prepare

We will use three EC2 instances with Ubuntu: one for the control plane and two for the workers.

InstanceFuture roleLab configuration
k8s-cpControl plane2 vCPUs, at least 4 GiB of RAM, and 20 GiB of disk.
k8s-worker-1Worker2 vCPUs, at least 4 GiB of RAM, and 20 GiB of disk.
k8s-worker-2Worker2 vCPUs, at least 4 GiB of RAM, and 20 GiB of disk.

Choose an x86_64 type with these resources available in the region. The size is a reference for this lab; larger applications require different sizing.

We will have a single control plane, without high availability. This installation is managed by us on EC2; to learn about the cluster and complete its installation, follow the article indicated at the end.

1. Choosing the region and network

In the AWS console, select the region you intend to work in. Use the same region in all steps.

In the VPC service, check if there is a suitable VPC for the lab. You can use the default VPC, if available, or create a dedicated network.

A possible organization is:

VPC: 172.31.0.0/16
└── Public Subnet: 172.31.10.0/24
    ├── k8s-cp
    ├── k8s-worker-1
    └── k8s-worker-2

Future Pods Network: 10.244.0.0/16
Future Services Network: 10.96.0.0/12

These addresses are examples. If using an existing VPC, keep its actual CIDRs and ensure they do not overlap with the Pods, Services, or VPN-connected network ranges.

Checking the public subnet

In the VPC console:

  1. Open Subnets and select the chosen subnet.
  2. Check the VPC and the associated route table.
  3. In the route table, verify the local VPC route and a 0.0.0.0/0 route pointing to an Internet Gateway.
  4. Under Internet Gateways, confirm that this gateway is attached to the VPC.

For direct SSH access in this guide, each instance will also receive a public IPv4. The route to the gateway, by itself, does not provide a public address to the machine. Internet Gateways documentation.

The machines will communicate with each other using private IPs. The public IP will be used for administrative access from your computer.

2. Creating the Security Group

In the EC2 console, open Security Groups, choose to create a group, and configure:

Create the group and then edit its inbound rules:

TypeProtocol/portSource
SSHTCP 22Your public IP with /32, using the My IP option.
All trafficAllThe ID of the k8s-lab group itself.

The second rule allows traffic between interfaces associated with this group, which is necessary for node communication and the Pod network. It does not authorize any internet address. Use this group exclusively for the three lab instances.

Keep outbound traffic open in this example to download packages and images. Security Groups documentation.

SSH access is restricted to your address. It is not necessary to expose the Kubernetes API or etcd to the public to follow the lab. If your IP changes, update the SSH rule.

This broad internal rule simplifies the study. A deployment with specific rules should consider the Kubernetes component ports and the chosen network plugin.

3. Creating the SSH key

In the EC2 console, open Key Pairs and create a key:

FieldExample value
Namek8s-lab-key
TypeRSA
Format.pem, for OpenSSH

Save the downloaded file in a private folder on your computer. The private key should not be uploaded to the blog repository or copied to the workers. Key pair creation.

4. Creating the three instances

In the EC2 console, choose Launch instance. To facilitate identification, create one machine at a time, repeating the configuration:

  1. Under Name and tags, use k8s-cp, then k8s-worker-1, and k8s-worker-2.
  2. Under Application and OS Images, choose Ubuntu Server 24.04 LTS, x86_64, from the official Canonical source.
  3. Select an instance type with the resources defined at the beginning of the article.
  4. For Key pair, select k8s-lab-key.
  5. In Network settings, choose the VPC and public subnet checked previously.
  6. Enable Auto-assign public IP and select the existing Security Group k8s-lab.
  7. Configure the root volume with 20 GiB, type gp3. For this disposable environment, check the option to delete the volume when terminating the instance.
  8. Review the settings and launch the machine.

Wait for the instances to be Running and pass status checks. EC2 instance creation guide.

Record the addresses of each node:

NamePrivate IPPublic IP
k8s-cpCopy from consoleCopy from console
k8s-worker-1Copy from consoleCopy from console
k8s-worker-2Copy from consoleCopy from console

Instances, storage, and public IPv4s may incur charges. Check your account’s values and benefits; the lab does not assume Free Tier coverage.

5. Accessing via SSH

In your computer’s terminal, adjust the key permission and connect using the actual address:

chmod 400 /caminho/k8s-lab-key.pem
ssh -i /caminho/k8s-lab-key.pem ubuntu@<IP-PUBLICO-DO-NO>

The Ubuntu AMI user is ubuntu. On the first connection, verify the host’s identity according to the instance information. Repeat access for all three machines. SSH connection prerequisites.

All subsequent commands are executed inside the instances, via SSH.

6. Preparing Ubuntu on each node

Setting unique names

Execute the corresponding command on each machine:

# Somente no control plane
sudo hostnamectl set-hostname k8s-cp
# Somente no primeiro worker
sudo hostnamectl set-hostname k8s-worker-1
# Somente no segundo worker
sudo hostnamectl set-hostname k8s-worker-2

Reconnect via SSH and check with hostname. The EC2 Name tag is for identification in the console; it does not replace the Ubuntu hostname configuration.

Updating and disabling swap

On all three new instances:

sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install -y ca-certificates curl gpg
sudo swapoff -a
swapon --show
sudo nano /etc/fstab

Comment out only the swap entries, if they exist. This keeps swap disabled after reboot. If /var/run/reboot-required exists after the update, reboot the machine and reconnect before continuing.

Configuring kernel modules and network

cat <<'EOF' | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF

sudo modprobe overlay
sudo modprobe br_netfilter

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

These parameters prepare the host for the cluster’s network roadmap. Files in /etc preserve settings after boot.

7. Preparing the containerd runtime

Still on all three machines:

sudo apt-get install -y containerd
containerd --version
sudo mkdir -p /etc/containerd
containerd config default | sudo tee /etc/containerd/config.toml > /dev/null
sudo nano /etc/containerd/config.toml

In the generated configuration for the runc runtime, change the existing option to:

SystemdCgroup = true

Ensure cri does not appear in disabled_plugins. The file section changes between containerd 1.x and 2.x; use the configuration generated by the installed version.

sudo systemctl enable --now containerd
sudo systemctl restart containerd
systemctl is-active containerd

We expect active to be returned. The runtime’s cgroup driver must be compatible with that of the kubelet; this guide adopts systemd. Container runtimes documentation.

8. Checking the preparation

On each node:

hostname
nproc
free -h
df -h /
swapon --show
lsmod | grep -E 'overlay|br_netfilter'
sysctl net.ipv4.ip_forward
sysctl net.bridge.bridge-nf-call-iptables
systemctl is-active containerd
curl -I https://pkgs.k8s.io

Check available resources, disabled swap, loaded modules, network parameters set to 1, and active runtime. curl verifies HTTPS access to the repository; it does not replace an image download test.

To verify private communication, run from one node to another’s private IP:

ping -c 3 <IP-PRIVADO-DO-OUTRO-NO>

Ping checks ICMP, not all Kubernetes ports. If it fails, check Security Group, NACLs, routes, and local firewall. Do not proceed with the installation while connectivity between machines is incorrect.

ProblemWhere to check
SSH times outPublic IP, route to gateway, SSH rule, and your current IP.
Permission denied (publickey)User ubuntu, key path, and selected key pair.
Packages don’t downloadDNS, outbound route, public IP, and outbound rules.
containerd doesn’t startConfiguration file and sudo journalctl -u containerd -n 100 --no-pager.

Next step: install Kubernetes

With the three machines prepared, follow the cluster creation article with kubeadm starting from the installation of kubeadm, kubelet, and kubectl.

Next comes the control plane initialization, the Pod network installation, and the workers joining. The official kubeadm requirements help review the preparation.

The guide in this article was not executed on an AWS account for validation. When concluding the study, terminate the instances and check for remaining volumes and addresses to avoid continuous charges.

References

What did you think?