博客
关于我
Horizontal Pod Autoscaler(Pod水平自动伸缩)
阅读量:418 次
发布时间:2019-03-06

本文共 4156 字,大约阅读时间需要 13 分钟。

Horizontal Pod Autoscaler 根据观察到的CPU利用率(或在支持自定义指标的情况下,根据其他一些应用程序提供的指标)自动伸缩 replication controller, deployment, replica set, stateful set 中的pod数量。注意,Horizontal Pod Autoscaling不适用于无法伸缩的对象,例如DaemonSets。

Horizontal Pod Autoscaler 被实现作为Kubernetes API资源和控制器。该资源决定控制器的行为。控制器会定期调整副本控制器或部署中副本的数量,以使观察到的平均CPU利用率与用户指定的目标相匹配。

1. Horizontal Pod Autoscaler 是如何工作的

Horizontal Pod Autoscaler 实现为一个控制循环,其周期由--horizontal-pod-autoscaler-sync-period选项指定(默认15秒)。

在每个周期内,controller manager都会根据每个HorizontalPodAutoscaler定义的指定的指标去查询资源利用率。 controller manager从资源指标API(针对每个pod资源指标)或自定义指标API(针对所有其他指标)获取指标。

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

如果某些Pod的容器未设置相关资源请求,则不会定义Pod的CPU使用率,并且自动缩放器不会对该指标采取任何措施。

2. 算法细节

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

直译为:(当前指标值 ➗ 期望指标值) ✖️ 当前副本数 ,结果再向上取整,最终结果就是期望的副本数量

例如,假设当前指标值是200m ,期望指标值是100m,期望的副本数量就是双倍。因为,200.0 / 100.0 == 2.0 

如果当前值是50m,则根据50.0 / 100.0 == 0.5,那么最终的副本数量就是当前副本数量的一半

如果该比率足够接近1.0,则会跳过伸缩

当targetAverageValue或者targetAverageUtilization被指定的时候,currentMetricValue取HorizontalPodAutoscaler伸缩目标中所有Pod的给定指标的平均值。

所有失败的和标记删除的Pod将被丢弃,即不参与指标计算

当基于CPU利用率来进行伸缩时,如果有尚未准备好的Pod(即它仍在初始化),那么该Pod将被放置到一边,即将被保留。

kubectl 也支持Horizontal Pod Autoscaler

# 查看autoscalers列表kubectl get hpa# 查看具体描述kubectl describe hpa# 删除autoscalerkubectl delete hpa# 示例:以下命名将会为副本集foo创建一个autoscaler,并设置目标CPU利用率为80%,副本数在2~5之间kubectl autoscale rs foo --min=2 --max=5 --cpu-percent=80

3. 演示

Horizontal Pod Autoscaler automatically scales the number of pods in a replication controller, deployment, replica set or stateful set based on observed CPU utilization.

创建Dockerfile,并构建镜像

FROM java:8COPY ./hello-world-0.0.1-SNAPSHOT.jar hello-world.jar CMD java -jar hello-world.jar 

在hello-world.jar中执行一些CPU密集型计算

运行镜像并暴露为服务

kubectl run hello-world-example \     --image=registry.cn-hangzhou.aliyuncs.com/chengjs/hello-world:2.0 \     --requests='cpu=200m' \     --limits='cpu=500m' \     --expose \     --port=80 \     --generator=run-pod/v1 

创建 Horizontal Pod Autoscaler

HPA将增加和减少副本数量,以将所有Pod的平均CPU利用率维持在50% 

kubectl autoscale deployment hello-world-example --cpu-percent=50 --min=1 --max=10 

检查autoscaler的当前状态

kubectl get hpa 

增加负载

接下来,利用压测工具持续请求,以增加负载,再查看

kubectl get deployment hello-world-example

通过使用autoscaling/v2beta2版本,你可以定义更多的指标 

首先,以autoscaling/v2beta2格式获取HorizontalPodAutoscaler的YAML

kubectl get hpa.v2beta2.autoscaling -o yaml > /tmp/hpa-v2.yaml

在编辑器中打开/tmp/hpa-v2.yaml文件,接下来对其进行修改

第一个可以替换的指标类型是Pod指标。这些指标在各个容器中平均在一起,并且和目标值进行比较,已确定副本数。例如:

type: Podspods:  metric:    name: packets-per-second  target:    type: AverageValue    averageValue: 1k 

第二个可以替换的指标类型是对象指标。顾名思义,它描述的是Object,而不是Pod。例如:

type: Objectobject:   metric:      name: requests-per-second   describedObject:      apiVersion: networking.k8s.io/v1beta1      kind: Ingress      name: main-route   target:      type: Value      value: 2k 

修改后完整的/tmp/hpa-v2.yaml文件如下:

apiVersion: autoscaling/v2beta2kind: HorizontalPodAutoscaler  metadata:    name:hello-world-example    namespace:defaultspec:  scaleTargetRef:    apiVersion: apps/v1    kind: Deployment    name: hello-world-example  minReplicas: 1  maxReplicas: 10  metrics:  - type: Resource    resource:      name: cpu      target:        type: Utilization        averageUtilization: 50  - type: Pods    pods:      metric:        name: packets-per-second      target:        type: AverageValue        averageValue: 1k  - type: Object    object:      metric:        name: requests-per-second      describedObject:        apiVersion: networking.k8s.io/v1beta1        kind: Ingress        name: main-route      target:        type: Value        value: 10kstatus:  observedGeneration: 1  lastScaleTime: 
currentReplicas: 1 desiredReplicas: 1 currentMetrics: - type: Resource resource: name: cpu current: averageUtilization: 0 averageValue: 0 - type: Object object: metric: name: requests-per-second describedObject: apiVersion: networking.k8s.io/v1beta1 kind: Ingress name: main-route current: value: 10k

4. Docs

 

转载地址:http://wmqkz.baihongyu.com/

你可能感兴趣的文章
mac mysql 进程_Mac平台下启动MySQL到完全终止MySQL----终端八步走
查看>>
Mac OS 12.0.1 如何安装柯美287打印机驱动,刷卡打印
查看>>
MangoDB4.0版本的安装与配置
查看>>
Manjaro 24.1 “Xahea” 发布!具有 KDE Plasma 6.1.5、GNOME 46 和最新的内核增强功能
查看>>
mapping文件目录生成修改
查看>>
MapReduce程序依赖的jar包
查看>>
mariadb multi-source replication(mariadb多主复制)
查看>>
MariaDB的简单使用
查看>>
MaterialForm对tab页进行隐藏
查看>>
Member var and Static var.
查看>>
memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
查看>>
memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
查看>>
Memcached:Node.js 高性能缓存解决方案
查看>>
memcache、redis原理对比
查看>>
memset初始化高维数组为-1/0
查看>>
Metasploit CGI网关接口渗透测试实战
查看>>
Metasploit Web服务器渗透测试实战
查看>>
MFC模态对话框和非模态对话框
查看>>
Moment.js常见用法总结
查看>>
MongoDB出现Error parsing command line: unrecognised option ‘--fork‘ 的解决方法
查看>>