Kubernetes HPA 不使用 istio 上的 prometheus 适配器使用自定义指标进行缩放
Kubernetes HPA not scaling with custom metric using prometheus adapter on istio
我有两个部署 运行 Istio 中同一服务的 v1 和 v2。我已经设置了一个自定义指标 'istio-total-requests' 以通过 prometheus 适配器收集。
我已经设置了一个 HPA 来扩展 v2 部署,当我发送请求时可以看到目标值在增加,但是没有发生的是 HPA 没有扩展 pods/replicas 的数量。
我是 运行 minikube v1.13.1 上的 kubernetes v1.19,无法理解为什么它不能扩展。
prometheus:
url: http://prometheus.istio-system.svc.cluster.local
rules:
default: false
custom:
# this rule matches cumulative cAdvisor metrics measured in seconds
- seriesQuery: 'istio_requests_total{kubernetes_namespace!="",kubernetes_pod_name!=""}'
seriesFilters: []
resources:
# template: <<.Resource>>
# skip specifying generic resource<->label mappings, and just
# attach only pod and namespace resources by mapping label names to group-resources
overrides:
kubernetes_namespace: {resource: "namespace"}
kubernetes_pod_name: {resource: "pod"}
# specify that the `container_` and `_seconds_total` suffixes should be removed.
# this also introduces an implicit filter on metric family names
name:
# we use the value of the capture group implicitly as the API name
# we could also explicitly write `as: ""`
matches: "^(.*)_total"
as: "_per_second"
# matches: ""
# as: ""
# specify how to construct a query to fetch samples for a given series
# This is a Go template where the `.Series` and `.LabelMatchers` string values
# are available, and the delimiters are `<<` and `>>` to avoid conflicts with
# the prometheus query language
metricsQuery: "sum(rate(<<.Series>>{<<.LabelMatchers>>}[1m])) by (<<.GroupBy>>)"
HPA YAML
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: translate-deployment-v2-hpa
spec:
minReplicas: 1
maxReplicas: 10
metrics:
- type: Pods
pods:
metric:
name: istio_requests_per_second
# selector: {matchLabels: {destination_version: 0.0.2}}
target:
type: AverageValue
averageValue: 10
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: translate-deployment-v2
看到 HPA 拉取并测量指标但没有缩放,它下面的 window 显示 prometheus-adapter 成功查询指标。
HPA 说明
我不清楚的最后一项是 selector 在上面的 hpa 定义中的目的是什么?是select普罗米修斯查询的数据范围内的特定值吗?
例如我知道我要查询的字段默认由envoy输出如下:
istio_requests_total{app="istio-ingressgateway",chart="gateways",connection_security_policy="unknown",destination_app="translate-pod",destination_canonical_revision="0.0.1",destination_canonical_service="translate-pod",destination_principal="spiffe://cluster.local/ns/default/sa/default",destination_service="translate-service.default.svc.cluster.local",destination_service_name="translate-service",destination_service_namespace="default",destination_version="0.0.1",destination_workload="translate-deployment",destination_workload_namespace="default",heritage="Tiller",install_operator_istio_io_owning_resource="unknown",instance="172.17.0.5:15020",istio="ingressgateway",istio_io_rev="default",job="kubernetes-pods",kubernetes_namespace="istio-system",kubernetes_pod_name="istio-ingressgateway-6cfd75fc57-flmhp",operator_istio_io_component="IngressGateways",pod_template_hash="6cfd75fc57",release="istio",reporter="source",request_protocol="http",response_code="200",response_flags="-",service_istio_io_canonical_name="istio-ingressgateway",service_istio_io_canonical_revision="latest",source_app="istio-ingressgateway",source_canonical_revision="latest",source_canonical_service="istio-ingressgateway",source_principal="spiffe://cluster.local/ns/istio-system/sa/istio-ingressgateway-service-account",source_version="unknown",source_workload="istio-ingressgateway",source_workload_namespace="istio-system"}
select或是否允许您进一步过滤系列数据,如果不是,目的是什么,您如何使用它?
根据您的屏幕截图,HPA 按预期工作,因为您的指标值低于阈值。如果该值没有超过您的阈值,HPA 将不会触发放大。相反,它可能会触发您的情况下的缩减。
您现在使用的指标是 istio_requests_per_second
。这是按每秒总请求数计算的。第一个屏幕截图显示平均值是 200m
,也就是 0.2
。您的阈值是 10
,因此在这种情况下 HPA 肯定不会扩大。
对于 selector,它使您能够 select 指标下的目标标签。例如,如果您只想根据 GET
方法缩放实例。在这种情况下,您可以定义 select 或匹配 GET
方法标签。
我有两个部署 运行 Istio 中同一服务的 v1 和 v2。我已经设置了一个自定义指标 'istio-total-requests' 以通过 prometheus 适配器收集。
我已经设置了一个 HPA 来扩展 v2 部署,当我发送请求时可以看到目标值在增加,但是没有发生的是 HPA 没有扩展 pods/replicas 的数量。
我是 运行 minikube v1.13.1 上的 kubernetes v1.19,无法理解为什么它不能扩展。
prometheus:
url: http://prometheus.istio-system.svc.cluster.local
rules:
default: false
custom:
# this rule matches cumulative cAdvisor metrics measured in seconds
- seriesQuery: 'istio_requests_total{kubernetes_namespace!="",kubernetes_pod_name!=""}'
seriesFilters: []
resources:
# template: <<.Resource>>
# skip specifying generic resource<->label mappings, and just
# attach only pod and namespace resources by mapping label names to group-resources
overrides:
kubernetes_namespace: {resource: "namespace"}
kubernetes_pod_name: {resource: "pod"}
# specify that the `container_` and `_seconds_total` suffixes should be removed.
# this also introduces an implicit filter on metric family names
name:
# we use the value of the capture group implicitly as the API name
# we could also explicitly write `as: ""`
matches: "^(.*)_total"
as: "_per_second"
# matches: ""
# as: ""
# specify how to construct a query to fetch samples for a given series
# This is a Go template where the `.Series` and `.LabelMatchers` string values
# are available, and the delimiters are `<<` and `>>` to avoid conflicts with
# the prometheus query language
metricsQuery: "sum(rate(<<.Series>>{<<.LabelMatchers>>}[1m])) by (<<.GroupBy>>)"
HPA YAML
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: translate-deployment-v2-hpa
spec:
minReplicas: 1
maxReplicas: 10
metrics:
- type: Pods
pods:
metric:
name: istio_requests_per_second
# selector: {matchLabels: {destination_version: 0.0.2}}
target:
type: AverageValue
averageValue: 10
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: translate-deployment-v2
看到 HPA 拉取并测量指标但没有缩放,它下面的 window 显示 prometheus-adapter 成功查询指标。
HPA 说明
我不清楚的最后一项是 selector 在上面的 hpa 定义中的目的是什么?是select普罗米修斯查询的数据范围内的特定值吗?
例如我知道我要查询的字段默认由envoy输出如下:
istio_requests_total{app="istio-ingressgateway",chart="gateways",connection_security_policy="unknown",destination_app="translate-pod",destination_canonical_revision="0.0.1",destination_canonical_service="translate-pod",destination_principal="spiffe://cluster.local/ns/default/sa/default",destination_service="translate-service.default.svc.cluster.local",destination_service_name="translate-service",destination_service_namespace="default",destination_version="0.0.1",destination_workload="translate-deployment",destination_workload_namespace="default",heritage="Tiller",install_operator_istio_io_owning_resource="unknown",instance="172.17.0.5:15020",istio="ingressgateway",istio_io_rev="default",job="kubernetes-pods",kubernetes_namespace="istio-system",kubernetes_pod_name="istio-ingressgateway-6cfd75fc57-flmhp",operator_istio_io_component="IngressGateways",pod_template_hash="6cfd75fc57",release="istio",reporter="source",request_protocol="http",response_code="200",response_flags="-",service_istio_io_canonical_name="istio-ingressgateway",service_istio_io_canonical_revision="latest",source_app="istio-ingressgateway",source_canonical_revision="latest",source_canonical_service="istio-ingressgateway",source_principal="spiffe://cluster.local/ns/istio-system/sa/istio-ingressgateway-service-account",source_version="unknown",source_workload="istio-ingressgateway",source_workload_namespace="istio-system"}
select或是否允许您进一步过滤系列数据,如果不是,目的是什么,您如何使用它?
根据您的屏幕截图,HPA 按预期工作,因为您的指标值低于阈值。如果该值没有超过您的阈值,HPA 将不会触发放大。相反,它可能会触发您的情况下的缩减。
您现在使用的指标是 istio_requests_per_second
。这是按每秒总请求数计算的。第一个屏幕截图显示平均值是 200m
,也就是 0.2
。您的阈值是 10
,因此在这种情况下 HPA 肯定不会扩大。
对于 selector,它使您能够 select 指标下的目标标签。例如,如果您只想根据 GET
方法缩放实例。在这种情况下,您可以定义 select 或匹配 GET
方法标签。