kubernetes 提供了一些功能来解决这些问题,主要包括 pod 水平扩容(HorizontalPodAutoscaler)、pod 垂直扩容(VerticalPodAutoscaler) 以及集群扩容 (ClusterAutoscaler) pod 水平扩容(HorizontalPodAutoscaler) HorizontalPodAutoscaler 实现了 pod 的水平伸缩,即副本数的伸缩,可以用于根据某种指标扩容的场景 HorizontalPodAutoscaler 的工作原理可以简述为下图: [hpa] HorizontalPodAutoscaler 作为一个 controller 运行于 kube-controller-manager ,计算副本数的预期值,如果当前 pod 的数量和副本数不一致,则 HorizontalPodAutoscaler 会对副本数进行调整。 当实际使用量小于期望使用量时,则 HorizontalPodAutoscaler 会减少 pod 的副本数。
HorizontalPodAutoscaler清单 HorizontalPodAutoscaler是用来设置动态扩缩容条件的组件。 apiVersion: autoscaling/v1 kind: HorizontalPodAutoscaler metadata: name: simple-http-hpa spec: scaleTargetRef simple_http_deployment.yaml deployment.apps/simple-http-deployment created service/simple-http-service created horizontalpodautoscaler.autoscaling /simple-http-hpa created 测试 查看HorizontalPodAutoscaler副本数量 kubectl get horizontalpodautoscalers.autoscaling
源码目录结构分析 HorizontalPodAutoscaler(以下简称HPA)的主要代码如下,主要涉及的文件不多。 hpaNamespacer是HorizontalPodAutoscalerInterface,包括HorizontalPodAutoscaler的Create, Update, UpdateStatus 上面代码中,将HPA resource的ListWatch Func注册为HorizontalPodAutoscaler Interface定义的List和Watch接口。 等等,说了这么多,怎么还没看到HorizontalPodAutoscaler struct的定义呢! 好吧,下面就来看看,正好HorizontalPodAutoscaler Interface中出现了。
如果在HorizontalPodAutoscaler中设置了多个指标,系统就会对每个指标都执行上面的算法,在全部结果中以期望副本数的最大值为最终结果。 HorizontalPodAutoscaler配置详解 Kubernetes将HorizontalPodAutoscaler资源对象提供给用户来定义扩缩容的规则。 下面对HorizontalPodAutoscaler的配置和用法进行说明。 (1)基于autoscaling/v1版本的HorizontalPodAutoscaler配置,仅可以设置CPU使用率: apiVersion: autoscaling/v1 kind: HorizontalPodAutoscaler (2)基于autoscaling/v2beta2的HorizontalPodAutoscaler配置: apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler
3、Merics Server 应用 Metrics Server 抽象了 HorizontalPodAutoscaler 资源对象,通过观察 Pod CPU、内存或者其它自定义指标自动缩放 Controller 、Deployment、StatefulSet中的 POd 数量,注意 HorizontalPodAutoscaler 不支持无法缩放的对象,比如 DaemonSet。 创建 HorizontalPodAutoscaler 对象,默认最小副本数为 1,cpu 或者 内存达到定义的限制开始扩容,最大副本数量为 4 apiVersion: autoscaling/v2beta1 kind: HorizontalPodAutoscaler metadata: name: cck-deployment spec: scaleTargetRef: apiVersion 如上所示讲述了 HorizontalPodAutoscaler 对 Deployment 资源对象的扩容过程。
本文将深入剖析三种核心扩缩容方案:HorizontalPodAutoscaler(HPA)、HorizontalPodCronscaler和EffectiveHorizontalPodAutoscaler 自动扩缩容的核心价值 资源利用率优化:避免过度配置造成的资源浪费 服务质量保障:突发流量时自动扩容保证服务稳定性 成本控制:低负载时自动缩容减少云资源支出 运维自动化:减少人工干预,提高系统自治能力 二、HorizontalPodAutoscaler Adapter:提供自定义指标 HPA Controller:决策中枢 2.2 HPA配置详解 以下是一个典型的HPA YAML定义: apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler Java集成示例 使用Fabric8 Kubernetes Client与HPA交互: import io.fabric8.kubernetes.api.model.autoscaling.v2beta2.HorizontalPodAutoscaler createOrUpdateHpa() { try (KubernetesClient client = new DefaultKubernetesClient()) { HorizontalPodAutoscaler
如果在HorizontalPodAutoscaler中设置了多个指标,系统就会对每个指标都执行上面的算法,在全部结果中以期望副本数的最大值为最终结果。 1.3 HorizontalPodAutoscaler Kubernetes将HorizontalPodAutoscaler资源对象提供给用户来定义扩缩容的规则。 HorizontalPodAutoscaler资源对象处于Kubernetes的API组“autoscaling”中, 目前包括v1和v2两个版本。 示例1:基于autoscaling/v1版本的HorizontalPodAutoscaler配置,仅可以设置CPU使用率。 示例2:基于autoscaling/v2beta2的HorizontalPodAutoscaler配置。
web --min=2 --max=10 -o yaml --dry-run=client > hpa-web.yaml 内容: apiVersion: autoscaling/v1 kind: HorizontalPodAutoscaler autoscale 模板 在 scaleTargetRef 后,增加 targetCPUUtilizationPercentage: 40 apiVersion: autoscaling/v1 kind: HorizontalPodAutoscaler currentReplicas: 0 desiredReplicas: 0 3.6 创建 hpa autoscale flamingskys$ kubectl create -f hpa-web.yaml horizontalpodautoscaler.autoscaling REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE horizontalpodautoscaler.autoscaling
Kubernetes中的自动扩展器有HorizontalPodAutoscaler(HPA)和VerticalPodAutoscaler(VPA)。 设置自动扩展策略以应对不同的负载需求为了设置自动扩展策略以应对不同的负载需求,可以按照以下步骤进行操作:创建HPA或VPA对象: 首先需要创建一个HorizontalPodAutoscaler(HPA)
HPA 实现的方式有两种:配置文件和命令行 配置文件 这种方式是通过定义 yaml 配置文件来创建 HPA,如下是基本定义: apiVersion: autoscaling/v1 kind: HorizontalPodAutoscaler minReplicas: 1 # (2) maxReplicas: 10 targetAverageUtilization: 50 # (3) 文件 kind 类型是 HorizontalPodAutoscaler
Pod副本的增加或者减少,但是在实际环境我们关键业务可能存在意想不到突发流量,如果我们手工配置把每个业务都配置更多的副本数量,对我们的资源控制和调度可能会产生影响,kubernetes给我们提供了要给HorizontalPodAutoscaler 3.创建内存HPA apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: memory-autoscaler
# 示例代码(HorizontalPodAutoscaler定义) apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler metadata
内部缩放有两种主要类型:VerticalPodAutoscaler 和 HorizontalPodAutoscaler。 HorizontalPodAutoscaler 或 HPA HorizontalPodAutoscaler就像VerticalPodAutoscaler一样,涉及在集群上安装并配置它。 当为部署配置HorizontalPodAutoscaler时,它会在pod 的限制中配置内存和 CPU 耗尽时增加内存和 CPU 。它增加了内存/CPU 以确保 pod 不会变得不稳定。 要安装HorizontalPodAutoscaler,您必须首先安装metrics-server。 apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler metadata: name: nginx-hpa spec: scaleTargetRef
HorizontalPodAutoscaler k8s提供HorizontalPodAutoscaler资源对象,让我们可以使用它进行配置扩缩容的规则。 HorizontalPodAutoscaler有两个版本,autoscaling/v1 只支持CPU使用率的指标数,autoscaling/v2则用于支持基于任意指标的自动扩缩容配置,包括基于资源使用率 apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler metadata: name: php-apache namespace
如果我们不知道怎么编写的话,可以查看上面命令行创建的HPA的YAML文件: $ kubectl get hpa hpa-demo -o yaml apiVersion: autoscaling/v1 kind: HorizontalPodAutoscaler 基于内存 HorizontalPodAutoscaler 是 Kubernetes autoscaling API 组的资源,在当前稳定版本 autoscaling/v1 中只支持基于 CPU 指标的缩放 Running 0 35s 然后需要创建一个基于内存的 HPA 资源对象:(hpa-mem.yaml) apiVersion: autoscaling/v2beta1 kind: HorizontalPodAutoscaler {"apiVersion":"autoscaling/v2beta1","kind":"HorizontalPodAutoscaler 直接创建上面的资源对象: $ kubectl apply -f hpa-prome.yaml horizontalpodautoscaler.autoscaling/nginx-custom-hpa created
deployment.extensions/istio-tracing created gateway.networking.istio.io/istio-autogenerated-k8s-ingress created horizontalpodautoscaler.autoscaling /istio-egressgateway created horizontalpodautoscaler.autoscaling/istio-ingressgateway created horizontalpodautoscaler.autoscaling /istio-policy created horizontalpodautoscaler.autoscaling/istio-telemetry created horizontalpodautoscaler.autoscaling
HorizontalPodAutoscaler, k8s的版本是1.14. List-2 apiVersion: autoscaling/v1 kind: HorizontalPodAutoscaler metadata: name: {{ template "consumer.fullname
如果 HorizontalPodAutoscaler 指定的是 targetAverageValue 或 targetAverageUtilization, 那么将会把指定 Pod 度量值的平均值做为 注意,平均利用率的原始值会通过 HorizontalPodAutoscaler 的状态体现( 即使使用了新的使用率,也不考虑未就绪 Pod 和 缺少指标的 Pod)。 如果创建 HorizontalPodAutoscaler 时指定了多个指标, 那么会按照每个指标分别计算扩缩副本数,取最大值进行扩缩。 0 31s #创建HPA $ kubectl autoscale deployment hpa-demo --cpu-percent=40 --min=1 --max=3 horizontalpodautoscaler.autoscaling /hpa-demo autoscaled #查看HPA $ kubectl get hpa hpa-demo -o yaml apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler
podinfo # HPA reference (optional) autoscalerRef: apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler 几秒钟后,Flager 将创建 canary 对象: # applied deployment.apps/podinfo horizontalpodautoscaler.autoscaling/podinfo ingresses.extensions/podinfo canary.flagger.app/podinfo # generated deployment.apps/podinfo-primary horizontalpodautoscaler.autoscaling kind: Deployment name: podinfo autoscalerRef: apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler
每个周期内,控制器管理器根据每个 HorizontalPodAutoscaler 定义中指定的指标查询资源利用率。 targetPort: 80 protocol: TCP 编写HPA,用于水平扩展,当cpu达到50%的利用率的时候开始扩展: apiVersion: autoscaling/v1 kind: HorizontalPodAutoscaler reconcileAutoscaler:计算副本数 func (a *HorizontalController) reconcileAutoscaler(hpav1Shared *autoscalingv1.HorizontalPodAutoscaler metric, statuses, timestamp, nil } 因为我们在设置metrics的时候实际上是一个数组,如下: apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler func (a *HorizontalController) normalizeDesiredReplicasWithBehaviors(hpa *autoscalingv2.HorizontalPodAutoscaler