HPA全称HorizontalPodAutoscaler Pod水平自动扩缩容,Kubernetes控制器HPA是一种用于自动调整Pod数量的控制器。它可以根据资源使用情况自动增加或减少Pod的数量,以确保应用程序的高可用性和性能。HPA可以根据CPU使用率或自定义指标来进行调整,例如请求延迟或网络吞吐量
Pod 水平自动扩缩中文文档
HPA一般通过指示工作负载资源如Deployment、StatefulSet 或其他类似资源进行扩缩容,对于像DaemonSet这种无法扩缩的对象不适用
使用之前我们先安装metrics-server,它可以用来收集集群中的资源使用情况。hpa通过metrics-server的api获取这些数据,然后进行扩缩容的副本计算,得到目标副本数量,如果与当前副本数不相同就通过副本控制器进行scale操作
metrics-server的yaml清单获取
metrics-server
安装完后使用top命令查看
[root@master hpa]# kubectl top node
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
master 124m 6% 2508Mi 69%
node01 59m 2% 1941Mi 54%
node02 53m 2% 2010Mi 55%
apiVersion: autoscaling/v2beta1 # 使用autoscaling/v2beta1版本的HPA API
kind: HorizontalPodAutoscaler # 定义HPA控制器
metadata:
name: example-hpa # HPA资源的名称
namespace: default # HPA资源所在的命名空间
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment # 所有Pod数量将根据该Deployment对象的副本数量进行自动缩放
name: example-deployment # HPA将控制的Deployment对象名称
minReplicas: 2 # 最小Pod数量
maxReplicas: 10 # 最大Pod数量
metrics:
- type: Resource # 使用资源指标类型
resource:
name: cpu # 使用CPU使用率作为资源指标
target:
type: Utilization # 使用CPU利用率作为度量标准
averageUtilization: 50 # 目标CPU利用率,达到该利用率时触发自动扩缩容
- type: Object # 使用对象指标类型
object:
metricName: example_http_requests_total # 自定义指标名称
target:
type: Value # 目标值类型,可以是Value或AverageValue
value: "100" # 触发自动扩缩容的目标值
describedObject:
apiVersion: v1
kind: Service # 该对象指标作用于Service对象
name: example-service # 该对象指标作用于的Service对象名称
上面的自定义指标配置不会再本章讲解
下面以一个案例来解释hpa的使用
使用hpa来控制deployment自动扩缩容,当pod资源平均使用率达到3%时进行扩容
创建一个Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
selector:
matchLabels:
app: nginx-hpa
template:
metadata:
labels:
app: nginx-hpa
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- containerPort: 80
protocol: TCP
resources:
requests:
memory: "50Mi"
cpu: "100m"
# 创建deployment
[root@master hpa]# kubectl create -f hpa-deploy.yaml
deployment.apps/nginx created
# 记录下IP后面压测用到 10.244.2.104
[root@master hpa]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-659b6d57bd-5tbg2 1/1 Running 0 10s 10.244.2.104 node02 <none> <none>
创建HPA
最小副本数1,最大10 autoscaling/v1版本
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: hpa
spec:
minReplicas: 1
maxReplicas: 10
targetCPUUtilizationPercentage: 3
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx
autoscaling/v2版本
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: hpa
spec:
minReplicas: 1
maxReplicas: 10
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 3
# 创建hpa
[root@master hpa]# kubectl create -f hpa.yaml
horizontalpodautoscaler.autoscaling/hpa created
# 查看hpa,还没有完全启动完
[root@master hpa]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
hpa Deployment/nginx <unknown>/3% 1 10 0 6s
# 再次查看hpa,启动完成 目前资源使用率 0%
[root@master hpa]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
hpa Deployment/nginx 0%/3% 1 10 1 20s
使用ab工具进行压测,没有该工具需要安装 httpd-tools
# 安装 httpd-tools
yum -y install httpd-tools
# ab工具可以模拟多个用户(并发请求),向Web服务器发送请求,并统计响应时间和处理请求数量等信息
# ab -n requests -c concurrency url
# 参数说明:
#- requests:总请求数,例如:-n 1000。
#- concurrency:并发请求数,例如:-c 10。
#- url:测试的URL地址。
压测
ab -n 10000 -c 1000 http://10.244.2.104/
观察压测结果
# 监听hpa 发现压测中cpu达到60% ,后面又降回去了
[root@master ~]# kubectl get hpa -w
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
hpa Deployment/nginx 0%/3% 1 10 1 7m30s
hpa Deployment/nginx 60%/3% 1 10 1 9m1s
hpa Deployment/nginx 0%/3% 1 10 4 9m16s
hpa Deployment/nginx 0%/3% 1 10 8 9m31s
hpa Deployment/nginx 0%/3% 1 10 10 9m46s
# 监听Pod 一开始只有一个nginx-659b6d57bd-5tbg2 运行了8m47s,压测后又创建了9个Pod,观察ContainerCreating数量
[root@master hpa]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
nginx-659b6d57bd-5tbg2 1/1 Running 0 8m47s
nginx-659b6d57bd-47f98 0/1 Pending 0 0s
nginx-659b6d57bd-47f98 0/1 Pending 0 0s
nginx-659b6d57bd-qs4tr 0/1 Pending 0 0s
nginx-659b6d57bd-tvnmp 0/1 Pending 0 0s
nginx-659b6d57bd-tvnmp 0/1 Pending 0 0s
nginx-659b6d57bd-qs4tr 0/1 Pending 0 0s
nginx-659b6d57bd-47f98 0/1 ContainerCreating 0 0s
nginx-659b6d57bd-qs4tr 0/1 ContainerCreating 0 0s
nginx-659b6d57bd-tvnmp 0/1 ContainerCreating 0 0s
nginx-659b6d57bd-47f98 1/1 Running 0 1s
nginx-659b6d57bd-tvnmp 1/1 Running 0 1s
nginx-659b6d57bd-qs4tr 1/1 Running 0 2s
nginx-659b6d57bd-mccdl 0/1 Pending 0 0s
nginx-659b6d57bd-mccdl 0/1 Pending 0 0s
nginx-659b6d57bd-w9r46 0/1 Pending 0 0s
nginx-659b6d57bd-498c2 0/1 Pending 0 0s
nginx-659b6d57bd-w9r46 0/1 Pending 0 0s
nginx-659b6d57bd-498c2 0/1 Pending 0 0s
nginx-659b6d57bd-mccdl 0/1 ContainerCreating 0 0s
nginx-659b6d57bd-nvhgh 0/1 Pending 0 0s
nginx-659b6d57bd-nvhgh 0/1 Pending 0 0s
nginx-659b6d57bd-w9r46 0/1 ContainerCreating 0 0s
nginx-659b6d57bd-498c2 0/1 ContainerCreating 0 0s
nginx-659b6d57bd-nvhgh 0/1 ContainerCreating 0 0s
nginx-659b6d57bd-mccdl 1/1 Running 0 1s
nginx-659b6d57bd-498c2 1/1 Running 0 1s
nginx-659b6d57bd-w9r46 1/1 Running 0 1s
nginx-659b6d57bd-nvhgh 1/1 Running 0 1s
nginx-659b6d57bd-gq6dc 0/1 Pending 0 0s
nginx-659b6d57bd-gq6dc 0/1 Pending 0 0s
nginx-659b6d57bd-k7vdz 0/1 Pending 0 0s
nginx-659b6d57bd-k7vdz 0/1 Pending 0 0s
nginx-659b6d57bd-gq6dc 0/1 ContainerCreating 0 0s
nginx-659b6d57bd-k7vdz 0/1 ContainerCreating 0 0s
nginx-659b6d57bd-gq6dc 1/1 Running 0 1s
nginx-659b6d57bd-k7vdz 1/1 Running 0 1s
# 观察deploy,看到副本数量也是达到10个
[root@master ~]# kubectl get deploy -w
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 1 1 9m19s
nginx 1/4 1 1 10m
nginx 1/4 1 1 10m
nginx 1/4 1 1 10m
nginx 1/4 4 1 10m
nginx 2/4 4 2 10m
nginx 3/4 4 3 10m
nginx 4/4 4 4 10m
nginx 4/8 4 4 10m
nginx 4/8 4 4 10m
nginx 4/8 4 4 10m
nginx 4/8 8 4 10m
nginx 5/8 8 5 10m
nginx 6/8 8 6 10m
nginx 7/8 8 7 10m
nginx 8/8 8 8 10m
nginx 8/10 8 8 11m
nginx 8/10 8 8 11m
nginx 8/10 8 8 11m
nginx 8/10 10 8 11m
nginx 9/10 10 9 11m
nginx 10/10 10 10 11m
压测完后缩容
大概等待五分钟,会看到缩容效果
# deploy的pod副本书已经缩为1个
[root@master ~]# kubectl get deploy -w
nginx 10/10 10 10 11m
nginx 10/1 10 10 16m
nginx 10/1 10 10 16m
nginx 1/1 1 1 16m
# 观察Pod,开始逐步缩容 Pod停止Terminating
[root@master ~]# kubectl get pod -w
nginx-659b6d57bd-mccdl 1/1 Terminating 0 5m45s
nginx-659b6d57bd-47f98 1/1 Terminating 0 6m
nginx-659b6d57bd-w9r46 1/1 Terminating 0 5m45s
nginx-659b6d57bd-gq6dc 1/1 Terminating 0 5m30s
nginx-659b6d57bd-nvhgh 1/1 Terminating 0 5m45s
nginx-659b6d57bd-k7vdz 1/1 Terminating 0 5m30s
nginx-659b6d57bd-498c2 1/1 Terminating 0 5m45s
nginx-659b6d57bd-qs4tr 1/1 Terminating 0 6m
nginx-659b6d57bd-tvnmp 1/1 Terminating 0 6m
nginx-659b6d57bd-498c2 0/1 Terminating 0 5m46s
nginx-659b6d57bd-498c2 0/1 Terminating 0 5m46s
nginx-659b6d57bd-498c2 0/1 Terminating 0 5m46s
nginx-659b6d57bd-k7vdz 0/1 Terminating 0 5m31s
nginx-659b6d57bd-k7vdz 0/1 Terminating 0 5m31s
nginx-659b6d57bd-k7vdz 0/1 Terminating 0 5m31s
nginx-659b6d57bd-nvhgh 0/1 Terminating 0 5m46s
nginx-659b6d57bd-nvhgh 0/1 Terminating 0 5m46s
nginx-659b6d57bd-nvhgh 0/1 Terminating 0 5m46s
nginx-659b6d57bd-47f98 0/1 Terminating 0 6m1s
nginx-659b6d57bd-47f98 0/1 Terminating 0 6m2s
nginx-659b6d57bd-47f98 0/1 Terminating 0 6m2s
nginx-659b6d57bd-gq6dc 0/1 Terminating 0 5m32s
nginx-659b6d57bd-gq6dc 0/1 Terminating 0 5m32s
nginx-659b6d57bd-gq6dc 0/1 Terminating 0 5m32s
nginx-659b6d57bd-mccdl 0/1 Terminating 0 5m47s
nginx-659b6d57bd-mccdl 0/1 Terminating 0 5m47s
nginx-659b6d57bd-mccdl 0/1 Terminating 0 5m47s
nginx-659b6d57bd-qs4tr 0/1 Terminating 0 6m2s
nginx-659b6d57bd-w9r46 0/1 Terminating 0 5m47s
nginx-659b6d57bd-qs4tr 0/1 Terminating 0 6m2s
nginx-659b6d57bd-qs4tr 0/1 Terminating 0 6m2s
nginx-659b6d57bd-w9r46 0/1 Terminating 0 5m47s
nginx-659b6d57bd-w9r46 0/1 Terminating 0 5m47s
nginx-659b6d57bd-tvnmp 0/1 Terminating 0 6m3s
nginx-659b6d57bd-tvnmp 0/1 Terminating 0 6m3s
nginx-659b6d57bd-tvnmp 0/1 Terminating 0 6m3s
# 只有一个Pod
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-659b6d57bd-5tbg2 1/1 Running 0 18m
欢迎关注,学习不迷路!