November 20, 2023

Azure Kubernetes Service Cost Optimization

Azure Kubernetes Service Cost Optimization blog post banner with an illustration of Sefa Aydınelli.

Kubernetes is an open-source orchestration platform that automates the deployment, scaling, and management of container-based applications. This platform enhances the stability of your applications while accelerating development and operational processes. Azure Kubernetes Service (AKS) is a service on Microsoft's Azure cloud platform that allows users to deploy and manage Kubernetes easily.

Using AKS can simplify the Kubernetes experience, but it's essential to be proactive in managing costs. There are various strategies and third-party tools that work with Kubernetes and can be employed in AKS to optimize expenses.

In this blog post, we will discuss how we optimized the Stage and Production environments running on Azure Kubernetes Service, detailing the various actions taken to enhance optimization.

Azure Kubernetes Service Pricing

Navigating the pricing structure of Azure Kubernetes Service (AKS) is crucial for optimizing your cloud expenses. Several factors influence the total cost of using AKS:

  • Node Pricing: In AKS, the main cost comes from the compute resources, especially the virtual machines hosting your containers. The cost varies based on VM size, so it's vital to choose what fits your workload. 
  • Associated Services: Although there's no direct charge for AKS, integrating services like Azure Monitor, Azure Networking, and persistent storage can affect the overall cost.
  • Region Variance: The Azure region where your cluster resides can also influence pricing due to regional operational cost differences.
  • Additional Features: Premium features, such as Azure Active Directory integration or advanced networking, may incur extra charges.

Cost Optimization Strategies

After understanding the fundamental pricing structure of AKS, the next step is to explore strategies for cost optimization. Efficiently managing your resources not only saves money but also ensures that your applications run smoothly. Here are some key strategies to consider:

Right-sizing Pods and Nodes

A direct way to save costs is by using just the right amount of resources. Regularly reviewing the size and resource allocation of your pods and nodes to ensure they match your application's needs is crucial.

To do this effectively:

  • Collect data on how many resources each service running on Kubernetes is using. Prometheus can be used for this purpose.
  • Using Grafana, visualize and analyze these metrics to determine average resource utilization. This insight can guide the resizing of pods and nodes based on actual usage patterns.
Resource usage metrics within an example namespace.
Resource usage metrics within an example namespace.
  • After determining the average resource, update the deployment resource definitions of the services.
  • Update the node-pool type according to the current resource definitions of the new pods.
  • Consider leveraging tools like Goldilocks. This tool recommends optimal resource request values for each of your Kubernetes pods, assisting in fine-tuning CPU and memory requests. For a deeper understanding of how Goldilocks can be beneficial, refer to CNCF’s introduction to Goldilocks and its GitHub repository.

By implementing these steps, you can optimize resource allocation in AKS, ensuring efficient performance while managing costs. Along with these measures, for the Baumappe services, we clarified the average resource utilization and made necessary resource allocations in the deployment configurations of all services to only what was needed. For more detailed information on resource management, you can visit Kubernetes' resource management documentation.

Autoscaling with Cluster Autoscaler in AKS

Once you've fine-tuned the size of your pods and nodes, the next logical step is to implement auto scaling. Autoscaling in AKS ensures that your services can efficiently adapt to changes in traffic, scaling horizontally as needed.

Horizontal Pod Autoscaler (HPA)

HPA automatically adjusts the number of pods used in a deployment or replica set based on observed CPU utilization and other select metrics. After you've set up the resource requests appropriately for your pods:

  • Configure HPA for your services. This ensures that as the demand for a service increases, more pods are spawned to handle the load, ensuring consistent performance.
  • Conversely, when traffic decreases, HPA will reduce the number of pods, leading to cost savings.
Node Scaling with Cluster Autoscaler

The Cluster Autoscaler in AKS automatically adjusts the size of the cluster, adding or removing nodes based on resource requirements and constraints. This dynamic scaling ensures:

  • During high traffic, the cluster can expand to accommodate the increased load, maintaining high availability and performance.
  • During periods of reduced traffic, the cluster downsizes, thereby cutting expenses while maintaining operational effectiveness.

By integrating both pod and node autoscaling mechanisms in AKS, you strike a balance between performance and cost, ensuring your applications remain responsive while optimizing cloud expenditure. We have configured auto-scaling to be active for all new node pools.

Spot Instance Usage in AKS Node Pools

Using spot instances in AKS node pools can result in significant cost savings, with potential reductions of up to 90%. Spot instances are Azure's evictable virtual machines that are offered at a discounted rate compared to standard instances. However, it's important to note that these instances can be interrupted and reclaimed by Azure based on their capacity needs.

Spot instances are ideal for development and testing, where interruptions aren't a major concern. Note that these instances operate on an “interruption-possible” basis due to Azure's demands.

For effective management of spot instance interruptions, the community-developed tool, AKS Node Termination Handler, is essential. It monitors termination notices, ensuring Kubernetes nodes are gracefully drained, reducing disruptions. For more details, refer to its GitHub repository.

By leveraging spot instances in AKS node pools and employing tools such as the AKS Node Termination Handler, you can optimize costs while maintaining the performance and reliability of your workloads.

In the Baumappe project, we created a spot node pool for our resources under the test namespace, thereby generating less cost for resources that would not be an issue in the event of interruptions.

Reservations and Savings Plan

Optimizing costs in Azure goes beyond just managing resources. Azure offers financial strategies to help reduce costs for long-term workloads with predictable usage.

Reservations

With Azure Reservations, you purchase specific resources in advance for a one- or three-year term. This commitment allows Azure to offer you a discounted price on those resources compared to pay-as-you-go prices. Specifically for AKS:

  • You have the option to reserve Virtual Machine instances for your node pools, which can result in cost savings.
  • By understanding your long-term needs and committing to them, reservations can significantly lower the total cost of your AKS operations.

Savings Plan

Azure Savings Plans offer a more flexible alternative to reservations. Instead of committing to specific VM sizes or families:

  • You commit to a consistent amount of compute usage (e.g., vCPU/hours) over one or three years.
  • This offers more flexibility in terms of VM sizes or families, allowing you to change your infrastructure without losing the benefit of the savings plan.

We have not yet taken any reservation or savings plan action. However, considering the stability of our resource usage going forward, we will take action accordingly.

Monitor Cost Usage with Kubecost

To complete your cost optimization strategy for AKS, it's paramount to have a clear insight into where your expenses are coming from and how they're trending over time. Kubecost is a tool tailored to Kubernetes that provides this visibility.

A Kubecost interface example.
A Kubecost interface example.

Kubecost offers several features to enhance your cost-tracking efforts:

  • Real-time Monitoring: Track cost and usage in real-time across multiple dimensions.
  • Allocation Breakdown: Understand costs by namespace, label, or deployment. This granular view helps pinpoint inefficiencies and over-provisioned resources.
  • Budget Alerts: Set budgets based on your criteria and receive alerts when you're trending over.

By integrating Kubecost into your AKS infrastructure, you gain the ability to make informed decisions based on actual usage and cost data. This ensures that your cost-saving measures are effective and that you can adapt as your usage patterns change.

To learn more about how Kubecost can enhance your AKS cost management, visit their official documentation and GitHub repository.

Using Kubecost, we have made future optimization actions visible, which will enable us to take steps towards cost optimization.

Cost Saving Achievements

AKS Cost Chart for Baumappe in the last 30 days.
AKS Cost Chart for Baumappe in the last 30 days.

Our strategic approach to managing Azure Kubernetes Service (AKS) resources has resulted in notable cost savings for the Baumappe project. By applying the methodologies outlined in this post, we've reduced the average daily costs from approximately €110 to €75. This represents a significant reduction, highlighting the effectiveness of our optimization efforts. The AKS Cost Chart for Baumappe over the last 30 days illustrates this descending trend in daily expenses, demonstrating our successful application of cost-saving measures such as rightizing resources, implementing autoscaling, and utilizing spot instances for our test environments.

These cost reductions are not our final goal but a milestone in our ongoing journey towards financial efficiency and resource optimization. We anticipate further decreases in expenditure as we continue to refine our strategies, informed by real-time data and enhanced by tools like Kubecost, which has provided us with actionable insights for future optimizations.

Conclusion

Saving money with Azure Kubernetes Service (AKS) is all about smart choices. As data and circumstances evolve, so do our strategies for resource utilization and cost management. Helpful tools like Goldilocks and Kubecost advise where to save more. As the world of cloud computing grows, it's crucial to stay on top of these money-saving tactics. With the proper steps and tools, AKS can be both powerful and cost-effective.

In implementing these strategies for the Baumappe project, we've seen tangible improvements in cost-efficiency. Looking ahead, we remain committed to refining our approach and leveraging emerging tools and insights to ensure AKS remains both powerful and economically viable.

Sefa Aydınelli
Development

You may also like