<p>I have a kubernetes setup with following</p>
<pre><code>sn.  type          service  namespace
1.   statefulset   rabbitmq rabbitmq
2.   deployment    pods     default
3.   hpa           hpa      default
</code></pre>
<p>The metrics is exported on `/apis/custom.metrics.k8s.io/v1beta1 as follows</p>
<pre><code>{
  &quot;kind&quot;: &quot;APIResourceList&quot;,
  &quot;apiVersion&quot;: &quot;v1&quot;,
  &quot;groupVersion&quot;: &quot;custom.metrics.k8s.io/v1beta1&quot;,
  &quot;resources&quot;: [
    {
      &quot;name&quot;: &quot;services/rabbitmq_queue_messages_ready&quot;,
      &quot;singularName&quot;: &quot;&quot;,
      &quot;namespaced&quot;: true,
      &quot;kind&quot;: &quot;MetricValueList&quot;,
      &quot;verbs&quot;: [
        &quot;get&quot;
      ]
    },
    {
      &quot;name&quot;: &quot;namespaces/rabbitmq_queue_messages_ready&quot;,
      &quot;singularName&quot;: &quot;&quot;,
      &quot;namespaced&quot;: false,
      &quot;kind&quot;: &quot;MetricValueList&quot;,
      &quot;verbs&quot;: [
        &quot;get&quot;
      ]
    },
    {
      &quot;name&quot;: &quot;pods/rabbitmq_queue_messages_ready&quot;,
      &quot;singularName&quot;: &quot;&quot;,
      &quot;namespaced&quot;: true,
      &quot;kind&quot;: &quot;MetricValueList&quot;,
      &quot;verbs&quot;: [
        &quot;get&quot;
      ]
    }
  ]
}
</code></pre>
<p>Configuration of hpa</p>
<pre><code>apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: demo-hpa
  namespace: default
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: test-pod2
  minReplicas: 1
  maxReplicas: 2
  metrics:
  - type: Object
    object:
      metric:
        name: &quot;rabbitmq_queue_messages_ready&quot;
      describedObject:
        apiVersion: custom.metrics.k8s.io/v1/beta1
        kind: Service
        name: rabbitmq
      target:
        type: Value
        value: 50
</code></pre>
<p>Whenever I try to deploy the hpa in <code>default</code> namespace I am getting the following error.</p>
<pre><code> ScalingActive  False   FailedGetObjectMetric  the HPA was unable to compute the replica count: unable to get metric rabbitmq_queue_messages_ready: Service on default rabbitmq/unable to fetch metrics from custom metrics API: the server could not find the metric rabbitmq_queue_messages_ready for services rabbitmq
</code></pre>
<p>But when the same hpa is as set do namespace <code>rabbitmq</code> with hpa</p>
<pre><code>apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: demo-hpa
  namespace: rabbitmq
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: test-pod2
  minReplicas: 1
  maxReplicas: 2
  metrics:
  - type: Object
    object:
      metric:
        name: &quot;rabbitmq_queue_messages_ready&quot;
      describedObject:
        apiVersion: custom.metrics.k8s.io/v1/beta1
        kind: Service
        name: rabbitmq
      target:
        type: Value
        value: 50
</code></pre>
<p>and deployment.yaml as</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-pod2
  labels:
    app: test-pod2
  namespace: rabbimtq
spec:
  replicas: 1
  selector:
    matchLabels:
      app: test-pod2
  template:
    metadata:
      labels:
        app: test-pod2
    spec:
      containers:
        - name: test-pod2
          image: golang:1.16
          command: [&quot;sh&quot;, &quot;-c&quot;, &quot;tail -f /etc/hosts&quot;]
</code></pre>
<p>The deployment works perfect.</p>
<p>Is there a way, I can export the metrics for service rabbitmq from <code>rabbitmq</code> namespace to <strong>any other namespaces</strong> which I can then use for scaling???</p>

dempti
<p>HPA is a <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/" rel="nofollow noreferrer">namespaced</a> resource. It means that it can only scale Deployments which are in the same Namespace as the HPA itself. That's why it is only working when both HPA and Deployment are in the <code>namespace: rabbitmq</code>. You can check it within your cluster by running:</p>
<pre><code>    kubectl api-resources --namespaced=true | grep hpa
    NAME                        SHORTNAMES   APIGROUP                    NAMESPACED   KIND
    horizontalpodautoscalers    hpa          autoscaling                 true         HorizontalPodAutoscaler
</code></pre>
<p>The easiest way to make it work would be to simply set the <code>Namespace</code> value of the HPA to the same Namespace of Deployment which you want to scale. For example, if your deployment is set like below:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: rabbimtq
</code></pre>
<p>the HPA must also be in the same Namespace:</p>
<pre><code>apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  namespace: rabbitmq
</code></pre>
<p>In other words: Set the <code>metadata.namespace</code> value of your HPA to the <code>metadata.namespace</code> value of your Deployment as the HPA is not able to scale Deployments outside of its Namespace.</p>
<p>Namespaces are like separate &quot;boxes&quot; in your Cluster. Namespaced resources cannot work outside of the Namespace they are into. They can't see resources that are not in their &quot;box&quot;.</p>
<p>By doing it that way there would be no need to reconfigure the custom metrics.</p>
