• HPA Pod自动扩缩容


    一、简介

    我们可以通过手工执行 kubectl scale命令和在Dashboard 上操作实现pod的扩缩容,Kubernetes系统也能够根据Pod当前负载的变化情况自动的进行扩缩容。Kubernetes提供了一个资源对象:Horizontal Pod Autoscaling (Pod水平自动伸缩),简称 HPAHPA通过监控分析 RC或者 Deployment控制的所有 Pod的负载变化情况确定是否需要调整 Pod 的副本数量,这是HPA的基本原理。

    HPA 控制器通过 Metrics Server的API获取指标数据,基于用户定义的扩缩容规则进行计算,得到目标Pod的副本数量。当目标Pod副本数量与当前副本数量不同时,HPA 控制器就向Pod副本控制器(Deployment、RC或RS)发起scale操作,调整Pod的副本数量,完成扩缩容操作。

    二、部署操作

    2.1 hpa-nginx.yaml

    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: hpa-nginx
      labels:
        app: hpa-nginx
    spec:
      selector:
        app: hpa-nginx
      ports:
      - port: 80
        protocol: TCP
        targetPort: 80---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: hpa-nginx
      labels:
        app: hpa-nginx
    spec:
      selector:
        matchLabels:
          app: hpa-nginx
      revisionHistoryLimit: 10
      template:
        metadata:
          name: hpa-nginx
          labels:
            app: hpa-nginx
        spec:
          containers:
          - name: hpa-nginx
            image:  nginx:1.18.0
            ports:
            - containerPort: 80
            resources:
              limits:
                cpu: 0.2
                memory: 100Mi
              requests:
                cpu: 0.1
                memory: 10Mi
                
    # resources 字段需要添加,否则hpa无法获取目标pod的资源利用指标,会显示unknow
    ​
    # kubectl get pods -l app=hpa-nginx
    NAME                         READY   STATUS    RESTARTS   AGE
    hpa-nginx-5dc8fcf8c8-wl9jr   1/1     Running   0          10m
    ​
    ​
    # kubectl get svc -l app=hpa-nginx
    NAME        TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
    hpa-nginx   ClusterIP   10.100.92.206   <none>        80/TCP    11m

     

    2.2 创建一个HPA

    # kubectl autoscale deployment hpa-nginx --cpu-percent=8 --min=1  --max=10
    ​
    ​
    # kubectl get hpa
    NAME        REFERENCE              TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
    hpa-nginx   Deployment/hpa-nginx   0%/10%    1         10        1          13h

    此命令创建一个关联资源hpa-nginx 的 HPA,最小的Pod副本数量为1,最大为10。HPA会根据设定的cpu使用率(8%)动态的增加或减少Pod的数量。

     

    2.3 测试HPA自动扩缩容

    hpa-verify.yaml

    apiVersion: v1
    kind: Pod
    metadata:
      name: hpa-busybox
      labels:
        app: hpa-busybox
    spec:
      containers:
      - name: hpa-busybox
        image: registry.cn-hangzhou.aliyuncs.com/google_containers/busybox
        imagePullPolicy: IfNotPresent
        command:
        - sleep
        - "600"
    kubectl exec -it hpa-busybox-d9c949d7-mpplg -- sh
    / # while true; do wget -q -O- http://hpa-nginx;done

    查看Pod资源使用情况和hpa扩缩容情况

    # hpa情况
    # kubectl get hpa
    NAME        REFERENCE              TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
    hpa-nginx   Deployment/hpa-nginx   86%/10%   1         10        9          14h
    ​
    # Pod资源情况
    ​
    # kubectl top pod hpa-nginx-57fcdcdc46-ww5s6
    NAME                         CPU(cores)   MEMORY(bytes)   
    hpa-nginx-57fcdcdc46-ww5s6   86m          1Mi 
    ​
    # hpa扩缩容情况
    # kubectl get pod -l app=hpa-nginx
    NAME                         READY   STATUS    RESTARTS   AGE
    hpa-nginx-57fcdcdc46-5frjt   1/1     Running   0          43s
    hpa-nginx-57fcdcdc46-856lm   1/1     Running   0          28s
    hpa-nginx-57fcdcdc46-9fm5x   1/1     Running   0          28s
    hpa-nginx-57fcdcdc46-bq48j   1/1     Running   0          43s
    hpa-nginx-57fcdcdc46-gx48s   1/1     Running   0          28s
    hpa-nginx-57fcdcdc46-h7kv4   1/1     Running   0          28s
    hpa-nginx-57fcdcdc46-pthrz   1/1     Running   0          13s
    hpa-nginx-57fcdcdc46-rwz7k   1/1     Running   0          43s
    hpa-nginx-57fcdcdc46-ww5s6   1/1     Running   0          14h

    将测试访问停掉之后,过段时间就会自动缩容

    # kubectl get hpa
    NAME        REFERENCE              TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
    hpa-nginx   Deployment/hpa-nginx   0%/10%    1         10        1          14h

    定义申明式yaml文件来创建hpa

    apiVersion: autoscaling/v1
    kind: HorizontalPodAutoscaler
    metadata:
      name: hpa-nginx
    spec:
      scaleTargetRef:
        apiVersion: apps/v1
        kind: Deployment
        name: hpa-nginx
      minReplicas: 1
      maxReplicas: 10
      targetCPUUtilizationPercentage: 50

     

  • 相关阅读:
    Web的26项基本概念和技术
    StringComparison枚举
    WebDriver使用指南(完整篇)
    Sublime Text 2 入门及技巧
    每天一个 Linux 命令(16):which whereis locate命令
    每天一个 Linux 命令(13):less 命令
    每天一个 Linux 命令(12):more命令
    每天一个linux命令(11):nl命令
    每天一个linux命令(10):cat 命令
    每天一个命令 ls
  • 原文地址:https://www.cnblogs.com/bigberg/p/13559410.html
Copyright © 2020-2023  润新知