Setting up an EKS cluster on AWS with Spot nodes

Setting up an EKS cluster on AWS with Spot nodes

August 18, 20217 min readSri VishnuvardhanSri Vishnuvardhan

In this article, we are going to see about Kubernetes and its use cases and also how to set up our own EKS cluster on AWS & integrate spot instances with EKS cluster.

If you want to know about Kubernetes, then you have to know the concepts of Containerization.

OS Provisioning

There are four ways to provision the OS.

  • Baremetal
  • Virtual machines
  • Containers
  • AWS instances

Baremetal

Bare metal is the oldest way to provision the OS. It has a feature of Single tenancy, which means it is dedicated to a single customer and isn’t shared between tenants.

Due to this feature, generally, Bare-metal servers are considered more secure and private.

Example: Windows in PC

But it has also had drawbacks. Since, the total amount of RAM, CPU and Hard disk is dedicated to a single OS/Server. In idle time, these resources are not utilized totally so it is not so efficient way of the utilization of Computing and Storage units.

Also, the time required to installing and booting the OS is high, which is not suited in today’s fast-moving agile world.

There comes the concept of Virtual Machines.

Virtual Machines

Virtual Machine uses a hypervisor that allows one host computer to support multiple guest VMs by virtually sharing its resources, such as memory and processing, hence providing the feature of multi-tenancy.

The example of hypervisors includes VMware, Hyper-V and Oracle Virtualbox.

Virtual Machine (VM) is a compute resource that uses software instead of a physical computer to run programs and deploy apps.

Each virtual machine runs its own operating system and functions separately from the other VMs, even when they are all running on the same host. We can even run a Virtual Linux OS on a physical PC.

In this way, the computing resources and storage resources can be effectively utilized in VM than bare-metal. But, still, the time to boot and install the Virtual OS should be optimized.

There comes the concept of Containerization.

Containerization

Containerization allows developers to create and deploy applications faster and more securely. It can launch an environment for developers within a fraction of seconds.

In simple words, Containerization is the encapsulation of an application and its required environment.

Virtual machines are often managed by a hypervisor, whereas container systems provide shared operating system services from the underlying host and isolate the applications using virtual-memory hardware.

The Virtual running OS is called Containers and the OS image which is used to create containers is called Container image.

Docker, Podman, CRI-O and Rocker are some of the popular container runtime tools used to create the containers.

But, this Containerization has also some drawbacks, in the real world there are tons of containers running as microservices and there is no proper management for containers for its disaster recovery and auto-scaling of containers based on demand and supply.

Here comes the real hero..Kubernetes.

Kubernetes

Kubernetes, also known as K8s (Between K and S in the word Kubernetes, there are 8 letters between it), is an open-source system for automating deployment, scaling, and management of containerized applications.

Kubernetes uses Master and Worker node architecture to give PaaS services such as deployment, scaling, load balancing, and lets users integrate their logging, monitoring, and alerting solutions.

Master and worker nodes constitute a single-node cluster. Officially, Kubernetes claims to support clusters with up to 5000 nodes.

Kubernetes operates at the container level rather than at the hardware level. In the Kubernetes cluster, a container is wrapped by the cover called Pods. One Pod has one or more containers inside in it.

Kubernetes Cluster setup

The cluster has two parts such as Control plane and the worker plane. Usually, Pods and Containers run only on worker plane. Both plane has their own set of components.

Control Plane Components

  • kube-apiserver — Acts as a front end to get requests from clients.
  • etcd — Datastore to store all cluster data
  • kube-scheduler — Schedule the pod to its respected node
  • kube-controller-manager — Runs all background processes creating pods, replication,etc.
  • Cloud-controller-manager — Responsible for setup our infrastructure on respective cloud platforms.

Worker plane components

  • kubelet — Acts as a Bridge between Control plane and worker plane
  • kube-proxy — Forwarding requests from outside of the network to Pod
  • Container runtime — Responsible for running Containers

Let's know about some of the important terminologies in Kubernetes.

Pods: It is the smallest execution unit in Kubernetes and it has one or more containers inside in it.

Deployments: It gives the added advantage such as Disaster recovery to the Kubernetes.

ReplicaSets: It gives the scaling feature. It fulfills its purpose by creating and deleting Pods as needed to reach the desired number. When a ReplicaSet needs to create new Pods, it uses its Pod template.

Services: It will act as an accessing point and also acts as a Load balancer for worker nodes.

That's all the basic concepts. Now let's see the Kubernetes drawbacks and how Cloud providers come into the picture in this field.

Due to the popularity of the Kubernetes, the cloud platforms came to host the Kubernetes. Because, one challenge/drawback is there in Kubernetes ie. Setting Kubernetes cluster.

Yes, due to the more number of components in the control plane, users found it hard to set up the cluster since it is complex. So, the Cloud provider came with the concept of hosting a control plane for users.

One such cloud is Amazon and its service is Elastic Kubernetes Service (EKS).

Elastic Kubernetes Service (EKS)

In this, We can just take care of the worker node and need not focus on Control plane components.

Note: EKS doesn't come under the free tier ($0.10 per hour)

Come Let's jump on this practical in setting EKS cluster on AWS.

Basic requirements:

  • aws-cli
  • kubectl
  • eksctl should be installed in your PC

Kubectl is the tool that acts as a client, used to talk with kube-api-server.

Here, mostly Kubectl uses YAML language to talk with kube-api-server.

apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig
metadata:
name: my-eks-cluster
region: ap-southeast-1
nodeGroups:
- name: mynode1
instanceType: t2.small
desiredCapacity: 2
ssh:
publicKeyName: eks-key

In above figure, YAML is used to create an EKS cluster in AWS. This code will creates two worker nodes in ap-southeast-1 region. The instance type should be minimum t2.small.

In my account, the KeyPair named eks-key already there. For you, you have to create the keypair and replace the name of publicKeyName.

After that, run the below code.

eksctl create cluster -f cluster.yml

The above picture shows the output of the execution of cluster.yml.

The two worker node groups were created. You can create any number of Pods in these node groups.

You can create a pod using the below code.

apiVersion: v1
kind: Pod
metadata:
name: httpd1
labels:
role: myrole
spec:
containers:
- name: web
image: httpd
ports:
- name: web
containerPort: 80
protocol: TCP

And run the below command to create a pod.

kubectl apply -f pod.yml

Here comes one interesting concept in AWS for optimizing the cost of instances namely Spot instances.

Spot instances

Spot Instances are the same as normal instances which are available at up to a 90% discount compared to On-Demand prices.

You can use Spot Instances for various stateless, fault-tolerant, or flexible applications such as big data, containerized workloads, CI/CD, web servers, high-performance computing (HPC), and test & development workloads.

Let's see how we can utilize this in our EKS cluster.

apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig
metadata:
name: my-eks-cluster1
region: ap-southeast-1
nodeGroups:
- name: spot
minSize: 2
maxSize: 5
instancesDistribution:
maxPrice: 0.017
instanceTypes: ["t2.small","t3.medium"]
onDemandBaseCapacity: 0
onDemandPercentageAboveBaseCapacity: 50
spotInstancePools: 2
ssh:
publicKeyName: eks-key

In the above code, we are creating two spot instances in which instance will be either t2.small or t2.medium or both depends on the availability of spot instance resources and when max. price meets with spot instance price, then spot instance will be launched.

Here,mixedInstancepolicy is used in order to prevent insufficient spot instance capacity in that region.

This Nodegroup uses 50% spot instances and 50% on-demand instances and no. of spotInstancePools is equal to no. of instance types we specified.

The above picture shows the output of the spot instance. Since t3.medium price exceeds our maximum price, two instances with instance type t2.small are launched.

Our final output will be like…

There is no incremental charge to use this feature and customers pay only for using the AWS resources, such as EC2 Spot Instances and EBS volumes.


Thank you all for your reads. Feedbacks are always welcome. Stay tuned for my next article.
Sri Vishnuvardhan

Written by

Sri Vishnuvardhan

Building production-grade geospatial platforms that combine GIS, remote sensing, cloud infrastructure, AI, and platform engineering to transform spatial data into scalable, real-world intelligence.

Enjoyed this article?

Subscribe to get notified when new articles on DevOps, Cloud, and platform engineering are published.

You'll receive a welcome email with a confirmation link. No spam — unsubscribe anytime.

Have a project in mind?

I work on cloud-native platforms, DevOps automation, and geospatial intelligence systems. Let's talk.

Get in touch
Setting up an EKS cluster on AWS with Spot nodes — Sri Vishnuvardhan A