How to Horizontal Autoscaler a Kubernetes Deployment

4/8/2019

EDIT:

SOLUTION: I forgot to add target_cpu_utilization_percentage to autoscaler.tf file


I want a web-service in Python (or other language) running on Kubernetes but with auto scaling.

I created a Deployment and a Horizontal Autoscaler but is not working.

I'm using Terraform to configure Kubernetes.

I have this files:

Deployments.tf

resource "kubernetes_deployment" "rui-test" {
  metadata {
    name = "rui-test"
    labels {
      app  = "rui-test"
    }
  }
  spec {
    strategy = {
      type = "RollingUpdate"
      rolling_update = {
        max_unavailable = "26%" # This is not working
      }
    }
    selector = {
        match_labels = {
            app = "rui-test"
        }
    }
    template = {
      metadata = {
        labels = {
          app = "rui-test"
        }
      }
      spec = {
        container {
          name              = "python-test1"
          image             = "***************************"
        }
      }
    }
  }
}

Autoscaler.tf

resource "kubernetes_horizontal_pod_autoscaler" "test-rui" {    
  metadata {
    name = "test-rui"
  }
  spec {
    max_replicas = 10 # THIS IS NOT WORKING
    min_replicas = 3  # THIS IS NOT WORKING

    scale_target_ref {
      kind = "Deployment"
      name = "test-rui" # Name of deployment
    }
  }
}

Service.tf

resource "kubernetes_service" "rui-test" {
  metadata {
    name = "rui-test"
    labels {
      app  = "rui-test"
    }
  }
  spec {
    selector {
      app  = "rui-test"
    }
    type = "LoadBalancer"  # Use 'cluster_ip = "None"' or 'type = "LoadBalancer"'
    port {
      name = "http"
      port = 8080
    }
  }
}

When I run kubectl get hpa I see this:

NAME       REFERENCE             TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
rui-test   Deployment/rui-test   <unknown>/80%   1         3         1          1h

Instead of:

rui-test   Deployment/rui-test   <unknown>/79%   3         10        1          1h

That is what I want.

But if I run kubectl autoscale deployment rui-test --min=3 --max=10 --cpu-percent=81 I see this:

Error from server (AlreadyExists): horizontalpodautoscalers.autoscaling "rui-test" already exists

In kubernetes appear this

enter image description here

-- Rui Martins
autoscaling
kubernetes
terraform

2 Answers

4/8/2019

You are missing the metrics server. Kubernetes needs to determine current CPU/Memory usage so that it can autoscale up and down.

One way to know if you have the metrics server installed is to run:

$ kubectl top node
$ kubectl top pod
-- Rico
Source: StackOverflow

4/8/2019

The Horizontal Pod AutoScaler is dependent on resource limits being configured for your Deployment.

From the documentation:

Please note that if some of the pod’s containers do not have the relevant resource request set, CPU utilization for the pod will not be defined and the autoscaler will not take any action for that metric.

-- Blokje5
Source: StackOverflow