k8s之HPA(Pod水平自动伸缩)

Horizontal Pod Autoscaler官方文档:Pod 水平自动扩缩 | Kubernetes

Pod 水平自动扩缩(Horizontal Pod Autoscaler) 可以基于 CPU 利用率自动扩缩 ReplicationController、Deployment、ReplicaSet 和 StatefulSet 中的 Pod 数量。 除了 CPU 利用率,也可以基于其他应程序提供的 自定义度量指标 来执行自动扩缩。 Pod 自动扩缩不适用于无法扩缩的对象,比如 DaemonSet。

Pod 水平自动扩缩特性由 Kubernetes API 资源和控制器实现。资源决定了控制器的行为。 控制器会周期性地调整副本控制器或 Deployment 中的副本数量,以使得类似 Pod 平均 CPU 利用率、平均内存利用率这类观测到的度量值与用户所设定的目标值匹配。

Kubectl top ->apiserver->metrics server-> kubelet(cadvisor)->pod

HPA是根据指标来进行自动伸缩的,目前HPA有两个版本–v1和v2beta

HPA的API有三个版本,通过kubectl api-versions | grep autoscal可看到

[root@master1 yaml]# kubectl api-versions | grep autosca
autoscaling/v1
autoscaling/v2beta1
autoscaling/v2beta2

查看使用的版本:

kubectl explain hpa

查看指定其他版本:

kubectl explain hpa --api-version=autoscaling/v2beta1

autoscaling/v1只支持基于CPU指标的缩放;

autoscaling/v2beta1支持Resource Metrics(资源指标,如pod内存)和Custom Metrics(自定义指标)的缩放;

autoscaling/v2beta2支持Resource Metrics(资源指标,如pod的内存)和Custom Metrics(自定义指标)和ExternalMetrics

1.部署一下metrics-server,收集集群资源利用率

metrics-server版本获取:
https://github.com/kubernetes-sigs/metrics-server/releases

vim metrics-server.yaml

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: system:aggregated-metrics-reader
  labels:
    rbac.authorization.k8s.io/aggregate-to-view: "true"
    rbac.authorization.k8s.io/aggregate-to-edit: "true"
    rbac.authorization.k8s.io/aggregate-to-admin: "true"
rules:
- apiGroups: ["metrics.k8s.io"]
  resources: ["pods", "nodes"]
  verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: metrics-server:system:auth-delegator
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: system:auth-delegator
subjects:
- kind: ServiceAccount
  name: metrics-server
  namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: metrics-server-auth-reader
  namespace: kube-system
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: extension-apiserver-authentication-reader
subjects:
- kind: ServiceAccount
  name: metrics-server
  namespace: kube-system
---
apiVersion: apiregistration.k8s.io/v1
kind: APIService
metadata:
  name: v1beta1.metrics.k8s.io
spec:
  service:
    name: metrics-server
    namespace: kube-system
  group: metrics.k8s.io
  version: v1beta1
  insecureSkipTLSVerify: true
  groupPriorityMinimum: 100
  versionPriority: 100
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: metrics-server
  namespace: kube-system
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: metrics-server
  namespace: kube-system
  labels:
    k8s-app: metrics-server
spec:
  selector:
    matchLabels:
      k8s-app: metrics-server
  template:
    metadata:
      name: metrics-server
      labels:
        k8s-app: metrics-server
    spec:
      serviceAccountName: metrics-server
      volumes:
      # mount in tmp so we can safely use from-scratch images and/or read-only containers
      - name: tmp-dir
        emptyDir: {}
      containers:
      - name: metrics-server
        image:  registry.cn-shenzhen.aliyuncs.com/lishanbin/metrics-server:v0.3.7
        imagePullPolicy: IfNotPresent
        args:
          - --cert-dir=/tmp
          - --secure-port=4443
          - --kubelet-insecure-tls
          - --kubelet-preferred-address-types=InternalIP
        ports:
        - name: main-port
          containerPort: 4443
          protocol: TCP
        securityContext:
          readOnlyRootFilesystem: true
          runAsNonRoot: true
          runAsUser: 1000
        volumeMounts:
        - name: tmp-dir
          mountPath: /tmp
      #nodeSelector:
      #  kubernetes.io/os: linux
      #  kubernetes.io/arch: "amd64"
---
apiVersion: v1
kind: Service
metadata:
  name: metrics-server
  namespace: kube-system
  labels:
    kubernetes.io/name: "Metrics-server"
    kubernetes.io/cluster-service: "true"
spec:
  selector:
    k8s-app: metrics-server
  ports:
  - port: 443
    protocol: TCP
    targetPort: main-port
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: system:metrics-server
rules:
- apiGroups:
  - ""
  resources:
  - pods
  - nodes
  - nodes/stats
  - namespaces
  - configmaps
  verbs:
  - get
  - list
  - watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: system:metrics-server
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: system:metrics-server
subjects:
- kind: ServiceAccount
  name: metrics-server
  namespace: kube-system
[root@master1 yaml]#  kubectl api-versions |grep metrics
metrics.k8s.io/v1beta1
[root@master1 yaml]# kubectl top nodes
NAME              CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%     
192.168.203.219   180m         2%     2936Mi          18%         
192.168.203.220   499m         6%     1662Mi          10%         
192.168.203.221   203m         2%     1883Mi          11%         
192.168.203.223   151m         1%     9805Mi          61%         
192.168.203.226   343m         4%     10645Mi         67%         
192.168.203.228   299m         3%     10698Mi         67%  

2.hpa基于cpu自动扩缩容

HPA伸缩过程:
收集HPA控制下所有Pod最近的cpu使用情况(CPU utilization)
对比在扩容条件里记录的cpu限额(CPUUtilization)
调整实例数(必须要满足不超过最大/最小实例数)
每隔30s做一次自动扩容的判断
CPU utilization的计算方法是用cpu usage(最近一分钟的平均值,通过metrics可以直接获取到)除以cpu request(这里cpu request就是我们在创建容器时制定的cpu使用核心数)得到一个平均值,这个平均值可以理解为:平均每个Pod CPU核心的使用占比。

HPA进行伸缩算法:
计算公式:TargetNumOfPods = ceil(sum(CurrentPodsCPUUtilization) / Target)
ceil()表示取大于或等于某数的最近一个整数
每次扩容后冷却3分钟才能再次进行扩容,而缩容则要等5分钟后。
当前Pod Cpu使用率与目标使用率接近时,不会触发扩容或缩容:
触发条件:avg(CurrentPodsConsumption) / Target >1.1 或 <0.9

 kubectl autoscale deployment f1 -n lishanbin --cpu-percent=1 --min=1 --max=2

 yaml例子

---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: ubb
  namespace: hpa
spec:
  maxReplicas: 4
  minReplicas: 1
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: ubb
  targetCPUUtilizationPercentage: 20

 

查看hpa

[root@master1 yaml]# kubectl get hpa   -n lishanbin
NAME   REFERENCE       TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
f1     Deployment/f1   2%/1%     1         2         2          3m33s

验证:可以查看到创建了一个新的pod

[root@master1 yaml]# kubectl get pod   -n lishanbin
NAME                     READY   STATUS        RESTARTS   AGE
f1-856675bdbf-n4fmk      1/1     Running       0          11d
f1-856675bdbf-n9gg8      1/1     Running       0          4m5s
[root@master1 yaml]# kubectl get deployment   -n baichuan
NAME     READY   UP-TO-DATE   AVAILABLE   AGE
f1       2/2     2            2           447d
f1web    1/1     1            1           447d

发现hpa已经超过了预定值,随之pod的副本数也变成了1个,最多可变成2个,停止负载后,副本数也会变成一个

如果出现了 failed to get cpu utilization: missing request for cpu 这样的错误信息。这是因为我们上面创建的 Pod 对象没有添加 request 资源声明,这样导致 HPA 读取不到 CPU 指标信息,所以如果要想让 HPA 生效,对应的 Pod 资源必须添加 requests 资源声明

假如targets字段有显示unknown

原因:

刚建立,等待一段时间再查看

需要自动伸缩的目标资源并没有进行资源限制

3.hpa基于内存扩缩容

targetAverageUtilization 表示的是百分比
targetAverageValue 表示的是数值,比如100m的CPU、100Mi的内存

apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: kevin-t-hap
  namespace: lishanbin
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: f1
  minReplicas: 1
  maxReplicas: 2
  metrics:
  - type: Resource
    resource:
      name: cpu
      targetAverageUtilization: 80
  - type: Resource
    resource:
      name: memory
      targetAverageValue: 30Mi

如上,设置了f1的deployment控制的pod的HPA限制,当cpu使用超过设置的80%,内存使用超过30Mi时就触发自动扩容,副本数最小为1,最大为2。

你可能感兴趣的:(kubernetes,kubernetes,容器,docker)