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

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

Horizontal Pod Autoscaler(HPA)简明指南

1. Horizontal Pod Autoscaler 是如何工作的

Horizontal Pod Autoscaler 是 Kubernetes 自动缩放机制的一部分,用于根据资源使用情况自动调整Pod数量。它监控指定指标(如CPU使用率),并根据目标值自动扩展或缩减副本数量。

HPA 的控制循环周期由 --horizontal-pod-autoscaler-sync-period 参数决定,默认为15秒。每个周期内,Controller Manager会查询资源指标API或自定义指标API,获取Pod的资源使用情况。

对于Pod资源指标(如CPU),HPA会从资源指标API获取数据。如果设置了目标利用率值,HPA会将其转换为资源请求百分比;如果设置了目标原始值,则直接使用原始指标值。HPA计算所有目标Pod的平均值,并根据比率调整副本数量。

需要注意的是,如果某些Pod的容器未设置资源请求,HPA不会考虑其CPU使用率。

2. 算法细节

HPA的算法基于以下公式计算期望副本数量:

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

直译为:当前副本数乘以(当前指标值 / 期望指标值),结果向上取整即为期望副本数量。

示例:

  • 当前指标值为200m,期望指标值为100m时,期望副本数量为双倍。
  • 当前指标值为50m,期望指标值为100m时,期望副本数量为当前副本数的一半。

如果比率接近1,则HPA不会进行缩放。

当基于CPU利用率缩放时,尚未准备好的Pod会被暂时保留。

3. 使用示例

1. 创建镜像并运行示例

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

2. 运行镜像并暴露为服务

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

3. 创建HPA并设置目标

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

4. 检查HPA状态

kubectl get hpa

5. 增加负载并验证

kubectl get deployment hello-world-example

4. 定义自定义指标

HPA支持自定义指标,可以通过以下方式定义:

1. Pod指标

metrics:- type: Pods  pods:    metric:      name: packets-per-second    target:      type: AverageValue      averageValue: 1k

2. 对象指标

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

完整示例:

apiVersion: autoscaling/v2beta2kind: HorizontalPodAutoscalermetadata:  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: 10k

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

你可能感兴趣的文章
NIH发布包含10600张CT图像数据库 为AI算法测试铺路
查看>>
Nim教程【十二】
查看>>
Nim游戏
查看>>
NIO ByteBuffer实现原理
查看>>
Nio ByteBuffer组件读写指针切换原理与常用方法
查看>>
NIO Selector实现原理
查看>>
nio 中channel和buffer的基本使用
查看>>
NIO基于UDP协议的网络编程
查看>>
NISP一级,NISP二级报考说明,零基础入门到精通,收藏这篇就够了
查看>>
Nitrux 3.8 发布!性能全面提升,带来非凡体验
查看>>
NI笔试——大数加法
查看>>
NLog 自定义字段 写入 oracle
查看>>
NLog类库使用探索——详解配置
查看>>
NLP 基于kashgari和BERT实现中文命名实体识别(NER)
查看>>
NLP 项目:维基百科文章爬虫和分类【01】 - 语料库阅读器
查看>>
NLP_什么是统计语言模型_条件概率的链式法则_n元统计语言模型_马尔科夫链_数据稀疏(出现了词库中没有的词)_统计语言模型的平滑策略---人工智能工作笔记0035
查看>>
NLP学习笔记:使用 Python 进行NLTK
查看>>
NLP的神经网络训练的新模式
查看>>
NLP采用Bert进行简单文本情感分类
查看>>
NLP问答系统:使用 Deepset SQUAD 和 SQuAD v2 度量评估
查看>>