<p><a href="https://i.stack.imgur.com/sjgU9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sjgU9.png" alt="enter image description here" /></a></p>
<p>I want to use the pod network received bytes count standard kubernetes metrics on HPA . Using following yaml to accomplish that but getting errors like unable to fetch metrics from custom metrics API: no custom metrics API (custom.metrics.k8s.io) registered</p>
<pre><code>apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: xxxx-hoa
  namespace: xxxxx
spec:
  scaleTargetRef:
    apiVersion: apps/v1beta1
    kind: Deployment
    name: xxxx-xxx
  minReplicas: 2
  maxReplicas: 6
  metrics:
  - type: Pods
    pods:
      metricName: received_bytes_count
      targetAverageValue: 20k
</code></pre>
<p>If anybody had an experience with same kind of metrics usage that would be greatly helpful</p>
<p><a href="https://i.stack.imgur.com/kLrx5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kLrx5.png" alt="enter image description here" /></a></p>

<p><strong>Solution</strong></p>
<p>To make it work, you need to deploy <a href="https://cloud.google.com/kubernetes-engine/docs/tutorials/autoscaling-metrics#step1" rel="nofollow noreferrer">Stackdriver Custom Metrics Adapter</a>. Below commands to deploy it.</p>
<pre><code>$ kubectl create clusterrolebinding cluster-admin-binding \
    --clusterrole cluster-admin --user &quot;$(gcloud config get-value account)&quot;

$ kubectl apply -f https://raw.githubusercontent.com/GoogleCloudPlatform/k8s-stackdriver/master/custom-metrics-stackdriver-adapter/deploy/production/adapter_new_resource_model.yaml
</code></pre>
<p>Later you need to use proper <code>Custom Metric</code>, in your case it should be <code>kubernetes.io|pod|network|received_bytes_count</code></p>
<p><strong>Description</strong></p>
<p>In <a href="https://cloud.google.com/kubernetes-engine/docs/concepts/custom-and-external-metrics" rel="nofollow noreferrer">Custom and external metrics for autoscaling workloads</a> documentation you have information that you need to deploy <code>StackDriver Adapter</code> before you will be able to get custom metrics.</p>
<blockquote>
<p>Before you can use custom metrics, you must enable Monitoring in your Google Cloud project and install the Stackdriver adapter on your cluster.</p>
</blockquote>
<p>Next step is to deploy your application (I've used Nginx deployment for test purpose) and create proper HPA.</p>
<p>In your HPA example, you have a few issues</p>
<pre><code>apiVersion: autoscaling/v2beta1 ## you can also use autoscaling/v2beta2 if you need more features, however for this scenario is ok
kind: HorizontalPodAutoscaler
metadata:
  name: xxxx-hoa
  namespace: xxxxx # HPA have namespace specified, deployment doesnt have
spec:
  scaleTargetRef:
    apiVersion: apps/v1beta1 # apiVersion: apps/v1beta1 is quite old. In Kubernetes 1.16+ it was changed to apps/v1
    kind: Deployment
    name: xxxx-xxx
  minReplicas: 2
  maxReplicas: 6
  metrics:
  - type: Pods
    pods:
      metricName: received_bytes_count # this metrics should be replaced with kubernetes.io|pod|network|received_bytes_count
      targetAverageValue: 20k
</code></pre>
<p>In GKE you can choose between <code>autoscaling/v2beta1</code> and <code>autoscaling/v2beta2</code>. Your case will work with both <code>apiVersions</code>, however if you would decide to use <code>autoscaling/v2beta2</code> you will need to change manifest syntax.</p>
<p>Why <code>kubernetes.io/pod/network/received_bytes_count</code> ?
You are refering to Kubernetes metrics, and <code>/pod/network/received_bytes_count</code> is provided in <a href="https://cloud.google.com/monitoring/api/metrics_kubernetes" rel="nofollow noreferrer">this docs</a>.</p>
<p>Why <code>|</code> instead of <code>/</code>? If you will check <a href="https://github.com/GoogleCloudPlatform/k8s-stackdriver/tree/master/custom-metrics-stackdriver-adapter#metric-names" rel="nofollow noreferrer">Stackdriver documentation on Github</a> you will find information.</p>
<blockquote>
<p>Stackdriver metrics have a form of paths separated by &quot;/&quot; character, but Custom Metrics API forbids using &quot;/&quot; character. When using Custom Metrics - Stackdriver Adapter either directly via Custom Metrics API or by specifying a custom metric in HPA, replace &quot;/&quot; character with &quot;|&quot;. For example, to use custom.googleapis.com/my/custom/metric, specify custom.googleapis.com|my|custom|metric.</p>
</blockquote>
<p><strong>Proper Configuration</strong></p>
<p><strong>for v2beta1</strong></p>
<pre><code>apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: xxxx-hoa
spec:
  scaleTargetRef:
    apiVersion: apps/v1 # In your case should be apps/v1beta1 but my deployment was created with apps/v1 apiVersion
    kind: Deployment
    name: nginx
  minReplicas: 2
  maxReplicas: 6
  metrics:
  - type: Pods
    pods:
      metricName: &quot;kubernetes.io|pod|network|received_bytes_count&quot;
      targetAverageValue: 20k
</code></pre>
<p><strong>for v2beta2</strong></p>
<pre><code>apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: xxxx-hoa
  namespace: default
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: nginx
  minReplicas: 2
  maxReplicas: 6
  metrics:
  - type: Pods
    pods:
      metric:
        name: &quot;kubernetes.io|pod|network|received_bytes_count&quot;
      target:
        type: AverageValue
        averageValue: 20k
</code></pre>
<p><strong>Tests Outputs</strong></p>
<pre><code>Conditions:
  Type            Status  Reason            Message
  ----            ------  ------            -------
  AbleToScale     True    SucceededRescale  the HPA controller was able to update the target scale to 2
  ScalingActive   True    ValidMetricFound  the HPA was able to successfully calculate a replica count from pods metric kubernetes.io|pod|network|received_bytes_count
  ScalingLimited  True    TooFewReplicas    the desired replica count is more than the maximum replica count
Events:
  Type    Reason             Age                 From                       Message
  ----    ------             ----                ----                       -------
  Normal  SuccessfulRescale  8m18s               horizontal-pod-autoscaler  New size: 4; reason: pods metric kubernetes.io|pod|network|received_bytes_count above target
  Normal  SuccessfulRescale  8m9s                horizontal-pod-autoscaler  New size: 6; reason: pods metric kubernetes.io|pod|network|received_bytes_count above target
  Normal  SuccessfulRescale  17s                 horizontal-pod-autoscaler  New size: 5; reason: All metrics below target
  Normal  SuccessfulRescale  9s (x2 over 8m55s)  horizontal-pod-autoscaler  New size: 2; reason: All metrics below target
</code></pre>
<p><strong>Possible Issues with your current configuration</strong></p>
<p>In your HPA you have specified namespace but not in your target Deployment. Both, HPA and deployment should have the same namespace. With this mismatch configuration you can encounter issue below:</p>
<pre><code>Conditions:
  Type         Status  Reason          Message
  ----         ------  ------          -------
  AbleToScale  False   FailedGetScale  the HPA controller was unable to get the target's current scale: deployments/scale.apps &quot;nginx&quot; not found
Events:
  Type     Reason          Age                  From                       Message
  ----     ------          ----                 ----                       -------
  Warning  FailedGetScale  94s (x264 over 76m)  horizontal-pod-autoscaler  deployments/scale.apps &quot;nginx&quot; not found
</code></pre>
<p>In Kubernetes 1.16+, deployments are using <code>apiVersion: apps/v1</code>, you won't be able to create deployment with <code>apiVersion: apps/v1beta1</code> in Kubernets 1.16+</p>
