The Art of ScalingšŸ“ˆ in Kubernetes: HPA, VPA, and Cluster Autoscalerā˜øļø

Saurabh AdhauSaurabh Adhau
5 min read

Kubernetes, often abbreviated as K8s, is a powerful container orchestration platform that has revolutionized the way applications are deployed and managed. One of its key features is the ability to scale applications seamlessly, ensuring optimal resource utilization and high availability. In this article, we will dive deep into the world of scaling in Kubernetes, exploring the different types of scaling, their features, and their disadvantages. Weā€™ll also use the banking industry as an example to illustrate scaling in K8s, complete with YAML files and commands for both scaling in and scaling out.

Types of Scaling in Kubernetesā˜øļø:

Horizontal Pod Autoscaling (HPA):

  • Features: HPA automatically adjusts the number of replicas of a pod based on CPU or memory usage metrics. It ensures that your application can handle varying workloads efficiently.

  • Disadvantages: It may not be suitable for applications with highly fluctuating or unpredictable traffic, and fine-tuning the autoscaling metrics can be challenging.

Vertical Pod Autoscaling (VPA):

  • Features: VPA adjusts the resource requests and limits of containers within a pod to optimize resource utilization. This is ideal for applications with varying resource demands.

  • Disadvantages: VPA might require extra configuration and could impact pod stability if not implemented correctly.

Cluster Autoscaler:

  • Features: The Cluster Autoscaler automatically adjusts the size of your cluster by adding or removing nodes based on resource requirements. It helps maintain a cost-effective and performant cluster.

  • Disadvantages: Scaling the cluster can take some time, which may not be suitable for applications requiring near-instantaneous scaling.

Scaling in the Banking Industry:

Imagine a banking application that experiences varying traffic throughout the day, with peak hours seeing a surge in user activity. Hereā€™s how you can implement scaling in Kubernetes to ensure a smooth banking experience:

Horizontal Pod Autoscaling (HPA):

  • Create an HPA resource to scale the banking applicationā€™s pods based on CPU utilization. For example, an HPA YAML file may look like this:
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: banking-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: banking-app
  minReplicas: 2
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        targetAverageUtilization: 60

Use the following command to apply the HPA resource:

kubectl apply -f banking-hpa.yaml
  • apiVersion and kind: These fields specify the resource type, which is an HPA in the autoscaling/v2beta2 version.

  • metadata: Here, you provide metadata for the HPA resource, including its name, which is set to "banking-hpa" in this example.

  • spec: The specification section contains the core configuration for HPA:

  • scaleTargetRef: This section specifies the reference to the deployment you want to scale. In this case, it refers to a deployment named "banking-app."

  • minReplicas and maxReplicas: These fields set the minimum and maximum number of replicas the HPA will manage. It ensures the application always has at least 2 replicas and scales up to a maximum of 10 replicas based on the defined metrics.

  • metrics: HPA can use various metrics for autoscaling, and in this example, we are using the Resource metric type for CPU utilization. The targetAverageUtilization field sets the target CPU utilization percentage at which the HPA will start scaling.

Vertical Pod Autoscaling (VPA):

Vertical Pod Autoscaling (VPA) is a feature in Kubernetes that automatically adjusts the resource requests and limits of containers within a pod to optimize resource utilization. Itā€™s particularly useful for applications with varying resource demands.

Hereā€™s an example YAML file to configure VPA for a banking application:

apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: banking-vpa
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: "Deployment"
    name: "banking-app"
  updatePolicy:
    updateMode: "Auto"

Use the following command to apply the VPA resource:

kubectl apply -f banking-vpa.yaml
  • apiVersion and kind: These fields define the resource type, which is a VerticalPodAutoscaler in the autoscaling.k8s.io/v1 version.

  • metadata: The metadata section provides a name for the VPA resource, which is set to "banking-vpa" in this example.

  • spec: The specification section contains the core configuration for VPA:

  • targetRef: This section specifies the reference to the deployment you want to scale vertically. In this case, it refers to a deployment named "banking-app."

  • updatePolicy: VPA provides an updateMode field. When set to "Auto," VPA automatically tunes resource requests and limits for containers in the specified pod based on historical resource usage.

Using the above VPA YAML file, you can enable Vertical Pod Autoscaling for your banking application, allowing Kubernetes to optimize resource requests and limits to better match the applicationā€™s actual resource usage.

Cluster Autoscaler:

  • Configure the Cluster Autoscaler to add or remove nodes dynamically based on resource demands. A typical Cluster Autoscaler YAML file might resemble this:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cluster-autoscaler
spec:
  replicas: 1
  template:
    spec:
      containers:
        - name: cluster-autoscaler
          image: k8s.gcr.io/cluster-autoscaler
          command:
            - cluster-autoscaler
            - --scale-down-utilization-threshold=0.5
            - --scale-down-delay-after-add=3m
            - --scale-down-unneeded-time=5m
          env:
            - name: MY_NODE_POOL
              value: "default-pool"

Deploy the Cluster Autoscaler with:

kubectl apply -f cluster-autoscaler.yaml
  • apiVersion and kind: These fields define the resource type, which is a Deployment in the apps/v1 version.

  • metadata: The metadata section provides a name for the deployment, which is "cluster-autoscaler" in this case.

  • spec: This section contains the deployment's configuration:

  • replicas: Sets the number of replica pods for the deployment, which is 1 in this example.

  • template: Specifies the pod template for the deployment. It defines the pod's specification, including its containers.

  • containers: This array contains the container specification for the pod. In this case, it uses the "cluster-autoscaler" image from the Google Container Registry.

  • command: The command field specifies the command to run when starting the container. Here, it runs the Cluster Autoscaler with various command-line options.

  • env: You can provide environment variables for the container. In this example, it sets the MY_NODE_POOL variable to "default-pool."

Conclusion šŸŒŸ

Kubernetes provides powerful tools for scaling applications in various ways, and each approach has its own set of features and disadvantages. By intelligently using Horizontal Pod Autoscaling and Cluster Autoscaler, industries like banking can maintain a responsive and cost-effective application while ensuring resource efficiency.

With these three scaling methods (Horizontal Pod Autoscaling, Vertical Pod Autoscaling, and Cluster Autoscaler) in your toolkit, you can ensure your Kubernetes-based banking application remains responsive, efficient, and cost-effective.

As Kubernetes continues to evolve, the ability to scale applications seamlessly and efficiently will remain a critical component of its appeal to businesses across various industries.

20
Subscribe to my newsletter

Read articles from Saurabh Adhau directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Saurabh Adhau
Saurabh Adhau

As a DevOps Engineer, I thrive in the cloud and command a vast arsenal of tools and technologies: ā˜ļø AWS and Azure Cloud: Where the sky is the limit, I ensure applications soar. šŸ”Ø DevOps Toolbelt: Git, GitHub, GitLab ā€“ I master them all for smooth development workflows. šŸ§± Infrastructure as Code: Terraform and Ansible sculpt infrastructure like a masterpiece. šŸ³ Containerization: With Docker, I package applications for effortless deployment. šŸš€ Orchestration: Kubernetes conducts my application symphonies. šŸŒ Web Servers: Nginx and Apache, my trusted gatekeepers of the web.