kubernetes-水平Pod自动伸缩

简介: 在当前的版本中,Pod水平自动伸缩支持基于CPU使用情况对Pod进行自动伸缩。Pod水平自动伸缩通过Kubernetes API资源和控制器进行实现。控制器会根据CPU的使用情况周期性的调整Pod的副本数量。

在当前的版本中,Pod水平自动伸缩支持基于CPU使用情况对Pod进行自动伸缩。Pod水平自动伸缩通过Kubernetes API资源和控制器进行实现。控制器会根据CPU的使用情况周期性的调整Pod的副本数量。

1、水平Pod自动伸缩是如何工作的?


Horizontal Pod Autoscaler diagram

水平Pod自动伸缩被实现为控制周期循环,其周期由控制器管理器的–horizontal-pod-autoscaler-sync-period标志进行控制(默认值为30秒)。在每个周期间,控制器管理器会根据每个Horizo​​ntalPodAutoscaler定义中指定的度量标准查询资源利用率。控制器管理器从资源指标的API(针对每个窗格的资源指标)或自定义指标的API(针对所有其他指标)获取度量。

  • 对于每个Pod资源指标(如CPU),控制器为HorizontalPodAutoscaler的目标Pod从资源指标API中获取指标。如果设置了目标利用率的值,则控制器将在会计算使用率,作为每个Pod中容器的等效资源请求的百分比。如果设置了目标原始值,则直接使用原始指标标准值。然后,控制器在所有目标Pod中获取利用率的平均值或原始值(取决于指定的目标类型),并产生用于缩放所需副本数量的比率。

需要注意的,如果某些Pod的容器没有设置相关的资源请求,则不会定义Pod的CPU利用率,并且autoscaler不会对该指标标准采取任何操作。

  • 对于每个Pod自定义的指标,与每个pod资源指标的工作机理类似;不同之处在于它使用初始值,而不是使用率值。
  • 对于对象指标和外部指标,将获取单个指标,该指标将描述相关对象。该指标将与目标值进行比较,以产生如上所述的比率。在autoscaling/v2beta2API版本中,可以选择在进行比较之前将该值除以pod的数量。

Horizo​​ntalPodAutoscaler通常从一系列的API聚集中获取指标(metrics.k8s.io, custom.metrics.k8s.io和external.metrics.k8s.io)。该metrics.k8s.io API通常由指标服务器提供,其需要单独启动。自动缩放器使用伸缩子资源访问相应的可伸缩控制器(例如复制控制器,部署和副本集)。Scale是一个接口,允许您动态设置副本数并检查每个当前状态。

2、算法

从最基本的角度来看,Horizo​​ntalPodAutoscaler控制器根据所需指标与当前指标之间的比率进行操作:

desiredReplicas = ceil[currentReplicas * ( currentMetricValue / desiredMetricValue )]

例如,如果当前的指标值为200m,而期望的指标值为100m,则Pod的副本数量将会翻倍,即200/100=2;如果当前的指标值为50m,则会将Pod的副本数减半。如果比率足够接近1.0(在全局可配置的容差范围内,从–horizontal-pod-autoscaler-tolerance标志,默认为0.1),将会忽略缩放。当指定targetAverageValuetargetAverageUtilization时,currentMetricValue会通过所有Pod(Horizo​​ntalPodAutoscaler伸缩目标)获取给定指标的平均值来计算。当基于CPU进行伸缩时,如果任意Pod还未准备就绪,则此Pod放弃。currentMetricValue / desiredMetricValue伸缩比率基于保留的Pod进行计算,而不是放弃的Pod进行计算。如果在HorizontalPodAutoscaler中指定了多个指标,则会计算每一个指标,并选择期望副本数最大的指标。

3、在kubectl中使用水平Pod自动伸缩

与所有API资源一样,kubectl以标准方式支持水平Pod自动伸缩。可以使用kubectl create命令创建一个新的自动缩放器。通过kubectl get hpa可以列出自动伸缩器,通过kubectl describe hpa能够获得自动伸缩器的详细说明。最后,可以使用kubectl delete hpa删除自动缩放器。此外,还有一个特殊kubectl autoscale命令可以轻松创建Horizo​​ntalPodAutoscaler

1)创建autoscale

$ kubectl autoscale deployment netdisk --min=2 --max=5 --cpu-percent=80 

为部署netdisk创建一个自动伸缩器,目标CPU利用率设置为80%,副本数量介于2和5之间。

2)获取autoscale

获取default命名空间下的hpa

$ kubectl get hpa

3)查看autoscale

查看netdisk hpa的详细信息。

$ kubectl describe hpa netdisk

4)删除autoscale

$ kubectl delete hpa netdisk

4、kubectl autoscale命令

kubectl autoscale命令的使用方法

$ autoscale (-f FILENAME | TYPE NAME | TYPE/NAME) [--min=MINPODS] --max=MAXPODS [--cpu-percent=CPU]

字段说明

名称 Shorthand 默认值 使用说明
allow-missing-template-keys true 如果值为真,则忽略模板中的任何错误。仅仅能够应用到golang和jsonpath输出格式。
cpu-percent -1 The target average CPU utilization (represented as a percent of requested CPU) over all the pods. If it’s not specified or negative, a default autoscaling policy will be used.
dry-run false If true, only print the object that would be sent, without sending it.
filename f [] Filename, directory, or URL to files identifying the resource to autoscale.
generator horizontalpodautoscaler/v1 The name of the API generator to use. Currently there is only 1 generator.
max -1 The upper limit for the number of pods that can be set by the autoscaler. Required.
min -1 The lower limit for the number of pods that can be set by the autoscaler. If it’s not specified or negative, the server will apply a default value.
name The name for the newly created object. If not specified, the name of the input resource will be used.
output o Output format. One of: json|yaml|name|template|go-template|go-template-file|templatefile|jsonpath|jsonpath-file.
record false Record current kubectl command in the resource annotation. If set to false, do not record the command. If set to true, record the command. If not set, default to updating the existing annotation value only if one already exists.
recursive R false Process the directory used in -f, –filename recursively. Useful when you want to manage related manifests organized within the same directory.
save-config false If true, the configuration of current object will be saved in its annotation. Otherwise, the annotation will be unchanged. This flag is useful when you want to perform kubectl apply on this object in the future.
template Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].

参考材料

1.《Horizo​​ntal Pod Autoscaler》地址:https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/

本文转自中文社区-kubernetes-水平Pod自动伸缩

相关实践学习
容器服务Serverless版ACK Serverless 快速入门:在线魔方应用部署和监控
通过本实验,您将了解到容器服务Serverless版ACK Serverless 的基本产品能力,即可以实现快速部署一个在线魔方应用,并借助阿里云容器服务成熟的产品生态,实现在线应用的企业级监控,提升应用稳定性。
云原生实践公开课
课程大纲 开篇:如何学习并实践云原生技术 基础篇: 5 步上手 Kubernetes 进阶篇:生产环境下的 K8s 实践 相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
相关文章
|
21天前
|
前端开发 编解码 数据格式
浅谈响应式编程在企业级前端应用 UI 开发中的实践
浅谈响应式编程在企业级前端应用 UI 开发中的实践
20 0
浅谈响应式编程在企业级前端应用 UI 开发中的实践
|
1月前
|
Prometheus Kubernetes 监控
容器服务ACK常见问题之pod设置securityContext调整参数失败如何解决
容器服务ACK(阿里云容器服务 Kubernetes 版)是阿里云提供的一种托管式Kubernetes服务,帮助用户轻松使用Kubernetes进行应用部署、管理和扩展。本汇总收集了容器服务ACK使用中的常见问题及答案,包括集群管理、应用部署、服务访问、网络配置、存储使用、安全保障等方面,旨在帮助用户快速解决使用过程中遇到的难题,提升容器管理和运维效率。
|
1天前
|
Kubernetes 监控 Cloud Native
Kubernetes自动伸缩方案的终极指南
【4月更文挑战第18天】
13 0
Kubernetes自动伸缩方案的终极指南
|
12天前
|
存储 Kubernetes 调度
Kubernetes Pod生命周期
Kubernetes Pod生命周期
19 0
Kubernetes Pod生命周期
|
12天前
|
存储 Kubernetes 应用服务中间件
Kubernetes Pod
Kubernetes Pod
42 0
Kubernetes Pod
|
23天前
|
存储 Kubernetes 调度
K8s Pod亲和性、污点、容忍度、生命周期与健康探测详解(下)
本文全面探讨了Kubernetes集群中Pod的四种关键机制——Pod亲和性、污点(Taints)、容忍度(Tolerations)、生命周期以及健康探测,为读者提供了深入理解并有效应用这些特性的指南。
|
23天前
|
Kubernetes 网络协议 Perl
k8s Failed to create pod sandbox: open /run/systemd/resolve/resolv.conf: no such file or directory
k8s Failed to create pod sandbox: open /run/systemd/resolve/resolv.conf: no such file or directory
22 0
|
1月前
|
Kubernetes Nacos 微服务
nacos常见问题之v2.2.3 k8s 微服务注册nacos强制删除 pod不消失如何解决
Nacos是阿里云开源的服务发现和配置管理平台,用于构建动态微服务应用架构;本汇总针对Nacos在实际应用中用户常遇到的问题进行了归纳和解答,旨在帮助开发者和运维人员高效解决使用Nacos时的各类疑难杂症。
24 1
nacos常见问题之v2.2.3 k8s 微服务注册nacos强制删除 pod不消失如何解决
|
1月前
|
域名解析 Kubernetes Linux
Kubernetes 外部 HTTP 请求到达 Pod 容器的全过程
Kubernetes 外部 HTTP 请求到达 Pod 容器的全过程
39 4
|
2月前
|
Kubernetes API Python

推荐镜像

更多