Autoscaling Kubernetes apps and clusters

There are two different dimensions to scaling on Kubernetes:

  • Scaling apps: Scaling the apps means adding more replicas to cloud deployments. In other words, making sure that enough running pods are available to serve your traffic.
  • Scaling clusters / nodepools: As your app scales, you also need more resources to run that app on. At a certain point, you\’ll need to add new resources to your cluster, which will mean scaling your nodepools.

In this, we\’ll explore both. We\’ll start with autoscaling an app, and then also add a cluster auto scaler. Why don\’t we get started with creating a new cluster and deploying a small sample app:

Creating cluster and sample app

For this post, I created a new cluster and deployed a pod based on an nginx image.

az group create \\
  -n autoscale \\
  -l westus2

az aks create \\
  -n autoscale \\
  -g autoscale \\
  --enable-managed-identity

az aks get-credentials \\
  -n autoscale \\
  -g autoscale

For the deployment, we\’ll use the following:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: autoscale-demo
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
        resources:
          requests:
            cpu: 500m
            memory: 256Mi
---
apiVersion: v1
kind: Service
metadata:
  name: autoscale-service
spec:
  selector:
    app: nginx
  type: LoadBalancer
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 80

We\’ll create this using:

kubectl create -f deploy-service.yaml

Notice how we set a resources request on the deployment. This means that each pod that is part of the deployment will need half a CPU core and 256MiB of memory.


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *