具有不同命名空间的 HPA

HPA with different namespaces

我的 kubernetes 设置如下

sn.  type          service  namespace
1.   statefulset   rabbitmq rabbitmq
2.   deployment    pods     default
3.   hpa           hpa      default

指标在 `/apis/custom.metrics.k8s.io/v1beta1 上导出如下

{
  "kind": "APIResourceList",
  "apiVersion": "v1",
  "groupVersion": "custom.metrics.k8s.io/v1beta1",
  "resources": [
    {
      "name": "services/rabbitmq_queue_messages_ready",
      "singularName": "",
      "namespaced": true,
      "kind": "MetricValueList",
      "verbs": [
        "get"
      ]
    },
    {
      "name": "namespaces/rabbitmq_queue_messages_ready",
      "singularName": "",
      "namespaced": false,
      "kind": "MetricValueList",
      "verbs": [
        "get"
      ]
    },
    {
      "name": "pods/rabbitmq_queue_messages_ready",
      "singularName": "",
      "namespaced": true,
      "kind": "MetricValueList",
      "verbs": [
        "get"
      ]
    }
  ]
}

hpa配置

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: "rabbitmq_queue_messages_ready"
      describedObject:
        apiVersion: custom.metrics.k8s.io/v1/beta1
        kind: Service
        name: rabbitmq
      target:
        type: Value
        value: 50

每当我尝试在 default 命名空间中部署 hpa 时,我都会收到以下错误。

 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

但是当相同的 hpa 与 set do namespace rabbitmq with hpa

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: "rabbitmq_queue_messages_ready"
      describedObject:
        apiVersion: custom.metrics.k8s.io/v1/beta1
        kind: Service
        name: rabbitmq
      target:
        type: Value
        value: 50

和deployment.yaml作为

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: ["sh", "-c", "tail -f /etc/hosts"]

部署完美。

有没有办法,我可以将服务 rabbitmq 的指标从 rabbitmq 命名空间导出到 任何其他命名空间,然后我可以将其用于扩展???

HPA 是一种 namespaced 资源。这意味着它只能扩展与 HPA 本身位于同一命名空间中的部署。这就是为什么只有当 HPA 和 Deployment 都在 namespace: rabbitmq 中时它才有效。您可以通过 运行:

在集群中检查它
    kubectl api-resources --namespaced=true | grep hpa
    NAME                        SHORTNAMES   APIGROUP                    NAMESPACED   KIND
    horizontalpodautoscalers    hpa          autoscaling                 true         HorizontalPodAutoscaler

使其工作的最简单方法是简单地将 HPA 的 Namespace 值设置为您要扩展的相同部署命名空间。例如,如果您的部署设置如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: rabbimtq

HPA 也必须在同一个命名空间中:

apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  namespace: rabbitmq

换句话说:将 HPA 的 metadata.namespace 值设置为部署的 metadata.namespace 值,因为 HPA 无法在其命名空间之外扩展部署。

命名空间就像集群中的独立“盒子”。命名空间资源不能在它们所在的命名空间之外工作。他们看不到不在他们“盒子”中的资源。

这样做就不需要重新配置自定义指标。