欢迎访问IT教程网

kubernetes HPA

Linux admin 871℃ 0评论

Horizontal Pod Autoscaling可以根据CPU利用率(内存为不可压缩资源)自动伸缩一个Replication Controller、Deployment 或者Replica Set中的Pod数量;

目前HPA只支持两个版本,其中v1版本只支持核心指标的定义;

[root@node1 k8s-prom]# kubectl api-versions |grep autoscaling
autoscaling/v1
autoscaling/v2beta1
autoscaling/v2beta2

创建一个pod

[root@node1 ~]# kubectl run myapp --image=ikubernetes/myapp:v1 --replicas=1 --requests='cpu=50m,memory=256Mi' --limits='cpu=50m,memory=256Mi' --labels='app=myapp' --expose --port=80
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
service/myapp created
deployment.apps/myapp created

下面我们让myapp 这个pod能自动水平扩展,用kubectl autoscale,其实就是创建HPA控制器的;

[root@node1 ~]# kubectl autoscale deployment myapp --min=1 --max=8 --cpu-percent=60
horizontalpodautoscaler.autoscaling/myapp autoscaled
[root@node1 ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
myapp Deployment/myapp <unknown>/60% 1 8 0 12s


--min:表示最小扩展pod的个数  --max:表示最多扩展pod的个数 
--cpu-percent:cpu利用率

修改type为NodePort做压测

[root@node1 ~]# kubectl patch svc myapp -p '{"spec":{"type": "NodePort"}}'
service/myapp patched
[root@node1 ~]# kubectl get svc |grep myapp
myapp NodePort 10.254.203.52 <none> 80:45764/TCP 61s

 

[root@node2 ~]# yum -y install httpd-tools
[root@node2 ~]# ab -c 1000 -n 50000000 http://192.168.57.14:45764/index.html
[root@node1 ~]# kubectl describe hpa |grep -A 3 "resource cpu" 
 resource cpu on pods (as a percentage of request): 96% (48m) / 60%
Min replicas: 1
Max replicas: 8
Deployment pods: 2 current / 2 desired

[root@node1 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp-657fb86dd-2xbfq 1/1 Running 0 2m11s
myapp-657fb86dd-nmf28 1/1 Running 0 4m52s

等压测完,cpu使用率降下来,pod数量还会自动恢复为1个,如下
[root@node1 ~]# kubectl describe hpa |grep -A 3 "resource cpu" 
 resource cpu on pods (as a percentage of request): 0% (0) / 60%
Min replicas: 1
Max replicas: 8
Deployment pods: 1 current / 1 desired

hpa v2

上面用的是hpav1来做的水平pod自动扩展的功能,hpa v1版本只能根据cpu利用率括水平自动扩展pod。 
接下来我们看一下hpa v2的功能,它可以根据自定义指标利用率来水平扩展pod。

[root@node1 ~]# kubectl delete hpa myapp
horizontalpodautoscaler.autoscaling "myapp" deleted
[root@node1 ~]# vim hpa-v2-demo.yaml


apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: myapp-hpa-v2
spec:
  scaleTargetRef: #根据什么指标来做评估压力
    apiVersion: apps/v1 #对谁来做自动扩展
    kind: Deployment
    name: myapp
  minReplicas: 1 #最少副本数量
  maxReplicas: 10 #最多副本数量
  metrics: #表示依据哪些指标来进行评估
  - type: Resource #表示基于资源进行评估
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50 #pod cpu使用率超过55%,就自动水平扩展pod个数
  - type: Resource
    resource:
      name: memory 
      targetAverageValue: 200Mi

[root@node1 ~]# kubectl apply -f hpa-v2-demo.yaml 
horizontalpodautoscaler.autoscaling/myapp-hpa-v2 created
[root@node1 ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
myapp-hpa-v2 Deployment/myapp <unknown>/50% 1 10 0 10s

再次启动压测查看结果 发现扩展到了3个

[root@node1 ~]# kubectl describe hpa |grep -A 3 "resource cpu" 
 resource cpu on pods (as a percentage of request): 102% (51m) / 50%
Min replicas: 1
Max replicas: 10
Deployment pods: 1 current / 3 desired
[root@node1 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp-657fb86dd-5jssn 1/1 Running 0 14s
myapp-657fb86dd-8876n 0/1 ContainerCreating 0 14s
myapp-657fb86dd-nmf28 1/1 Running 0 14m

hpa v2可以根据cpu和内存使用率进行伸缩Pod个数,还可以根据其他参数进行pod处理,如http并发量

这个需要依靠prometheus

[root@node1 ~]# cat hpa-v2-custom.yaml 
 apiVersion: autoscaling/v2beta2 #从这可以看出是hpa v2版本
 kind: HorizontalPodAutoscaler
 metadata:
   name: myapp-hpa-v2
 spec: 
   scaleTargetRef: #根据什么指标来做评估压力
     apiVersion: apps/v1 #对谁来做自动扩展 
     kind: Deployment 
     name: myapp 
   minReplicas: 1 #最少副本数量 
   maxReplicas: 10 
   metrics: #表示依据哪些指标来进行评估 
   - type: Pods #表示基于资源进行评估 
     pods: 
       metricName: http_requests #自定义的资源指标 
       targetAverageValue: 800m #m表示个数,表示并发数800

本文固定链接:IT教程网 » kubernetes HPA
本站内容除特别标注外均为原创,欢迎转载,但请保留出处!

喜欢 (0)
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址