k8s hpa中的指标来源及实现

指标类型

在HorizontalPodAutoscaler 中包含以下几种指标源类型:

  • Object(v2beta1)
  • Pods(v2beta1)
  • Resource(v2beta1)
  • External(v2beta2)

Object类型

Object类型是用于描述k8s内置对象的指标,例如ingress对象中hits-per-second指标

type ObjectMetricSource struct {
	// 用于描述k8s对象.
	Target CrossVersionObjectReference `json:"target" protobuf:"bytes,1,name=target"`
	// 所查询的metric名称.
	MetricName string `json:"metricName" protobuf:"bytes,2,name=metricName"`
	// 指标的目标值
	TargetValue resource.Quantity `json:"targetValue" protobuf:"bytes,3,name=targetValue"`
}

示例:

kind: HorizontalPodAutoscaler
apiVersion: autoscaling/v2alpha1
spec:
  scaleTargetRef:
    kind: ReplicationController
    name: WebFrontend
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Object
    object:
      target:
        kind: Service
        name: Frontend
      metricName: hits-per-second
      targetValue: 1k

Pods类型

pods类型描述当前扩容目标中每个pod的指标(例如,每秒处理的事务数)。在与目标值进行比较之前,将对值进行平均。

type PodsMetricSource struct {
	// 所查询的metric名称
	MetricName string `json:"metricName" protobuf:"bytes,1,name=metricName"`
	// 目标平均值
	TargetAverageValue resource.Quantity `json:"targetAverageValue" protobuf:"bytes,2,name=targetAverageValue"`
}

示例:

apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: podinfo
spec:
  scaleTargetRef:
    apiVersion: extensions/v1beta1
    kind: Deployment
    name: podinfo
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Pods
    pods:
      metricName: http_requests
      targetAverageValue: 10

Resource类型

Resource是Kubernetes已知的资源指标,如request和limit中所指定的,描述当前扩容目标的每个pod(例如CPU或内存)。该指标将会在与目标值对比前进行平均,被此类指标内置于Kubernetes中,且使用"pods"源在正常的每个pod度量标准之上提供特殊的扩展选项。

type ResourceMetricSource struct {
	// 所查询的metric名称
	Name v1.ResourceName `json:"name" protobuf:"bytes,1,name=name"`
	// 目标使用平均百分比
	tilization *int32 `json:"targetAverageUtilization,omitempty" protobuf:"varint,2,opt,name=targetAverageUtilization"`
	// 目标平均值
	TargetAverageValue *resource.Quantity `json:"targetAverageValue,omitempty" protobuf:"bytes,3,opt,name=targetAverageValue"`
}

示例:

kind: HorizontalPodAutoscaler
apiVersion: autoscaling/v2alpha1
spec:
  scaleTargetRef:
    kind: ReplicationController
    name: WebFrontend
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      targetAverageUtilization: 80

External 类型

ExternalMetricSource指示如何扩展与任何Kubernetes对象无关的指标(例如,云消息传递服务中的队列长度,或集群外部运行的负载均衡器的QPS)。

type ExternalMetricSource struct {
	// 度量通过名称和选择器标识目标度量。
	Metric MetricIdentifier `json:"metric" protobuf:"bytes,1,name=metric"`
	// target specifies the target value for the given metric
	// 为指定指标指定目标值
	Target MetricTarget `json:"target" protobuf:"bytes,2,name=target"`
}

示例:

kind: HorizontalPodAutoscaler
apiVersion: autoscaling/v2beta2
spec:
  scaleTargetRef:
    kind: ReplicationController
    name: Worker
  minReplicas: 2
  maxReplicas: 10
  metrics:
   - type: External
     external:
       metricName: queue_messages_ready
       metricSelector:
         matchLabels:
           queue: worker_tasks
       targetAverageValue: 30

指标来源

hpa controller可通过以下api来获取指标实现自动扩容:

metrics.k8s.io

用于hpa 中Resource类型数据来源.
已有实现:

custom.metrics.k8s.io

用于hpa 中object/pods类型的数据来源,需要自己实现适配器(custom metrics adapters)
已有实现:

external.metrics.k8s.io

用于 hpa 中external类型的数据来源,同样需要云厂商或平台自己实现适配器(custom metrics adapters)

已有实现:

参考:

欢迎加入QQ群:k8s开发与实践(482956822)一起交流k8s技术

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值