HPA控制器介绍以及实战案例
背景:
根据当前pod的负载,动态调整 pod副本数量,业务高峰期自动扩容pod的副本数以尽快响应pod的请求。
在业务低峰期对pod进行缩容,实现降本增效的目的。
动态伸缩控制器类型:
水平pod自动缩放器(HPA):基于pod 资源利用率横向调整pod副本数量。
垂直pod自动缩放器(VPA):基于pod资源利用率,调整对单个pod的最大资源限制,不能与HPA同时使用。
集群伸缩(Cluster Autoscaler,CA):基于集群中node 资源使用情况,动态伸缩node节点,从而保证有CPU和内存资源用于创建pod。
具体执行过程参照下图:
HPA控制器简介:
Horizontal Pod Autoscaling (HPA)控制器,根据预定义好的阈值及pod当前的资源利用率,自动控制在k8s集群中运行的pod数量(自动弹性水平自动伸缩).
指标说明:
horizontal-pod-autoscaler-sync-period #默认每隔15s(可以通过–horizontal-pod-autoscaler-sync-period修改)查询metrics的资源使用情况。
horizontal-pod-autoscaler-downscale-stabilization #缩容间隔周期,默认5分钟。
horizontal-pod-autoscaler-sync-period #HPA控制器同步pod副本数的间隔周期
horizontal-pod-autoscaler-cpu-initialization-period #初始化延迟时间,在此时间内 pod的CPU 资源指标将不会生效,默认为5分钟。
horizontal-pod-autoscaler-initial-readiness-delay #用于设置 pod 准备时间, 在此时间内的 pod 统统被认为未就绪及不采集数据,默认为30秒。
kube-controller-manager --help|grep horizontal-pod-autoscaler-downscale-stabilization #可以查看具体的默认值
注意:
horizontal-pod-autoscaler-tolerance #HPA控制器能容忍的数据差异(浮点数,默认为0.1),即新的指标要与当前的阈值差异在0.1或以上,即要大于1+0.1=1.1,或小于1-0.1=0.9,比如阈值为CPU利用率50%,当前为80%,那么80/50=1.6 > 1.1则会触发扩容,反之会缩容。即触发条件:avg(CurrentPodsConsumption) / Target >1.1 或 <0.9=把N个pod的数据相加后根据pod的数量计算出平均数除以阈值,大于1.1就扩容,小于0.9就缩容。
计算公式:TargetNumOfPods = ceil(sum(CurrentPodsCPUUtilization) / Target) #ceil是一个向上取整的目的pod整数。
指标数据需要部署metrics-server,即HPA使用metrics-server作为数据源。
部署metric-server
资源文件下载地址:
https://github.com/kubernetes-sigs/metrics-server
kubectl apply -f metrics-server.yaml #部署metric-server
kubectl top pod xxx -n ns #查看pod指标
kubectl top node xxx #查看node指标
实战案例【基于k8s1.24.2版本】:
tomcat.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: mytomcat
spec:replicas: 5selector:matchLabels:app: mytomcatminReadySeconds: 1progressDeadlineSeconds: 60revisionHistoryLimit: 5strategy:type: RollingUpdaterollingUpdate:maxSurge: 1maxUnavailable: 1template:metadata:name: mytomcatlabels:app: mytomcatspec:containers:- name: mytomcatimage: tomcat:8ports:- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:name: mytomcat
spec:#type: NodePortports:- port: 8080#nodePort: 30090selector:app: mytomcat
hap.yaml
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:namespace: demoname: demo-tomcat-podautoscalerlabels:app: demo-tomcat
spec:scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: mytomcat minReplicas: 2maxReplicas: 8targetCPUUtilizationPercentage: 60
root@ubuntu01:/app# kubectl get pod -n demo
NAME READY STATUS RESTARTS AGE
myserver-nginx-deployment-56f4ccb9bd-9kjlc 1/1 Running 7 (47m ago) 14d
mytomcat-689d96db6f-4t9lh 1/1 Running 0 8m29s
mytomcat-689d96db6f-c29sl 1/1 Running 0 8m30s
mytomcat-689d96db6f-dhhmm 1/1 Running 0 6s
mytomcat-689d96db6f-vsmph 1/1 Running 0 7m35s
mytomcat-689d96db6f-w6fwq 1/1 Running 0 7m35sroot@ubuntu01:/app# kubectl describe hpa demo-tomcat-podautoscaler -n demo
Warning: autoscaling/v2beta2 HorizontalPodAutoscaler is deprecated in v1.23+, unavailable in v1.26+; use autoscaling/v2 HorizontalPodAutoscaler
Name: demo-tomcat-podautoscaler
Namespace: demo
Labels: app=demo-tomcat
Annotations: <none>
CreationTimestamp: Fri, 19 Aug 2022 21:32:01 +0800
Reference: Deployment/mytomcat
Metrics: ( current / target )resource cpu on pods (as a percentage of request): 2% (2m) / 60%
Min replicas: 2
Max replicas: 8
Deployment pods: 2 current / 2 desired
Conditions:Type Status Reason Message---- ------ ------ -------AbleToScale True ScaleDownStabilized recent recommendations were higher than current one, applying the highest recent recommendationScalingActive True ValidMetricFound the HPA was able to successfully calculate a replica count from cpu resource utilization (percentage of request)ScalingLimited False DesiredWithinRange the desired count is within the acceptable range
Events:Type Reason Age From Message---- ------ ---- ---- -------Normal SuccessfulRescale 3m40s horizontal-pod-autoscaler New size: 4; reason: All metrics below targetNormal SuccessfulRescale 54s horizontal-pod-autoscaler New size: 2; reason: All metrics below target
从上面可以看出由最初的5个pod副本数降为hpa控制器最小的2个
将上面deployment资源文件镜像修改为:
image: lorel/docker-stress-ng
args: [“–vm”,“2”,“–vm-bytes”,“256M”]
apiVersion: apps/v1
kind: Deployment
metadata:name: mytomcat
spec:replicas: 2selector:matchLabels:app: mytomcatminReadySeconds: 1progressDeadlineSeconds: 60revisionHistoryLimit: 5strategy:type: RollingUpdaterollingUpdate:maxSurge: 1maxUnavailable: 1template:metadata:name: mytomcatlabels:app: mytomcatspec:containers:- name: mytomcatimage: lorel/docker-stress-ngargs: ["--vm","2","--vm-bytes","256M"]ports:- containerPort: 8080resources:requests:cpu: 100mmemory: 100Milimits:cpu: 100mmemory: 100Miroot@ubuntu01:/app# kubectl describe hpa demo-tomcat-podautoscaler -n demo
Warning: autoscaling/v2beta2 HorizontalPodAutoscaler is deprecated in v1.23+, unavailable in v1.26+; use autoscaling/v2 HorizontalPodAutoscaler
Name: demo-tomcat-podautoscaler
Namespace: demo
Labels: app=demo-tomcat
Annotations: <none>
CreationTimestamp: Fri, 19 Aug 2022 21:50:01 +0800
Reference: Deployment/mytomcat
Metrics: ( current / target )resource cpu on pods (as a percentage of request): 87% (87m) / 60%
Min replicas: 2
Max replicas: 8
Deployment pods: 7 current / 8 desired
Conditions:Type Status Reason Message---- ------ ------ -------AbleToScale True SucceededRescale the HPA controller was able to update the target scale to 8ScalingActive True ValidMetricFound the HPA was able to successfully calculate a replica count from cpu resource utilization (percentage of request)ScalingLimited True TooManyReplicas the desired replica count is more than the maximum replica count
Events:Type Reason Age From Message---- ------ ---- ---- -------Normal SuccessfulRescale 32m horizontal-pod-autoscaler New size: 4; reason: All metrics below targetNormal SuccessfulRescale 30m horizontal-pod-autoscaler New size: 2; reason: All metrics below targetWarning FailedGetScale 4m39s horizontal-pod-autoscaler deployments/scale.apps "mytomcat" not foundWarning FailedGetResourceMetric 4m9s (x2 over 4m24s) horizontal-pod-autoscaler failed to get cpu utilization: unable to get metrics for resource cpu: no metrics returned from resource metrics APIWarning FailedComputeMetricsReplicas 4m9s (x2 over 4m24s) horizontal-pod-autoscaler invalid metrics (1 invalid out of 1), first error is: failed to get cpu utilization: unable to get metrics for resource cpu: no metrics returned from resource metrics APIWarning FailedGetResourceMetric 3m54s horizontal-pod-autoscaler failed to get cpu utilization: did not receive metrics for any ready podsWarning FailedComputeMetricsReplicas 3m54s horizontal-pod-autoscaler invalid metrics (1 invalid out of 1), first error is: failed to get cpu utilization: did not receive metrics for any ready podsNormal SuccessfulRescale 2m54s horizontal-pod-autoscaler New size: 4; reason: cpu resource utilization (percentage of request) above targetNormal SuccessfulRescale 98s horizontal-pod-autoscaler New size: 5; reason: cpu resource utilization (percentage of request) above targetNormal SuccessfulRescale 83s horizontal-pod-autoscaler New size: 7; reason: cpu resource utilization (percentage of request) above targetNormal SuccessfulRescale 20s horizontal-pod-autoscaler New size: 8; reason: cpu resource utilization (percentage of request) above targetroot@ubuntu01:/app# kubectl get pod -n demo
NAME READY STATUS RESTARTS AGE
myserver-nginx-deployment-56f4ccb9bd-9kjlc 1/1 Running 7 (83m ago) 14d
mytomcat-76798bc994-2gxxf 1/1 Running 0 5m51s
mytomcat-76798bc994-2z5cm 1/1 Running 0 4m35s
mytomcat-76798bc994-5s2tf 1/1 Running 0 4m20s
mytomcat-76798bc994-dlbgf 1/1 Running 0 4m20s
mytomcat-76798bc994-dncl4 1/1 Running 0 5m51s
mytomcat-76798bc994-f5b5c 1/1 Running 0 7m36s
mytomcat-76798bc994-r59j7 1/1 Running 0 3m17s
mytomcat-76798bc994-rzl5w 1/1 Running 0 7m36s
从图中可以看出扩容到8个副本数后即使还是超过阀值,但由于hpa控制器最大pod副本数为8,故到8个后pod副本数不在进行扩容。
HPA控制器介绍以及实战案例相关推荐
- Python爬虫---爬虫介绍,实战案例
目录标题 1.爬虫介绍 1.1 爬虫的合法性 1.2 网络爬虫的尺寸 1.3 robots.txt协议 1.4 http&https协议 1.5 requests模块 1.5.1 reques ...
- 012:pyquery介绍与实战爬取糗事百科猫眼排行
很久没更新了.最近一直在使用pyquery做一些小爬虫文件.个人感觉是值得推荐的,本篇我来介绍下pq的用法及其实战.内容主要以代码为主. PyQuery库也是一个非常强大又灵活的网页解析库,如果你有前 ...
- ssm 异常捕获 统一处理_SpringMVC 统一异常处理介绍及实战
背景 什么是统一异常处理 目标 统一异常处理实战 用 Assert(断言) 替换 throw exception 定义统一异常处理器类 扩展 总结 <Java 2019 超神之路> < ...
- 【分布式事务】tcc-transaction分布式TCC型事务框架搭建与实战案例(基于Dubbo/Dubbox)...
一.背景 有一定分布式开发经验的朋友都知道,产品/项目/系统最初为了能够快速迭代上线,往往不太注重产品/项目/系统的高可靠性.高性能与高扩展性,采用单体应用和单实例数据库的架构方式快速迭代开发:当产品 ...
- Linux 运维自动化之Cobbler实战案例
大纲 一.前言 二.Cobbler 工作原理详解 三.Cobbler 常用命令汇总 四.Cobbler 各种目录说明 五.自定义Kickstart文件详解 六.Cobbler 实战案例安装CentOS ...
- java 502错误,Spring Boot连接超时导致502错误的实战案例
1.问题描述 内部系统之间通过Nginx来实现路由转发. 但最近发现有一个系统,经常报502错误,每天达到上百次,完全无法忍受. 2. 原因排查 于是进行排查, 发现配置人员把连接超时时间(serve ...
- 中小型局域网规划实战案例
中小型局域网规划实战案例 一.网络拓扑 二.拓扑介绍 三.IP地址说明 四.配置核心交换机SW2 1.配置vlan及ip 2.配置端口 3.检查配置 五.配置SW3 六.接入层配置 1.SW4配置 ① ...
- Python应用实战案例-Pythongeopandas包详解(附大量案例及代码)
前言 以下为博主为大家准备的人工智能&算法研究类精品专栏,喜欢的小伙伴自行下载. 深度学习100例全系列详细教程 深度学习算法原理介绍及应用案例 tensorflow从入门到精通100讲 深 ...
- 高可用架构设计之道,实战案例直面流量洪峰
流量洪峰所带来的一系列挑战,足以激发每位程序员的斗志:高并发.大吞吐.紧急扩容.降级保护--那么,作为程序员,应该如何应对? 由腾讯云官方社区-云加社区举办的线上直播活动,主题聚焦在「高可用架构之流量 ...
最新文章
- python自学argparse模块
- 阿里巴巴硅谷 Istio 专家解读Istio 1.0 发布
- C++ 输入字符串和string 类型总结
- mysql同步大师_数据库大师成长日记:您最需要了解的NoSQL非关系型数据库
- 微信端php 开发技术要求,微信第三方平台开发详解——PHP版
- 面向对象的tab选项卡实现
- Zabbix 数据清理
- Windows 10的成功能让苹果学到点什么?
- 【树叶分类】基于matlab GUI BP神经网络植物叶片识别分类【含Matlab源码 916期】
- 【深入理解JS核心技术】14. 什么是一元函数
- 设置计算机网络文件共享,局域网电脑文件共享怎么设置
- 要成为云架构师需要学什么技能?
- 速写中的颈部肌肉怎么表现?详细画法看这里~
- 30天自制操作系统第8天harib05c
- 在Windows 7和Vista中禁用程序兼容性助手
- 软件测试中如何测试算法?
- .NET内存分析工具 DotMemory
- 【JavaScript】ES5和ES6面向对象
- Learning Entity and Relation Embeddings for Knowledge Graph Completion (TransR)论文翻译
- 留学生回国考计算机等级考试,2018年下半年国家公派留学人员外语水平考试(WSK)报考通知...
热门文章
- mysql_connect函数怎么调用,PHP连接MySQL数据库的连接函数mysql_connect的第三个参数是( )。...
- 通讯录二维码使英文变为中文
- 潮玩艺术盛会来杭州了
- 英文星期的来历(都是来自神人)
- 0324的学习笔记----里面最重要的就是一个tom猫的动画,和涉及到的内存问题(创建imageview的两种方式,imagenamed就会形成缓存,占用很多内
- 程序猿有话说:计算机,学着挺有意思的,就是头冷
- 0基础自学php教程
- Redis 3 配置详解
- 2019下半年中小学教资考试教育知识与能力试题(中学)——主观题
- 【三国演义】——诸葛亮