0

我有一个 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 时,请rabbitmq使用 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命名空间导出到任何其他命名空间,然后我可以将其用于扩展???

4

1 回答 1

1

HPA 是一个命名空间资源。这意味着它只能扩展与 HPA 本身在同一命名空间中的部署。这就是为什么它只有在 HPA 和 Deployment 都在namespace: rabbitmq. 您可以通过运行以下命令在集群中检查它:

    kubectl api-resources --namespaced=true | grep hpa
    NAME                        SHORTNAMES   APIGROUP                    NAMESPACED   KIND
    horizontalpodautoscalers    hpa          autoscaling                 true         HorizontalPodAutoscaler

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

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

HPA 也必须在相同的命名空间中:

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

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

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

通过这种方式,无需重新配置自定义指标。

于 2021-03-02T10:21:19.613 回答