Autoscaling in Kubernetes

Bhavin Gandhi

@_bhavin192

geeksocket.in

What is autoscaling?

autoscaling-explained.png

Image Credits: http://blog.infracloud.io/kubernetes-autoscaling-explained

Why you need autoscaling?

  • Cost saving
  • Less downtime

What to autoscale

  • Application instances (Pods)
  • Nodes

When to autoscale

  • What are metrics

How to autoscale

Autoscaling Nodes

Autoscaling Pods

  • Horizontally
  • Vertically

HorizontalPodAutoscaler

  • Controller loop
    • Looks for certain metric values and takes decisions based on those values
  • Fetches metrics values from the end point
    • metrics.k8s.io
    • custom.metrics.k8s.io
    • external.metrics.k8s.io

horizontal-pod-autoscaler.png

Image Credits: https://git.k8s.io/website (CC BY 4.0)

Autoscaling based on CPU/RAM

core-metrics.png

Image Credits: http://blog.infracloud.io/kubernetes-autoscaling-custom-metrics

Demo

# Create a deployment
$ kubectl run stress-deploy \
  --image=monitoringartist/docker-killer:latest \
  --limits="cpu=200m,memory=512Mi" \
  --requests="cpu=200m,memory=512Mi" \
  --env="TIMEOUT=10000" -- cpubomb

# Autoscale it based on CPU usage
$ kubectl autoscale deployment \
  --max="100" --min="3" \
  --cpu-percent="80" \
  stress-deploy

Check the status

# Watch HorizontalPodAutoscaler
$ kubectl get hpa -w

# Watch Pods
$ kubectl get pods -w

# Get list of Nodes
$ kubectl get nodes

HPA specification

apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: stress-deploy
spec:
  scaleTargetRef:
    apiVersion: apps/v1beta1
    kind: Deployment
    name: stress-deploy
  minReplicas: 3
  maxReplicas: 100
  metrics:
  - type: Resource
    resource:
      name: cpu
      targetAverageUtilization: 80

Autoscaling based on custom metrics

Using Prometheus and Prometheus adapter

Using Datadog's Cluster Agent

What's next

Thank you