七、HorizontalPodAutoscaler(HPA)

目录

一、HPA概述:

二、HPA工作机制:

三、HPA流程:

 四、HPA API对象:

五、示例:

1、基于CPU的HPA

2、常见问题:

3、基于内存的HPA

一、HPA概述:

  1. Horizontal Pod Autoscaler,中文就是水平自动伸缩
  2. 可以基于CPU利用率自动扩缩,Replicationcontroller、Deployment、ReplicaSet、和StatefulSet中的pod 的数量
  3. 除了CPU利用率、内存占用外,也可以积极与其他应用程序提供的自定义度量指标来执行自动扩缩
  4. Pod 自动扩缩不适用于无法扩缩的对象,比如 DaemonSet。
  5. Pod 水平自动扩缩特性由 Kubernetes API 资源和控制器实现。资源决定了控制器的行为。
  6. 控制器会周期性的调整副本控制器或 Deployment 中的副本数量,以使得 Pod 的平均 CPU 利用率与用户所设定的目标值匹配。

二、HPA工作机制:

        Pod水平自动扩容器的实现是一个控制回路,由控制器管理的--horizontal-pod-autoscaler-sync-period 参数指定周期(默认值为 15 秒)通过Status.PodSelector来查询pods的状态,获取pod的CPU使用率,然后,通过现有的pod的CPU使用率的平均值跟目标使用率进行比较,并且在扩容时,还要遵循预先这顶的副本数显示:MinReplicas

三、HPA流程:

  1. 创建HPA资源,设定目标CPU使用率限额,以及最大、最小实例数
  2. 收集一组中(PodSelector)每个pod最近一分钟内的cpu使用率,并计算平均值
  3. 读取hpa中设定的cpu使用限额
  4. 计算:平均值之和/限额,求出目标调整的实例个数
  5. 目标调整的个数不能超过设定的最大最小实例数,如果没有超过就扩容,超过,就扩容至最大实例数
  6. 回环到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: 10
  minReplicas: 1
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: hpa-demo
  targetCPUUtilizationPercentage: 10
#或者使用命令:
[root@k8s-master-1 test]# kubectl autoscale deployment hpa-demo --cpu-percent=10 --min=1 --max=10

2、创建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: nginx

  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.15.3
        ports:
        - containerPort: 80
        resources:
          requests:
            memory: 50Mi
            cpu: 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 recommendation
  ScalingActive   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: nginx

  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.15.3
        ports:
        - containerPort: 80
        resources:
          requests:
            memory: 25Mi
            cpu: 0.01
          limits:
            memory: 60Mi
            cpu: 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: 10
  minReplicas: 1
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: hpa-demo
  metrics:
  - type: Resource
    resource:
      name: memory
      targetAverageUtilization: 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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • 29
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

繁华依在

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值