<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jaison Netto on Medium]]></title>
        <description><![CDATA[Stories by Jaison Netto on Medium]]></description>
        <link>https://medium.com/@jaisonnetto?source=rss-3232cf3c92f4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6m8pjuSC154jwH-YHcviIA.jpeg</url>
            <title>Stories by Jaison Netto on Medium</title>
            <link>https://medium.com/@jaisonnetto?source=rss-3232cf3c92f4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 17:37:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jaisonnetto/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Reduce Kubernetes Infrastructure cost with EC2 Spot Instances — Part 2]]></title>
            <link>https://medium.com/upday-devs/reduce-kubernetes-infrastructure-cost-with-ec2-spot-instances-part-2-6e311ef56b84?source=rss-3232cf3c92f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/6e311ef56b84</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[spot-instances]]></category>
            <dc:creator><![CDATA[Jaison Netto]]></dc:creator>
            <pubDate>Fri, 30 Apr 2021 13:07:12 GMT</pubDate>
            <atom:updated>2021-04-30T13:36:14.899Z</atom:updated>
            <content:encoded><![CDATA[<h3>Reduce Kubernetes Infrastructure cost with EC2 Spot Instances — Part 2</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*A84Ek-_iJSfnhlfXDKzIsA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@f7photo?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Michael Longmire</a></figcaption></figure><p>This post is part two of the series about using <a href="https://aws.amazon.com/ec2/spot"><strong><em>Amazon EC2 Spot</em> </strong></a>instances as <a href="https://kubernetes.io/"><strong><em>Kubernetes</em></strong></a><strong><em> </em></strong>worker nodes. You can read Part 1 on our journey to Spot instance <a href="https://medium.com/upday-devs/reduce-kubernetes-infrastructure-cost-with-spot-instance-part-1-153ea317aed8">here</a>.</p><p><em>This part assumes technical expertise along with some working knowledge on AWS and Kubernetes.</em></p><p><em>The following topics are discussed under this post:</em></p><ol><li><em>EC2 Instance types</em></li><li><em>Launch Template</em></li><li><em>Terraform</em></li><li><em>Node Termination Handler</em></li><li><em>Notification/Alerts</em></li></ol><h3>Choosing EC2 Instance Types</h3><p>Choosing the right instance types is very important when we start using Spot instances. The following points will help you choose the right set of instance types for your use case.</p><ol><li>Understand the CPU and Memory requirements for a <em>Worker Group</em> and get all possible instance types that can give similar resources</li><li>Add at least 5 to 6 instance types if you are deciding to use 100% Spot for that <em>Worker Group</em></li><li>Look for the pricing history of the last 3 months and see if it make sense to use those.</li><li>Look for the interruption history of all the instances.</li><li>Group instances based on type and generation. For example, you can use <strong><em>m5.large</em></strong>, <strong><em>m5a.large</em></strong>, <strong><em>m5ad.large</em></strong>, <strong><em>m5d.large</em></strong>, <strong><em>c5.large</em></strong>, <strong><em>c5a.large</em></strong>, <strong><em>m4.large</em></strong>, <strong><em>c4.large</em></strong>, etc. If you prefer to use only CPU-optimized instances for a workflow, consider all the possible C instance types.</li><li>The cheapest on-demand instance might not be the cheapest Spot instance.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*55DQDyVnIAAdQxFS5sEt8A.png" /></figure><h3>Launch Template</h3><p>The purchase options and instance types section in the <strong><em>Launch Template</em></strong> provides us with the options to use Spot instance. There are certain terminologies used under <strong><em>Launch Template</em></strong> to set up your <em>Worker Group</em> with Spot instance. Let’s have a look at those.</p><p><strong>On-demand base capacity</strong></p><p>This option defines whether you want to use on-demand along with Spot instances or not. It accepts any number including zero. For example, zero means you don’t need any on-demand instances, but if you set it to 2, the first two nodes in the ASG will be on-demand, and the rest can be on-demand or spot based on other parameters.</p><p><strong>On-demand percentage above base</strong></p><p>This is the option that defines whether to use Spot or on-demand when the ASG is scaling up from the base capacity. This value is in percentage. For example, If you set it to 25%, it means that out of 4 nodes that are created as part of scaling, 1 will be on-demand and the others will be Spot.</p><p><strong>Spot allocation strategy</strong></p><p>This option defines the strategy to use for allocating Spot instances. <strong>AWS</strong> supports two strategies, <strong>Lowest Price</strong> and <strong>Capacity Optimised</strong></p><ul><li><strong>LowestPrice</strong></li></ul><p>This strategy creates multiple pools of instances based on the instance types we provide and Spot Instances are provisioned from the Spot capacity pool with the lowest price.</p><ul><li><strong>CapacityOptimised</strong></li></ul><p>Instance types are chosen based on the real-time capacity data and predictions on the available capacity so that the interruptions are minimal.</p><p><strong>Capacity Rebalancing</strong></p><p>You can configure Spot Fleet to launch a replacement Spot Instance when Amazon EC2 emits a rebalance recommendation to notify you that a Spot Instance is at an elevated risk of interruption. Capacity Rebalancing helps you maintain workload availability by proactively augmenting your fleet with a new Spot Instance before a running instance is interrupted by Amazon EC2.</p><h3>Terraform</h3><p>Let’s look at the <a href="https://www.terraform.io/"><strong><em>Terraform</em></strong></a> snippet for creating the <em>Worker Groups</em> with the <strong><em>Launch Template</em></strong>. You will be using all the parameters we discussed under the <strong><em>Launch Template</em></strong> here. You can look at the below snippet for two <em>Worker Group</em> definitions.</p><pre>Worker Group1:<br>------</pre><pre>{<br>   name                      = &quot;test_fullspot&quot;<br>   override_instance_types   = [&quot;t3a.xlarge&quot;, &quot;t3.xlarge&quot;, &quot;t2.xlarge&quot;, &quot;m5a.xlarge&quot;, &quot;m5.xlarge&quot;, &quot;m4.xlarge&quot;, &quot;m5n.xlarge&quot;]<br>   spot_instance_pools       = 6<br>   spot_allocation_strategy  = &quot;lowest-price&quot;<br>   kubelet_extra_args        = &quot;--node-labels=cluster=mytestcluster,purpose=mypurpose --node-labels=node.kubernetes.io/lifecycle=`curl -s <a href="http://169.254.169.254/latest/meta-data/instance-life-cycle%60">http://169.254.169.254/latest/meta-data/instance-life-cycle`</a>&quot;<br>   asg_desired_capacity      = 2<br>   asg_min_size              = 2<br>   asg_max_size              = 25<br>   root_volume_size          = 50<br>   root_volume_type          = &quot;gp3&quot;<br>}</pre><pre>Worker Group2:<br>------</pre><pre>{<br>   name                      = &quot;test_spot_and_on-demand&quot;<br>   override_instance_types   = [&quot;t3a.xlarge&quot;, &quot;t3.xlarge&quot;, &quot;t2.xlarge&quot;, &quot;m5a.xlarge&quot;, &quot;m5.xlarge&quot;, &quot;m4.xlarge&quot;, &quot;m5n.xlarge&quot;]<br>   spot_instance_pools       = 0<br>   on_demand_base_capacity   = 2<br>   on_demand_percentage_above_base_capacity = 25<br>   spot_allocation_strategy  = &quot;capacity-optimized&quot;<br>   kubelet_extra_args        = &quot;--node-labels=cluster=mytestcluster,purpose=mypurpose --node-labels=node.kubernetes.io/lifecycle=`curl -s <a href="http://169.254.169.254/latest/meta-data/instance-life-cycle%60">http://169.254.169.254/latest/meta-data/instance-life-cycle`</a>&quot;<br>   asg_desired_capacity      = 2<br>   asg_min_size              = 2<br>   asg_max_size              = 10<br>   root_volume_size          = 50<br>   root_volume_type          = &quot;gp3&quot;<br>}</pre><p>If you look at the first <em>Worker Group</em> (<strong><em>test_fullspot</em></strong>), you can observe the following:</p><ol><li><strong><em>on_demand_base_capacity</em></strong><em> is not mentioned. This means on-demand instances won’t be used for handling base capacity.</em></li><li><strong><em>on_demand_percentage_above_base_capacity</em></strong><em> is not mentioned, which means on-demand instances won’t be used while scaling up.</em></li><li><strong><em>Spot_instance_pools</em></strong><em> is given as </em><strong><em>6</em></strong><em>, this means the </em><strong><em>Launch Template</em></strong><em> can create 6 possible sets with all the instance types we have given. For example, if we require to run 3 nodes, one set can be three </em><strong><em>t3a.xlarge</em></strong><em> and another set can be two </em><strong><em>t3a.xlarge</em></strong><em> and one </em><strong><em>t3.xlarge,</em></strong><em> and so on.</em></li><li><strong><em>Spot_allocation_strategy</em></strong><em> is given as </em><strong><em>lowest-price.</em></strong><em> This means the </em><strong><em>Launch Template</em></strong><em> will consider the instance pool with the least cost regardless of availability/interruption history or any other factors.</em></li></ol><p>Let’s look at the 2nd <em>Worker Group</em> (<strong><em>test_spot_and_on-demand</em></strong>) now.</p><ol><li><strong><em>on_demand_base_capacity</em></strong><em> is set to </em><strong><em>2</em></strong><em>. This config ensures that the first two nodes will be on-demand instances.</em></li><li><strong><em>on_demand_percentage_above_base_capacity</em></strong><em> is set to </em><strong><em>25</em></strong><em>, which translates to 1 on-demand instance per 3 Spot instances when scaling up.</em></li><li><strong><em>Spot_allocation_strategy</em></strong><em> is set to </em><strong><em>capacity-optimized</em></strong><em>. </em><strong><em>Launch Template</em></strong><em> will not try to blindly save cost here, rather it will use a mix of instances to ensure that the capacity is not interrupted by simultaneous spot terminations.</em></li><li><strong><em>Spot_instace_pools</em></strong><em> is set to zero as the </em><strong><em>capacity-optimized</em></strong><em> strategy does not use </em><strong><em>pools</em></strong><em>.</em></li></ol><p>Both the <em>Worker Groups</em> are good in their own way if we use it in the appropriate workflows. We can use the first <em>Worker Group</em> (100% Spot) in any non-critical use case, whereas the second can be used for any general workflow and still save a lot.</p><h3>AWS Node Termination Handler</h3><p>Let’s look at the <a href="https://github.com/aws/aws-node-termination-handler"><strong><em>AWS Node Termination handler</em></strong></a> setup. Node Termination Handler should be run as a Kubernetes daemon set. It can be installed using helm in one of the two ways:</p><ol><li>Add the helm repo eps-charts and install <strong><em>Node Termination Handler</em></strong> using helm install command with all necessary parameters.</li><li>Download/clone the chart and then make changes to the config by updating the <strong><em>values.yml</em></strong> and then install the chart.</li></ol><p>Some parameters that we need to consider in the <strong><em>Node Termination Handler</em></strong> configuration;</p><ul><li><strong>Node selector</strong></li></ul><p>Node selector gives you control to run the <strong><em>Node Termination Handler</em></strong> either on all the nodes in your cluster or only on the spot instance. The ideal situation is to run it only on Spot instances as the other nodes might not get interrupted/stopped (unless there are any maintenance or issues in the hardware node). You can use the below snippet to run the Spot termination handler only on the Spot instance. If you look at our terraform code, you can see that we are setting this label in all the Spot instances using the EC2-metadata.</p><pre>nodeSelector: {<br>   node.kubernetes.io/lifecycle: spot<br>}</pre><ul><li><strong>Webhook URL</strong></li></ul><p>It is an optional parameter that we can use to notify about the events that the Spot termination handler is getting (Spot interruptions). You can provide any webhook URL here and the <strong><em>Node Termination Handler</em></strong> will send the events to it. We are using a slack webhook here so that we get notified whenever an interruption is about to happen.</p><pre>webhookURL: “https://hooks.slack.com/services/xxxx/ssssess&quot;</pre><ul><li><strong>Webhook Template</strong></li></ul><p>You can customize the notification message that’s sent to the webhook URL using the webhook template.</p><pre>webhookTemplate: “{\”text\”:\”:rotating_light:*INSTANCE INTERRUPTION NOTICE*:rotating_light:\n*_EventID:_* `{{ .EventID }}`\n*_Environment:_* `&lt;env_name&gt;`\n*_InstanceId:_* `{{ .InstanceID }}`\n*_InstanceType:_* `{{ .InstanceType }}`\n*_Start Time:_* `{{ .StartTime }}`\n*_Description:_* {{ .Description }}\”}”</pre><p><em>Once the configuration is made, we can install the helm chart using the helm install command.</em></p><p>Let’s see how the <strong><em>Node Termination Handler</em></strong> works when one of the Spot instances gets a termination notice.</p><ol><li>AWS pushes this termination notice through instance metadata.</li><li><strong><em>Node Termination Handler</em></strong> DaemonSet running in the node will get this metadata information from the <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-categories.html">EC2 instance metadata</a></li><li>Once it gets the metadata, it uses Kubernetes API to cordon the node to ensure no new work is scheduled there.</li><li>It drains the node using Kubernetes API.</li><li>Sends a notification to the webhook URL about the Spot interruption.</li><li>The Spot instance which got the termination notice will get terminated when the notice window expires.</li><li>cluster-auto-scaler finds the need for a new instance and adds another node (Spot) to the ASG.</li></ol><p><em>Let’s look at a real-time example in which a Spot instance in the Staging environment got an interruption notice. Below are the logs and the notification details.</em></p><pre>2020/10/15 13:16:46 Got interruption event from channel {InstanceID:i-xx13xx015f7xx<br>InstanceType:m4.xlarge PublicHostname: PublicIP: LocalHostname:ip-10-xx-96-yy9.yy-west-1.compute.internal<br>LocalIP:10.41.96.249 AvailabilityZone:xx-west-1c}<br>{EventID:spot-itn-xxx62f74d4dda8a12XXXXXXXX3dfb1e5b1caafedf059fc5a1e<br>Kind:SPOT_ITN<br>Description:Spot ITN received. Instance will be interrupted at 2020-10-15T13:18:46.155Z<br>State: NodeName:ip-10-xx-yy-xx9.xx-west-1.compute.internal<br>StartTime:2021-02-15 13:18:46.155 +0000 UTC EndTime:0001-01-01 00:00:00 +0000 UTC Drained:false PreDrainTask:0x113c8a0 PostDrainTask:&lt;nil&gt;}<br>2020/10/15 13:16:46 Node &quot;ip-10-xx-yy-x9.xx-west-1.compute.internal&quot; successfully cordoned and drained.<br>2020/10/15 13:16:46 Webhook Success: Notification Sent!</pre><p><em>You can see steps 1 to 5 in the logs.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EhklujphhEaaC-y7VS_cZw.png" /><figcaption>Notification for Spot Interruption</figcaption></figure><h3>Notification about Spot Unavailability</h3><p>After setting up all the above things, is this really required? Yes, though we have given a wide range of instance types for all our <em>Worker Groups</em>, there might be a situation where none of the instance types are available in Spot capacity when the ASG tries to provision a new one(Scale-up).</p><p>The <strong><em>Launch Template</em></strong> tries to create an instance with any of the instance types that we have mentioned and if it can’t get any in a specified time, it will retry again. This process can get repeated if the Spot capacity is not yet available. Your application/environment might become degraded as it&#39;s not getting resources to scale up.</p><p>To handle such a rare situation, we can set up a small workflow.</p><ol><li><em>Create an event Rule under </em><a href="https://aws.amazon.com/eventbridge/"><strong><em>AWS EventBridge</em></strong></a></li><li><em>Write a pattern to filter EC2 Instance Launch Unsuccessful events</em></li><li><em>Create a </em><a href="https://aws.amazon.com/lambda/"><strong><em>Lambda</em></strong></a><em> to process/filter events, perform required actions/notify the stakeholders.</em></li><li><em>Set the created </em><strong><em>Lambda</em></strong><em> as a target in the Event Rule</em></li></ol><p>A sample notification sent using the above workflow is shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I4i9FmCcNXgJHKHrpwaLNQ.png" /><figcaption>Notification about instance Launch Failure</figcaption></figure><h3>What’s Next</h3><p>One addition we want to make to this implementation is to accumulate all the spot provision and interruption events, we can then analyze this data and plot it so that we can have a better understanding of what’s happening by looking at a dashboard.</p><h3>Conclusion</h3><p>Our non-production and production environments are adapted to this new implementation for over 4 months now and so far it is running fine with significant savings in cost without compromising the productivity or performance. Our non-production environments run on 100% Spot capacity and our production environment uses both Spot and On-demand capacity.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e311ef56b84" width="1" height="1" alt=""><hr><p><a href="https://medium.com/upday-devs/reduce-kubernetes-infrastructure-cost-with-ec2-spot-instances-part-2-6e311ef56b84">Reduce Kubernetes Infrastructure cost with EC2 Spot Instances — Part 2</a> was originally published in <a href="https://medium.com/upday-devs">upday devs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Reduce Kubernetes Infrastructure cost with Spot instance — Part 1]]></title>
            <link>https://medium.com/upday-devs/reduce-kubernetes-infrastructure-cost-with-spot-instance-part-1-153ea317aed8?source=rss-3232cf3c92f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/153ea317aed8</guid>
            <category><![CDATA[upday]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[aws-spot]]></category>
            <category><![CDATA[cloud-cost-optimization]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Jaison Netto]]></dc:creator>
            <pubDate>Fri, 22 Jan 2021 14:04:43 GMT</pubDate>
            <atom:updated>2022-01-10T09:42:25.056Z</atom:updated>
            <content:encoded><![CDATA[<h3>Reduce Kubernetes Infrastructure cost with EC2 Spot Instances — Part 1</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sl3zPl0rJCD-9BGOWqLRdw.jpeg" /><figcaption><strong>$$$</strong> // photo from my camera roll</figcaption></figure><p>This post is part one of two series about using <a href="https://aws.amazon.com/ec2/spot"><strong><em>Amazon EC2 Spot</em> </strong></a>instances as <a href="https://kubernetes.io/"><strong><em>Kubernetes</em></strong></a><strong><em> </em></strong>worker nodes.</p><ul><li><a href="https://medium.com/upday-devs/reduce-kubernetes-infrastructure-cost-with-spot-instance-part-1-153ea317aed8"><strong>Part 1</strong></a> is more focused on our journey of moving to Spot instance worker nodes.</li><li><a href="https://medium.com/upday-devs/reduce-kubernetes-infrastructure-cost-with-ec2-spot-instances-part-2-6e311ef56b84"><strong>Part 2</strong></a> will dive into technical aspects and best practices of using Spot instances in Kubernetes.</li></ul><p>To begin, let us understand what a Spot instance is.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MBy-uVkEz_w42S_YFzihxA.jpeg" /></figure><p>Whenever you create an <a href="https://aws.amazon.com/ec2"><strong><em>EC2</em></strong></a> instance in <a href="https://aws.amazon.com/"><strong><em>AWS</em></strong></a>, you are most likely creating an on-demand EC2 instance. This means you requested for an EC2 instance, AWS provided that and you are charged for it. AWS holds a lot of physical machines for handling requests like that. AWS introduced the concept of Spot instance so that they can make money out of this capacity, which is not being used, but at the same time is also available whenever there is demand. The result is, you can get the same computing capacity with less than half of its price, but the instance might go unavailable (<a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-interruptions.html">Spot interruption</a>) with a small window of notification time (2 minutes). So, if your workflow is either not affected by this kind of interruption or if you have a way to handle the interruptions, a Spot instance could be a good option for you.</p><h3><strong>Initial Analysis:</strong></h3><p>We broke down our initial analysis into the following steps:</p><ol><li><strong><em>Investigating the Spot instance pricing, interruption history, optimal instance sizes</em></strong></li><li><strong><em>Implementing Spot instance as EKS worker nodes</em></strong></li><li><strong><em>Handling Spot interruptions</em></strong></li></ol><h4><strong>Initial analysis on instance types:</strong></h4><p>Investigating the Spot instance pricing, interruption history, and optimal instance types are a manual process where we must look at pricing history and interruption histories in general. We also have to look at what kind of instance types we are currently using and what are all the possible Spot instance options to replace those.</p><blockquote>For example: If you are using an m5.xlarge instance for a worker group, you can consider having all the below options.</blockquote><blockquote>m5.xlarge, m5a.xlarge, m4.xlarge, m5n.xlarge, m5d.xlarge, m5nd.xlarge</blockquote><blockquote>You can also consider mixing it with other instance types like the c5/c4 or r5/r4 as well if they fit in fine for your use case and capacity.</blockquote><p>The point of giving multiple instance types is to ensure that we have a big pool of options and thereby the chances of getting a proper Spot instance whenever required. Let’s imagine you limited yourself to only one possible instance type for the Spot request, it means if that exact instance type is not available at that time, you will not be getting a new instance provisioned. To avoid such a situation, it is better to have a variety of instance types for the Spot request. You must choose this based on the kind of capacity you need, Spot price history, etc.</p><h4>Initial analysis on<strong> Implementation:</strong></h4><p>There are multiple ways to implement this.</p><ol><li><strong>Launch Configuration</strong></li><li><strong>Launch Template</strong></li></ol><p><a href="https://docs.aws.amazon.com/autoscaling/ec2/userguide/LaunchConfiguration.html"><strong><em>Launch Configuration</em></strong></a> is a template with necessary configurations like <em>instance type</em>, <em>security group, etc</em>. <a href="https://docs.aws.amazon.com/autoscaling/ec2/userguide/AutoScalingGroup.html"><strong><em>Auto Scaling Group</em></strong></a> will be using <strong><em>Launch Configuration</em></strong> to launch a new instance based on the scaling requirement it gets.</p><p>Within <strong><em>Launch Configuration</em></strong>, we have an option of purchasing Spot instances over on-demand instances. You can define whether you want to use on-demand or Spot instance but not a combination of both. Since you cannot use Spot and on-demand within the same worker group, you must look at your current worker configurations and decide how much can be on Spot instances, and split the worker group into two with separate <strong><em>Launch Configuration</em></strong>. Also, you must make associated changes in the K8s manifest to point which pod/deployment goes to Spot/on-demand.</p><p><a href="https://docs.aws.amazon.com/autoscaling/ec2/userguide/LaunchTemplates.html"><strong><em>Launch Template</em></strong></a> which is a relatively newer implementation has a better way of solving this problem where we can allocate Spot and on-demand under the same worker group. This method is the one to go for if you really need the flexibility of mixing Spot and on-demand within the same worker group itself.</p><h4>Handling Spot<strong> Interruptions:</strong></h4><p>Demand for Spot instances can vary significantly from moment to moment, and the availability of Spot instances can also vary significantly depending on how many unused EC2 instances are available. It is always possible that your Spot instance might be interrupted and how we handle these interruptions, without interrupting our service delivery, is a particularly important part. AWS Node Termination Handler <a href="https://github.com/aws/aws-node-termination-handler"><strong><em>aws-node-termination-handler</em></strong></a> is the key to solving this problem. This handler will run a small pod on each host to perform monitoring and react accordingly. It listens to the EC2 instance metadata and when it detects a Spot interruption notification in the instance metadata, it uses the Kubernetes API to cordon the node to ensure no new work is scheduled there, then drains it, removing any existing work.</p><h3><strong>Implementation:</strong></h3><p>We used the data from our initial analysis to conclude the set of instance types that need to be used and the kind of ratio we need for on-demand to Spot. Also, it was certain that we wanted more of a flexible implementation and hence decided to go with the <strong><em>Launch Template</em> </strong>option.</p><p>We are using <a href="https://github.com/terraform-aws-modules/terraform-aws-eks"><strong><em>Terraform EKS</em></strong></a> module to create our <a href="https://aws.amazon.com/eks"><strong><em>EKS</em></strong></a> infrastructure and were using <strong><em>Launch Configuration</em></strong>. Since the module supports <strong><em>Launch Template</em></strong> as well, we had to make some associated changes in <a href="https://www.terraform.io/"><strong><em>Terraform</em></strong></a> to make it use the <strong><em>Launch Template</em></strong> instead of the <strong><em>Launch Configuration</em></strong>.</p><p><strong><em>Node termination handler</em></strong> can be deployed to the cluster using <em>helm</em> or directly using manifests. Using <a href="https://helm.sh/"><strong>helm</strong></a><strong> </strong>is an easier approach with much more control on the configuration part as well. We configured the values in such a way that we run the node termination handler only on Spot instances and receive a notification to our slack channel whenever a node gets terminated.</p><p>We were very certain about the availability of our environment and hence wanted to avoid any issues that can arise as part of Spot implementation. One issue we could think of was the unavailability of any Spot instance (though we have given enough choices on instance types) in an <a href="https://docs.aws.amazon.com/autoscaling/ec2/userguide/AutoScalingGroup.html"><strong><em>Auto Scaling Group</em></strong></a>. A situation like this can put the application performance at risk and will affect the scaling capabilities (at least for some time). To identify such a situation as early as possible and handle it better, we created an <a href="https://aws.amazon.com/eventbridge/"><strong><em>EventBridge</em></strong></a> in AWS to identify these kinds of events and trigger a <a href="https://aws.amazon.com/lambda/"><strong><em>Lambda</em></strong></a> function that will notify us through slack and email.</p><h3>How much can we save?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/1*g70OszL4GRFUn4l62QEcTg.jpeg" /></figure><p>Here is the interesting part and let us go through some numbers. AWS says you might be saving close to <strong>60–70%</strong> when you use an equivalent Spot instance instead of the one you are using right now. Considering the variation in Spot pricing and availability, you should be able to save at least <strong>40 to 50%,</strong> if you are moving all your workflow from on-demand to Spot. Based on your infrastructure requirements, you might be either running the entire workflow in Spot instance or only part of it. Based on all these considerations, your savings might be different. We followed a <strong>100%</strong> Spot on some of our non-production environments where we only considered cost-saving. However, there are other production environments where we run on-demand and Spot hand in hand to get the best out of cost savings and capacity.</p><p><em>To conclude, with this implementation, all our Kubernetes worker groups could utilize a mix of on-demand and Spot instances and we made a 30% savings on our Kubernetes infrastructure cost.</em></p><p>Continued in <a href="https://medium.com/upday-devs/reduce-kubernetes-infrastructure-cost-with-ec2-spot-instances-part-2-6e311ef56b84"><strong><em>Part 2</em></strong></a>, is <em>all the </em><strong><em>technical details and best practices.</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=153ea317aed8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/upday-devs/reduce-kubernetes-infrastructure-cost-with-spot-instance-part-1-153ea317aed8">Reduce Kubernetes Infrastructure cost with Spot instance — Part 1</a> was originally published in <a href="https://medium.com/upday-devs">upday devs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>