k8s-HPA(pod的自动弹性伸缩)

image.png

前言
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

  • kubeadm安装的k8s集群默认已经有metrics-server了
[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
[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

2.2.1 yml文件

  • 创建HPA的yml文件如下:
    以下是v2版本对CPU和内存的限制,创建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

你可能感兴趣的:(k8s-HPA(pod的自动弹性伸缩))