从逻辑意义上讲,水平 pod 自动扩缩器是一个控制循环,用于检查其指定目标(例如部署)以查看是否已达到指标阈值。 (这实际上是通过控制器管理器和指标 API 发生的)。 HPA 是它自己的独立资源,它通过称为Scale subresource 的东西与部署进行交互。这是一种抽象,可充当 Kubernetes 可能希望自动扩展的任何未来资源(不仅仅是部署)的接口。
Horizontal Pod Autoscaler 的规范(取自 k8s 设计文档):
type HorizontalPodAutoscalerSpec struct {
// reference to Scale subresource; horizontal pod autoscaler will learn the current resource
// consumption from its status,and will set the desired number of pods by modifying its spec.
ScaleRef SubresourceReference
// lower limit for the number of pods that can be set by the autoscaler, default 1.
MinReplicas *int
// upper limit for the number of pods that can be set by the autoscaler.
// It cannot be smaller than MinReplicas.
MaxReplicas int
// target average CPU utilization (represented as a percentage of requested CPU) over all the pods;
// if not specified it defaults to the target CPU utilization at 80% of the requested resources.
CPUUtilization *CPUTargetUtilization
}
请注意,HPA 使用 scale ref 来定位所需资源,而 Deployment 通过其选择器包含多个资源。出于灵活性原因,HPA 与特定部署分离。这意味着当您删除 Deployment 时,k8s 可以删除它通过其选择器管理的所有内容。 HPA 不由 Deployment 管理,而仅通过其自己的规范连接到它。 HPA 可以保留,等待新部署取代原来的位置,可以重新配置,也可以删除。