<p>I'm trying to deploy my own HPA without a success.
Although while trying to deploy the official PHP of kubernetes, it worked as planned.
When I tryied to deploy my own Test Deployment with the HPA, it didn't work.</p>
<h1>Compare 2 HPA deployments processes - Kubernetes official deployment VS My Test deployment:</h1>
<p><em><strong>Deploy the Official Kubernetes template image:</strong></em></p>
<pre><code>$ kubectl run php-apache --image=gcr.io/google_containers/hpa-example --requests=cpu=200m --expose --port=80
service &quot;php-apache&quot; created
deployment &quot;php-apache&quot; created
</code></pre>
<p><em><strong>My own Test Deployment result</strong></em></p>
<pre><code>{
   &quot;apiVersion&quot;: &quot;autoscaling/v1&quot;,
   &quot;kind&quot;: &quot;HorizontalPodAutoscaler&quot;,
   &quot;metadata&quot;: {
       &quot;annotations&quot;: {
           &quot;autoscaling.alpha.kubernetes.io/conditions&quot;: &quot;[{\&quot;type\&quot;:\&quot;AbleToScale\&quot;,\&quot;status\&quot;:\&quot;False\&quot;,\&quot;lastTransitionTime\&quot;:\&quot;2019-12-22T20:39:59Z\&quot;,\&quot;reason\&quot;:\&quot;FailedGetScale\&quot;,\&quot;message\&quot;:\&quot;the HPA controller was unable to get the target's current scale: deployments/scale.apps \\\&quot;gw-autoscale-t6\\\&quot; not found\&quot;}]&quot;
       },
       &quot;creationTimestamp&quot;: &quot;2019-12-22T20:39:44Z&quot;,
       &quot;labels&quot;: {
           &quot;app&quot;: &quot;gw-autoscale-t6&quot;
       },
       &quot;name&quot;: &quot;gw-autoscale-t6&quot;,
       &quot;namespace&quot;: &quot;dev&quot;,
       &quot;resourceVersion&quot;: &quot;17299134&quot;,
       &quot;selfLink&quot;: &quot;/apis/autoscaling/v1/namespaces/dev/horizontalpodautoscalers/gw-autoscale-t6&quot;,
       &quot;uid&quot;: &quot;2f7e014c-24fb-11ea-a4d8-a28620329da6&quot;
   },
   &quot;spec&quot;: {
       &quot;maxReplicas&quot;: 3,
       &quot;minReplicas&quot;: 1,
       &quot;scaleTargetRef&quot;: {
           &quot;apiVersion&quot;: &quot;apps/v1&quot;,
           &quot;kind&quot;: &quot;Deployment&quot;,
           &quot;name&quot;: &quot;gw-autoscale-t6&quot;
       },
       &quot;targetCPUUtilizationPercentage&quot;: 80
   },
   &quot;status&quot;: {
       &quot;currentReplicas&quot;: 0,
       &quot;desiredReplicas&quot;: 0
   }
}
</code></pre>
<p><em><strong>The HPA deployment yaml I used for both of the deployment above</strong></em>
<strong>(Official Deployment and my Test Deployment):</strong></p>
<pre><code>apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: {{ .Values.name }}
  labels:
    app: {{ .Values.name }}
  namespace: {{ .Values.namespace }}
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: {{ .Values.name }}
  minReplicas: {{ .Values.spec.replicaCountMin }}
  maxReplicas: {{ .Values.spec.replicaCountMax }}
  targetCPUUtilizationPercentage: 50
  selector:
    matchLabels:
      app: {{ .Values.name }}
  template:
    metadata:
      labels:
        app: {{ .Values.name }}
        release: {{ .Release.Name }}
        heritage: {{ .Release.Service }}
    spec:
      containers:
      - name: {{ .Chart.Name }}
        image: &quot;{{ .Values.image.repository }}:{{ .Values.image.tag }}&quot;
</code></pre>
<h2>The results:</h2>
<p><em><strong>PHP HPA is working as planned</strong></em></p>
<pre><code>NAME        REFERENCE              TARGETS  MINPODS   MAXPODS   REPLICAS   AGE
php-apache  Deployment/php-apache  0%/50%    1        3         1          3d7h
</code></pre>
<p><em><strong>Test Deployment HPA is not working</strong></em></p>
<pre><code>NAME              REFERENCE                    TARGETS         MINPODS  MAXPODS   REPLICAS   AGE
gw-autoscale-t6   Deployment/gw-autoscale-t6   &lt;unknown&gt;/80%   1        3         0          11m
</code></pre>
<p><em><strong>The Test Deployment HPA error</strong></em></p>
<pre><code>Name:                                                  gw-autoscale-t6
Namespace:                                             dev
Labels:                                                app=gw-autoscale-t6
Annotations:                                           &lt;none&gt;
CreationTimestamp:                                     Sun, 22 Dec 2019 22:39:44 +0200
Reference:                                             Deployment/gw-autoscale-t6
Metrics:                                               ( current / target )
  resource cpu on pods  (as a percentage of request):  &lt;unknown&gt; / 80%
Min replicas:                                          1
Max replicas:                                          3
Deployment pods:                                       0 current / 0 desired
Conditions:
  Type         Status  Reason          Message
  ----         ------  ------          -------
  AbleToScale  False   FailedGetScale  the HPA controller was unable to get the target's current scale: deployments/scale.apps &quot;gw-autoscale-t6&quot; not found
Events:
  Type     Reason          Age                 From                       Message
  ----     ------          ----                ----                       -------
  Warning  FailedGetScale  27s (x81 over 20m)  horizontal-pod-autoscaler  deployments/scale.apps &quot;gw-autoscale-t6&quot; not found
</code></pre>
<ul>
<li><p>I have also tried a lot of other types of deployment files.</p>
</li>
<li><p>My Metrics-Service is installed.</p>
</li>
<li><p>I'm installing my deployment with Helm.</p>
</li>
</ul>
<p><strong>What can I do to solve this one?</strong></p>
<hr />
<h2>The solution I found was to add the 'resources' property to the Test Deployment yaml file:</h2>
<p>For instance: in case of scale by cpu usage, use it as followed in the deployment file.</p>
<pre><code>resources:
    requests:
       cpu: {{ .Values.request.cpu }}
    limits:
       cpu: {{ .Values.limits.cpu }}
</code></pre>

<p>The way I solved it is by adding the cpu resources to the deployment's file and keeping only the necessary HPA deployment yaml fields.</p>
<p><strong>HPA deployment file</strong></p>
<pre><code>apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: {{ .Values.name }}
  labels:
    app: {{ .Values.name }}
  namespace: {{ .Values.namespace }}
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: {{ .Values.name }}
  minReplicas: {{ .Values.spec.replicaCountMin }} 
  maxReplicas: {{ .Values.spec.replicaCountMax }} 
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: {{ .Values.spec.cpuUtil }} 

</code></pre>
<p><strong>Deployment file - resource addition</strong></p>
<pre><code>apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: {{ .Values.name }}
  labels:
    app: {{ .Values.name }}
  namespace: {{ .Values.namespace }}
spec:
  replicas: {{ .Values.replicaCount }}
  template:
    metadata:
      labels:
        app: {{ .Values.name }}
        release: {{ .Release.Name }}
        heritage: {{ .Release.Service }}
    spec:
      containers:
      - name: {{ .Chart.Name }}
        image: &quot;{{ .Values.image.repository }}:{{ .Values.image.tag }}&quot;
        resources:
          requests:
            cpu: {{ .Values.request.cpu }}
          limits:
            cpu: {{ .Values.limits.cpu }} 
</code></pre>
<p><strong>You must add this code:</strong></p>
<pre><code>resources:
  requests:
     cpu: {{ .Values.request.cpu }}
  limits:
     cpu: {{ .Values.limits.cpu }}
</code></pre>
<p>Hope it helps :)</p>
