Featured image

Introduction

Deploying Kubernetes in production on Azure requires thoughtful architecture and operational best practices to balance performance, reliability, and cost. Azure Kubernetes Service (AKS) offers a managed Kubernetes environment with native integration to Azure’s ecosystem, but achieving optimal cost efficiency and scalable performance demands a comprehensive understanding of AKS features and configurations.

This in-depth article explores practical guidance and best practices for designing production-grade AKS clusters with a focus on cost optimization, autoscaling strategies, and selecting appropriate VM sizes for your workloads. We will dive into how to leverage AKS capabilities such as Cluster Autoscaler, Horizontal Pod Autoscaler, and Azure Container Instances (ACI) connector to build a flexible, cost-effective environment.


Understanding Cost Optimization Principles in AKS Production

Managing operational expenses (OPEX) while providing flexible, on-demand capacity is a primary goal in production Kubernetes environments. AKS offers several mechanisms to automatically adjust resources based on workload demand, reducing waste and ensuring performance.

Key principles include:

  • Reducing OPEX: Automate scaling to avoid over-provisioning nodes and pods.
  • Flexible On-Demand Capacity: Scale resources dynamically to handle workload spikes and troughs without manual intervention.

Cluster Autoscaler (CA): Dynamically Adjust Node Count

The Cluster Autoscaler is a critical component for managing the number of nodes in an AKS cluster. It monitors your cluster every 10 seconds to detect pending pods that cannot be scheduled due to insufficient node resources or identifies nodes that are underutilized.

How Cluster Autoscaler Works

  • Scaling Up: When pods are pending due to lack of resources, CA adds new nodes up to a defined maximum.
  • Scaling Down: Nodes unused for more than 10 minutes are removed to save costs.

Best Practices for CA

  • Define sensible minimum and maximum node counts based on your expected workload patterns. For example, maintain a minimal node count to ensure baseline availability during low traffic, and set a maximum to control cost during peak usage.
  • Monitor scaling events and adjust thresholds to balance responsiveness and cost.

Example CA Configuration Snippet (YAML)

apiVersion: autoscaling/v1
kind: ClusterAutoscaler
metadata:
  name: cluster-autoscaler
  namespace: kube-system
spec:
  scaleDown:
    enabled: true
    delayAfterAdd: 10m
    delayAfterDelete: 10s
    delayAfterFailure: 3m
  resourceLimits:
    minNodes: 3
    maxNodes: 10

Horizontal Pod Autoscaler (HPA): Scaling Application Pods

Where Cluster Autoscaler manages infrastructure nodes, the Horizontal Pod Autoscaler scales the number of application pods based on real-time metrics like CPU, memory, or custom metrics.

HPA Benefits

  • Handles short-term load spikes quickly without waiting for new nodes.
  • Ensures your application maintains performance and availability.

How CA and HPA Work Together

  • HPA rapidly scales pods within existing nodes during small spikes.
  • CA scales nodes when pod resource demand exceeds current cluster capacity.

Example HPA Definition

apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: web-app-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: web-app
  minReplicas: 3
  maxReplicas: 15
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 60

Azure Container Instances (ACI) Connector: Instant Bursting Capacity

The ACI connector integrates Azure Container Instances as a virtual node into your AKS cluster, providing nearly unlimited capacity for burst workloads.

Key Advantages

  • Instant Pod Launch: Unlike CA, which requires time to provision VMs, ACI can spin up containers instantly.
  • Seamless Integration: Pods scheduled on the virtual node behave like any other pod in AKS.

When to Consider ACI Connector

  • For unpredictable or very bursty workloads where rapid scaling is needed.
  • As a cost-effective alternative to overprovisioning nodes for rare peak loads.

Considerations

  • ACI is not a replacement for HPA but can substitute or complement CA.
  • Currently supports deployment into existing Virtual Network subnets via the AKS virtual node feature (private preview).

References


Selecting Optimal Node VM Sizes

Choosing the right VM sizes for your AKS nodes has a major impact on cost and performance. Azure offers a variety of VM series tailored to different workloads.

  • Dsv3 and Esv3 series: SSD-backed storage with balanced CPU to memory ratios.
  • Common sizes:
    • Standard_D2s_v3 (2 vCPUs, 8 GB RAM)
    • Standard_D4s_v3 (4 vCPUs, 16 GB RAM)
    • Standard_E2s_v3 (2 vCPUs, 16 GB RAM) for higher memory per CPU
    • Standard_E4s_v3 (4 vCPUs, 32 GB RAM)

For GPU Workloads

  • Use N-series VMs designed specifically for GPU acceleration.

For Development/Test Environments

  • Use Burstable VM sizes like Standard_B2ms, B4ms, or B8ms to save costs without sacrificing needed performance.

Practical Advice

  • Collaborate with your development teams to understand workload characteristics regarding CPU, memory, GPU, and IOPS requirements.
  • Choose node sizes that best fit the application resource needs to avoid overprovisioning.

Putting It All Together: Production AKS Architecture Example

Imagine a scenario where you run a microservices-based e-commerce platform on AKS with varying traffic:

  • Baseline Traffic: 3 nodes with Standard_D4s_v3 VMs to handle normal load.
  • Autoscaling: Cluster Autoscaler set with minNodes=3, maxNodes=10.
  • Pod Autoscaling: HPA configured to scale web front-end pods between 3 and 15 replicas based on CPU utilization.
  • Burst Handling: ACI connector enabled to handle sudden traffic spikes, allowing instant pod deployment without waiting for node provisioning.

This architecture ensures:

  • Efficient resource utilization and cost savings during off-peak times.
  • Responsive scaling to maintain user experience during traffic surges.
  • Flexibility and operational simplicity leveraging Azure native integrations.

Best Practices Summary

  • Enable both Cluster Autoscaler and Horizontal Pod Autoscaler: They complement each other for node and pod scaling.
  • Define appropriate scaling boundaries: Prevent runaway costs and maintain availability.
  • Leverage Azure Container Instances for burst capacity: Fast pod scaling without adding cluster nodes.
  • Select VM sizes aligned with workload requirements: Balance CPU, memory, and cost.
  • Monitor and tune autoscaling parameters regularly: Use metrics and logs to optimize scaling behavior.

Additional Resources


Conclusion

Designing a production-grade AKS environment that is both performant and cost-effective requires a strategic approach to autoscaling and infrastructure selection. By leveraging AKS’s Cluster Autoscaler, Horizontal Pod Autoscaler, and Azure Container Instances connector, you can build a resilient and flexible platform that adapts to workload demands dynamically.

Selecting the right VM sizes and tuning autoscaling parameters based on real-world application metrics are key best practices to optimize costs. Collaboration between infrastructure, operations, and development teams ensures that the cluster configuration matches application needs precisely.

Implementing these comprehensive, practical recommendations will empower you to harness the full potential of AKS for production workloads while controlling operational expenses effectively.