0

我安装了 Kubernetes 集群和 Prometheus/Prometheus 适配器。

这是prometheus适配器配置规则:

rules:
  custom:
  - seriesQuery: '{__name__=~"container_cpu_usage_seconds_total"}'
    resources:
      overrides:
      template: "<<.Resource>>"
#          namespace:
#            resource: namespace
#          pod:
#            resource: pod
    name:
      matches: "container_cpu_usage_seconds_total"
      as: "my_custom_metric"
    metricsQuery: sum(<<.Series>>{container="php-apache"}) by (<<.GroupBy>>)

这是我的 hpa 配置:

apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: php-apache
  spec:
scaleTargetRef:
 apiVersion: apps/v1
 kind: Deployment
 name: php-apache
minReplicas: 1
maxReplicas: 6
metrics:
- type: Pods
  pods:
    metric:
      name: my_custom_metric
    target:
      type: Value
      averageValue: 250 //limit
---
apiVersion: v1
kind: Service
metadata:
  name: php-apache
  labels:
    run: php-apache
spec:
  ports:
  - port: 80
  selector:
    run: php-apache

这里的问题是我想根据container=php-apache使用的副本的摘要而不是它们的平均值进行扩展。

这是从 Prometheus 适配器返回的值:

{
  "kind": "MetricValueList",
  "apiVersion": "custom.metrics.k8s.io/v1beta1",
  "metadata": {
    "selfLink": "/apis/custom.metrics.k8s.io/v1beta1/namespaces/default/pods/%2A/malakas"
  },
  "items": [
    {
      "describedObject": {
        "kind": "Pod",
        "namespace": "default",
        "name": "php-apache-d4cf67d68-8ddbx",
        "apiVersion": "/v1"
      },
      "metricName": "my_custom_metric",
      "timestamp": "2021-04-16T10:52:02Z",
      "value": "331827m",
      "selector": null
    },
    {
      "describedObject": {
        "kind": "Pod",
        "namespace": "default",
        "name": "php-apache-d4cf67d68-zxkrd",
        "apiVersion": "/v1"
      },
      "metricName": "my_custom_metric",
      "timestamp": "2021-04-16T10:52:02Z",
      "value": "44478m",
      "selector": null
    }
  ]
}

在此示例中,有 2 个副本。我想得到一个结果(这两个的总和),而不是像上面那样的两个结果,以便将结果传递给 hpa 并相应地缩放。

我怎样才能做到这一点?

4

1 回答 1

0

您应该使用来自服务而不是来自 pod 的指标:

kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1/namespaces/default/services/*/my_custom_metric"
于 2022-01-19T12:11:43.493 回答