k8s对象-HPA(pod的自动弹性伸缩,autoscaling/v1,autoscaling/v2beta1)

前言
HPA(Horizontal Pod Autoscaler)Pod自动弹性伸缩,K8S通过对Pod中运行的容器各项指标(CPU占用、内存占用、网络请求量)的检测,实现对Pod实例个数的动态新增和减少。

1. autoscaling/v1

说明:该API版本仅对CPU做了限制

创建一个HPA,yml文件如下:

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: iot-server
spec:
  minReplicas: 1                   #最小pod数量
  maxReplicas: 10                    #最大pod数量
  scaleTargetRef:
    apiVersion: v1
    kind: Deployment          #需要扩容的对象类型
    name: iot-server            #需要扩容的对象名称
  targetCPUUtilizationPercentage: 90   #CPU平均使用率超过90%扩容

说明:需要对容器进行request进行限制。

2. autoscaling/v2beta1

说明:
autoscaling/v2beta1可以限制 CPU、MEM、自定义metric 做出限制。
但是依赖于metrics-server

2.1 metrics-server安装

命令不要复制,因为pod的名字是我自己的,tab补全就可以了。

[root@DoM01 crust-framework]# kubectl get pod -n kube-system metrics-server-5996bcd4f-b7xrh
NAME                             READY   STATUS    RESTARTS   AGE
metrics-server-5996bcd4f-b7xrh   1/1     Running   1          49d
  • 测试metrics-server

安装好之后可以使用top命令了,在安装文档里可以理解到,这里不赘述了。我们直接看我自己的一个namespace下的pod占用资源。

[root@DoM01 crust-framework]# kubectl top pod -n crust-framework
NAME                       CPU(cores)   MEMORY(bytes)
auth-6b68bccbcd-tp99p      1010m        5867Mi
gateway-679776557b-82gtv   2916m        1282Mi
user-6b4c7466c-tv572       97m          4179Mi
web-65d57c4559-bdq2x       0m 

2.2 限制CPU和MEM

  • yml文件

说明:
这是我写在整个项目的chart里边的一部分,大家使用的时候只要把里边的变量替换成你的实际值即可。

---
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: auth
  namespace: "{{ .Values.global.namespace }}"
spec:
  scaleTargetRef:
    apiVersion: extensions/v1beta1
    kind: Deployment
    name: auth
  minReplicas: {{ .Values.auth.minReplicas }}
  maxReplicas: {{ .Values.auth.maxReplicas }}
  metrics:
  - type: Resource
    resource:
      name: cpu
      targetAverageUtilization: {{ .Values.auth.targetAverageUtilization }}
  - type: Resource
    resource:
      name: memory
      targetAverageValue:  {{ .Values.auth.targetAverageValue }}
---

注解:
minReplicas:副本最小值(示例 1)
maxReplicas: 副本最大值(示例 3)
targetAverageUtilization:CPU扩容阈值(示例 80)
targetAverageValue: MEM扩容限制(示例 12Gi)

  • 查看
[root@DoM01 crust-framework]# kubectl get pod -n crust-framework
NAME                       READY   STATUS    RESTARTS   AGE
auth-66f9487df-g4v85       1/1     Running   0          102s
auth-66f9487df-hl4md       1/1     Running   0          42s
gateway-7db45f869c-bchvj   1/1     Running   0          118m
user-6b4c7466c-tv572       1/1     Running   1          9d
web-65d57c4559-bdq2x       1/1     Running   1          9d

2.3 自定义metric

apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: podinfo
spec:
  scaleTargetRef:
    apiVersion: extensions/v1beta1
    kind: Deployment
    name: podinfo
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      targetAverageUtilization: 80
  - type: Resource
    resource:
      name: memory
      targetAverageValue: 200Mi
  - type: Pods
    pods:
      metric:
        name: packets-per-second
      target:
        type: AverageValue
        averageValue: 1k
  - type: Object
    object:
      metric:
        name: requests-per-second
      describedObject:
        apiVersion: networking.k8s.io/v1beta1
        kind: Ingress
        name: main-route
      target:
        type: Value
        value: 10k

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

玄德公笔记

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

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

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

打赏作者

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

抵扣说明:

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

余额充值