AWS EKS Workshop: Mastering Managed Node Groups with Spot Instances
Written on
Introduction
Spot Instances utilize spare EC2 capacity available at a lower rate than On-Demand pricing. Leveraging these instances can significantly reduce overall Amazon EC2 operational expenses for workloads that can tolerate interruptions.
See the savings illustrated below:
In this chapter, we will engage in a hands-on approach to provisioning, managing, and maintaining EC2 Spot Capacity using Amazon EKS Managed Node Groups.
Best Practices for EC2 Spot Instances
- Suitable Workloads: Spot Instances are ideal for stateless, fault-tolerant applications, working effectively in scenarios such as big data, containerized workloads, CI/CD, stateless web services, high-performance computing (HPC), and rendering tasks.
- Unsuitable Workloads: They are not recommended for workloads that are inflexible, stateful, or fault-intolerant, particularly those that cannot handle occasional unavailability of the desired capacity.
To successfully adopt Spot Instances, consider implementing Spot Instance Diversification. This strategy involves sourcing capacity from various Spot Instance pools for scaling and replacing instances when a termination notice is received.
In the context of Amazon EKS, common strategies for diversifying Spot Instance pools include:
- Creating multiple node groups of varying sizes (e.g., one group with 4 vCPUs and 16GB RAM, another with 8 vCPUs and 32GB RAM).
- Implementing instance diversification within node groups by selecting a variety of instance types and families from different Spot Instance pools that meet the same vCPU and memory requirements.
For this demonstration, we will focus on instance types with 2 vCPUs and 4GiB of memory. The command-line tool amazon-ec2-instance-selector will assist in identifying the relevant instance types and families.
This complete end-to-end demonstration will follow these steps:
- Create an Amazon EKS cluster with a standard ON_DEMAND Node Group.
- Establish a second Node Group for Spot Capacity to run our selected workloads.
- Modify our demo retail application's Catalog component to operate on Spot instances in the second node group.
- Simulate Spot Interruption to observe how the EKS Node Group manages interruptions seamlessly, replacing nodes without disrupting the application (see command output post-implementation).
Managed Node Groups (MNG) with Spot Instances — Theory and References
- Managed Node Groups (MNG) with Spot Instances EKS Workshop Page — This is the primary source for this post, providing comprehensive conceptual guidance and hands-on steps.
- Amazon EKS Managed Spot Workers Workshop — Another workshop focused on Spot Computing within the Amazon EKS environment.
- Amazon EC2 Instance Purchasing Options Documentation — Essential reading for understanding On-Demand, Spot, and other computing options available on AWS.
- Amazon EC2 Instance Selector — GitHub repository and guide for this useful command-line tool.
- Spot Instance Interruptions — A practical simulation of Spot Instance termination and its handling within the Amazon EKS architecture.
- Tutorial: Test Spot Instance Interruptions Using AWS FIS — Learn to simulate Spot interruptions with the Fault Injection Service.
- AWS Blog: Amazon EKS and Spot Instances in Action at Delivery Hero — Similar hands-on experience shared by the Delivery Hero team.
- AWS Blog: Building for Cost Optimization and Resilience for EKS with Spot Instances — An introductory blog with additional insights.
Managed Node Groups (MNG) with Spot Instances — Hands-On Implementation
It's time to build!
We begin from scratch with access to an AWS account. The upcoming steps will guide you through the process. First, follow the instructions from previous chapters to create the cluster, then proceed with preparing the environment.
The end-to-end demonstration consists of FIVE STEPS:
- Build Cluster and Prepare Environment
- Instance Type Diversification
- Create Spot Capacity
- Run a Workload on Spot
- Simulate Spot Interruption with AWS FIS
Step 1: Build Cluster and Prepare Environment
Ensure you have created an Amazon EKS Cluster as instructed in EKS Workshop Chapter 2.
Prepare the environment for MNG - Spot Instances:
prepare-environment fundamentals/mng/spot
Step 2: Instance Type Diversification
Provision the second node group with instance types having 2 vCPUs and 4GiB of memory. Use the amazon-ec2-instance-selector tool to identify the relevant instance types.
Verify that the CLI tool is operational:
ec2-instance-selector --version
Use the tool to list instances:
ec2-instance-selector --vcpus 2 --memory 4 --gpus 0 --current-generation -a x86_64 --deny-list 't.*' --output table-wide
Step 3: Create Spot Capacity
Deploy a managed node group dedicated to Spot instances. Name this group 'managed-spot'.
aws eks create-nodegroup
--cluster-name $EKS_CLUSTER_NAME
—nodegroup-name managed-spot
--node-role $SPOT_NODE_ROLE
—subnets $PRIMARY_SUBNET_1 $PRIMARY_SUBNET_2 $PRIMARY_SUBNET_3
--instance-types m5.large t3.medium t3.large
—capacity-type SPOT
--scaling-config minSize=2,maxSize=3,desiredSize=2
—disk-size 20
Step 4: Run a Workload on Spot
Adjust the retail store application to run the catalog component on the new Spot instances by adding a nodeSelector field.
Review and apply the Kustomize patch:
kubectl apply -k ~/environment/eks-workshop/modules/fundamentals/mng/spot/deployment
Step 5: Simulate Spot Interruption with AWS FIS
To create a simulation of a Spot interruption, download the FIS template:
Deploy the FIS template with CloudFormation:
aws cloudformation deploy
--stack-name fis-spot
—template-file spotinterruption.yaml
--capabilities "CAPABILITY_NAMED_IAM"
Spot Node Interruption Handling by Spot Node Group — Screenshot
Conclusion and Next Steps
This chapter offered a comprehensive, hands-on demonstration of using the amazon-ec2-instance-selector tool, provisioning and managing EC2 Spot Capacity within an Amazon EKS cluster, and effectively handling Spot interruptions while adhering to Kubernetes best practices.
It is advisable to explore the integration of scalable, resilient system designs that leverage the cost-saving potential of Spot instances, rather than solely depending on pricier On-Demand instances. This chapter serves as a solid foundation for those looking to adopt such strategies in both non-containerized and container-based environments like Amazon EKS.
With Chapter 34— Managed Node Groups (MNG) for EC2 Spot Capacity/Instances, we conclude the Managed Node Groups section of the Amazon EKS Fundamentals module. Our next focus will be on implementing Amazon EKS Fargate.
See you there soon!
> If you found this article helpful, please share it to assist others; your support is greatly appreciated!