手机网站做多少钱,做网站需要公司么,中国形象设计网,wordpress html5 app目录
一、HPA概述#xff1a;
二、HPA工作机制#xff1a;
三、HPA流程: 四、HPA API对象:
五、示例#xff1a;
1、基于CPU的HPA
2、常见问题#xff1a;
3、基于内存的HPA 一、HPA概述#xff1a;
Horizontal Pod Autoscaler#xff0c;中文就是水平自动伸缩可…目录
一、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对象:
#有三个版本
[rootk8s-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-versionautoscaling/v2beta1
##如果使用其他版本可以使用--api-version指明版本
五、示例
1、基于CPU的HPA
1、创建HorizontalPodAutoscaler.yaml
[rootk8s-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
#或者使用命令
[rootk8s-master-1 test]# kubectl autoscale deployment hpa-demo --cpu-percent10 --min1 --max102、创建Deployment.如果要想让HPA生效,对应的Pod资源必须添加requests资源声明
[rootk8s-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 一致。
[rootk8s-master-1 test]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
hpa-demo Deployment/hpa-demo 0%/10% 1 10 1 11m[rootk8s-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
[rootk8s-master-1 test]# kubectl expose deployment hpa-demo --port80 --target-port80^C
[rootk8s-master-1 test]#
[rootk8s-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[rootk8s-master-1 test]# [rootk8s-master-1 test]# kubectl exec -it busybox -- sh
/ #
/ # while true; do wget -q -O- 10.0.0.56; done#在查看hpa 数量在增加
[rootk8s-master-1 ~]# [rootk8s-master-1 ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
hpa-demo Deployment/hpa-demo 316%/10% 1 10 1 30m[rootk8s-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
[rootk8s-master-1 ~]# [rootk8s-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#接下来我们ctrlc取消压力测试过1分钟甚至更久就看到cpu和pod数量都回去了
[rootk8s-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
[rootk8s-master-1 test]# [rootk8s-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
[rootk8s-master-1 test]# [rootk8s-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
[rootk8s-master-1 test]# kubectl apply -f deployment.yaml
deployment.apps/hpa-demo created[rootk8s-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%内存利用[rootk8s-master-1 test]# kubectl apply -f HorizontalPodAutoscaler.yaml
horizontalpodautoscaler.autoscaling/hpa-demo created[rootk8s-master-1 test]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
hpa-demo Deployment/hpa-demo 5%/50% 1 10 1 47s