本文主要是介绍七、HorizontalPodAutoscaler(HPA),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
一、HPA概述:
二、HPA工作机制:
三、HPA流程:
四、HPA API对象:
五、示例:
1、基于CPU的HPA
2、常见问题:
3、基于内存的HPA
一、HPA概述:
- Horizontal Pod Autoscaler,中文就是水平自动伸缩
- 可以基于CPU利用率自动扩缩,Replicationcontroller、Deployment、ReplicaSet、和StatefulSet中的pod 的数量
- 除了CPU利用率、内存占用外,也可以积极与其他应用程序提供的自定义度量指标来执行自动扩缩
- Pod 自动扩缩不适用于无法扩缩的对象,比如 DaemonSet。
- Pod 水平自动扩缩特性由 Kubernetes API 资源和控制器实现。资源决定了控制器的行为。
- 控制器会周期性的调整副本控制器或 Deployment 中的副本数量,以使得 Pod 的平均 CPU 利用率与用户所设定的目标值匹配。
二、HPA工作机制:
Pod水平自动扩容器的实现是一个控制回路,由控制器管理的--horizontal-pod-autoscaler-sync-period 参数指定周期(默认值为 15 秒)通过Status.PodSelector来查询pods的状态,获取pod的CPU使用率,然后,通过现有的pod的CPU使用率的平均值跟目标使用率进行比较,并且在扩容时,还要遵循预先这顶的副本数显示:MinReplicas
三、HPA流程:
- 创建HPA资源,设定目标CPU使用率限额,以及最大、最小实例数
- 收集一组中(PodSelector)每个pod最近一分钟内的cpu使用率,并计算平均值
- 读取hpa中设定的cpu使用限额
- 计算:平均值之和/限额,求出目标调整的实例个数
- 目标调整的个数不能超过设定的最大最小实例数,如果没有超过就扩容,超过,就扩容至最大实例数
- 回环到2,不断循环
四、HPA API对象:
#有三个版本
[root@k8s-master-1 cfg]# kubectl api-versions |grep autoscal
autoscaling/v1 #只通过CPU为参考,来改变pod副本数
autoscaling/v2beta1 #支持通过CPU、内存、连接数以及用户自定义
的资源指标数据为参考
autoscaling/v2beta2 #同上kubectl explain hpa ##默认查询到的是autoscaling/v1版本
kubectl explain hpa --api-version=autoscaling/v2beta1
##如果使用其他版本,可以使用--api-version指明版本
五、示例:
1、基于CPU的HPA
1、创建HorizontalPodAutoscaler.yaml
[root@k8s-master-1 test]# vim HorizontalPodAutoscaler.yaml
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:name: hpa-demo
spec:maxReplicas: 10minReplicas: 1scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: hpa-demotargetCPUUtilizationPercentage: 10
#或者使用命令:
[root@k8s-master-1 test]# kubectl autoscale deployment hpa-demo --cpu-percent=10 --min=1 --max=102、创建Deployment.如果要想让HPA生效,对应的Pod资源必须添加requests资源声明
[root@k8s-master-1 test]# vim deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: hpa-demo
spec:selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- name: nginximage: nginx:1.15.3ports:- containerPort: 80resources:requests:memory: 50Micpu: 50m
2、常见问题:
1、HPA无法计算副本计数:无法获取资源cpu的指标:没有从资源指标API返回指标
解决:由于我前期添加了聚合 API,没有重启kube-controller-manager.service、kube-scheduler.service,所以一直报错,在这就是在HorizontalPodAutoscaler.yaml文件中spec.scaleTargetRef.apiVersion: apps/v1,忘记加s了。这里要和Deployment的apiVersion 一致。
[root@k8s-master-1 test]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
hpa-demo Deployment/hpa-demo 0%/10% 1 10 1 11m[root@k8s-master-1 test]# kubectl describe hpa
Name: hpa-demo
Namespace: default
Labels: <none>
Annotations: <none>
CreationTimestamp: Fri, 05 May 2023 13:55:49 +0800
Reference: Deployment/hpa-demo
Metrics: ( current / target )resource cpu on pods (as a percentage of request): 0% (0) / 10%
Min replicas: 1
Max replicas: 10
Deployment pods: 1 current / 1 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
压力测试
暴露端口给service
#暴露端口给service
[root@k8s-master-1 test]# kubectl expose deployment hpa-demo --port=80 --target-port=80^C
[root@k8s-master-1 test]#
[root@k8s-master-1 test]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hpa-demo ClusterIP 10.0.0.56 <none> 80/TCP 21m
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 15d[root@k8s-master-1 test]# [root@k8s-master-1 test]# kubectl exec -it busybox -- sh
/ #
/ # while true; do wget -q -O- 10.0.0.56; done#在查看hpa 数量在增加
[root@k8s-master-1 ~]# [root@k8s-master-1 ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
hpa-demo Deployment/hpa-demo 316%/10% 1 10 1 30m[root@k8s-master-1 ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
hpa-demo 8/8 8 8 13m
web 1/1 1 1 11d
[root@k8s-master-1 ~]# [root@k8s-master-1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
busybox 1/1 Running 0 94s
hpa-demo-f45447f69-59zsv 1/1 Running 0 13m
hpa-demo-f45447f69-5nd8k 1/1 Running 0 22s
hpa-demo-f45447f69-9whvx 1/1 Running 0 7s
hpa-demo-f45447f69-pv7gb 1/1 Running 0 22s
hpa-demo-f45447f69-qwcrp 1/1 Running 0 7s
hpa-demo-f45447f69-tk4x9 1/1 Running 0 7s
hpa-demo-f45447f69-wvrt8 1/1 Running 0 22s
hpa-demo-f45447f69-xnrcd 1/1 Running 0 7s
web-96d5df5c8-vmxgr 1/1 Running 0 5h51m#接下来我们ctrl+c取消压力测试,过1分钟,甚至更久就看到cpu和pod数量都回去了
[root@k8s-master-1 test]# kubectl get pod
NAME READY STATUS RESTARTS AGE
hpa-demo-f45447f69-59zsv 1/1 Running 0 19m
web-96d5df5c8-vmxgr 1/1 Running 0 5h57m
[root@k8s-master-1 test]# [root@k8s-master-1 test]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
hpa-demo 1/1 1 1 19m
web 1/1 1 1 11d
[root@k8s-master-1 test]# [root@k8s-master-1 test]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
hpa-demo Deployment/hpa-demo 0%/10% 1 10 1 36m
3、基于内存的HPA
创建deployment
apiVersion: apps/v1
kind: Deployment
metadata:name: hpa-demo
spec:selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- name: nginximage: nginx:1.15.3ports:- containerPort: 80resources:requests:memory: 25Micpu: 0.01limits:memory: 60Micpu: 0.05
[root@k8s-master-1 test]# kubectl apply -f deployment.yaml
deployment.apps/hpa-demo created[root@k8s-master-1 test]# vim HorizontalPodAutoscaler.yaml
apiVersion: autoscaling/v2beta1 # v2beta1版本
kind: HorizontalPodAutoscaler
metadata:name: hpa-demo
spec:maxReplicas: 10minReplicas: 1scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: hpa-demometrics:- type: Resourceresource:name: memorytargetAverageUtilization: 50 # 50%内存利用[root@k8s-master-1 test]# kubectl apply -f HorizontalPodAutoscaler.yaml
horizontalpodautoscaler.autoscaling/hpa-demo created[root@k8s-master-1 test]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
hpa-demo Deployment/hpa-demo 5%/50% 1 10 1 47s
这篇关于七、HorizontalPodAutoscaler(HPA)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!