kubernetes - HPA 自動縮放

December 20, 2020

DeploymentReplicaSetReplication ControllerStatefulSet 等資源可以使用手動方式運行時調整,因此可根據業務規模進行調整。但這樣的調整需要依賴資源的監控來推敲合理的值進行調整。以下是 “HPA 示意圖” 官方提供

HPA(Horizontal Pod Autoscaler),可讓控制器下的 Pod 進行伸縮的工具,HPA 可以針對 CPU 指標數據作為評估基準,或是從資源指標 API 和自訂義指標 API 中獲取的指標數據。

CA(Cluster Autoscaler) 是集群規模自動伸縮工具,能應用在 GCP、AWS 或 Azure 等上。

VPA(Vertical Pod Autoscaler) 是 Pod 應用垂直伸縮工具,透過調整 Pod 對象的 CPU 和 Memory 資源需求量完成擴展或收縮。

HPA 本身是一個 loop 的實現,週期由 controller-manager--horizontal-pod-autoscaler-sync-period 定義,預設 30 秒。週期內 controller-manager 根據 HPA 定義中的指標查詢相應資源使用率。controller-manager 從資源指標或是自定義指標 API 中獲取數據。

HAP 可以藉由 HeapsterREST 接口獲取指標。使用 Heapster 時需要佈署;使用 REST 時,資源指標 API 和 API Server 部分需要先佈署。

HPA 控制器

我們可以藉由 kubectl autoscale 進行操作。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
        resources:
          requests:
            memory: "256Mi"
            cpu: "50m"
          limits:
            memory: "256Mi"
            cpu: "50m"

以下是宣告一個 HPA 控制器自動控管其 Pod 的規模,當 CPU 使用率到 60% 時,會進行增長。

$ kubectl autoscale deployment nginx --min=2 --max=5 --cpu-percent=60
$ kubectl get hpa nginx 
NAME    REFERENCE          TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
nginx   Deployment/nginx   <unknown>/60%   2         5         2          30m

而 HPA 資源字段主要包含,maxReplicasminReplicasscaleTargetReftargetCPUUtilizationPercentage 等。

kubectl get hpa nginx -o yaml
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  annotations:
    autoscaling.alpha.kubernetes.io/conditions: '[{"type":"AbleToScale","status":"True","lastTransitionTime":"2021-12-25T11:14:30Z","reason":"SucceededGetScale","message":"the
      HPA controller was able to get the target''s current scale"},{"type":"ScalingActive","status":"False","lastTransitionTime":"2021-12-25T11:14:30Z","reason":"FailedGetResourceMetric","message":"the       
      HPA was unable to compute the replica count: failed to get cpu utilization:
      unable to get metrics for resource cpu: unable to fetch metrics from resource
      metrics API: the server could not find the requested resource (get pods.metrics.k8s.io)"}]'
  creationTimestamp: "2021-12-25T11:14:15Z"
  name: nginx
  namespace: default
  resourceVersion: "48291"
  uid: d99a6f71-e29f-4390-8445-7d671b3a8514
spec:
  maxReplicas: 5
  minReplicas: 2
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: nginx
  targetCPUUtilizationPercentage: 60
status:
  currentReplicas: 2
  desiredReplicas: 0