<p><strong>EDIT:</strong></p>

<p><strong>SOLUTION:</strong> I forgot to add <code>target_cpu_utilization_percentage</code> to <code>autoscaler.tf</code> file</p>

<hr>

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

<p>I created a <code>Deployment</code> and a <code>Horizontal Autoscaler</code> but is not working.</p>

<p>I'm using Terraform to configure Kubernetes.</p>

<p>I have this files:</p>

<p><strong>Deployments.tf</strong></p>

<pre><code>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             = "***************************"
        }
      }
    }
  }
}
</code></pre>

<p><strong>Autoscaler.tf</strong></p>

<pre><code>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
    }
  }
}
</code></pre>

<p><strong>Service.tf</strong></p>

<pre><code>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
    }
  }
}
</code></pre>

<p>When I run <code>kubectl get hpa</code> I see this:</p>

<pre><code>NAME       REFERENCE             TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
rui-test   Deployment/rui-test   &lt;unknown&gt;/80%   1         3         1          1h
</code></pre>

<p>Instead of:</p>

<pre><code>rui-test   Deployment/rui-test   &lt;unknown&gt;/79%   3         10        1          1h
</code></pre>

<p>That is what I want.</p>

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

<pre><code>Error from server (AlreadyExists): horizontalpodautoscalers.autoscaling "rui-test" already exists
</code></pre>

<p>In kubernetes appear this</p>

<p><a href="https://i.stack.imgur.com/r91fw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/r91fw.png" alt="enter image description here"></a></p>

Rui Martins
<p>You are missing the <a href="https://github.com/kubernetes-incubator/metrics-server" rel="nofollow noreferrer">metrics server</a>. Kubernetes needs to determine current CPU/Memory usage so that it can autoscale up and down.</p>

<p>One way to know if you have the metrics server installed is to run:</p>

<pre><code>$ kubectl top node
$ kubectl top pod
</code></pre>
