終於成功部署 Kubernetes HPA 基於 QPS 進行自動伸縮

昨天晚上經過壓測驗證了 HPA 部署成功了。html

所使用的 HPA 配置文件以下:git

apiVersion: autoscaling/v2beta2 
kind: HorizontalPodAutoscaler
metadata: 
  name: blog-web
spec: 
  scaleTargetRef: 
    apiVersion: apps/v1 
    kind: Deployment 
    name: blog-web
  minReplicas: 2
  maxReplicas: 8 
  metrics:
  - type: Pods
    pods:
      metric:
        name: http_requests_received
      target:
        type: AverageValue
        averageValue: 10

最小 pod 副本數是 2 ,最大 pod 副本數是 8 ,基於 http_requests_received 指標(對應的就是 QPS )進行伸縮,當指標平均值高於 10 時,自動進行擴容。github

使用下面的壓測命令發起了 100 個併發請求。web

hey -c 100 -z 5m http://hostname

隨後 HPA 自動將對應的 pod 副本由 2 個擴容至 8 個。docker

NAME       REFERENCE             TARGETS     MINPODS   MAXPODS   REPLICAS   AGE
blog-web   Deployment/blog-web   10253m/10   2         8         8          4d23h

上面的 http_requests_received 指標是 HPA 經過 custom-metrics-apiserver 獲取到的(這個指標名稱是由提供指標數據的應用決定的)。api

# kubectl get apiservices | grep custom-metrics
v1beta1.custom.metrics.k8s.io          monitoring/custom-metrics-apiserver   True        5d16h

custom-metrics-apiserver 是一個 extension API server ,用於提供 custom metrics ,它是由 k8s-prometheus-adapter 部署的,用於從 prometheus 中獲取監控指標數據,能夠經過下面的命令手動請求這個 api 。bash

kubectl get --raw /apis/custom.metrics.k8s.io/v1beta1/namespaces/production/pods/*/http_requests_received | jq .

http_requests_received 指標數據是 prometheus 經過 ServiceMonitor 發現對應的 target (這裏是 pod )並請求 /metrics 抓取 http_requests_received_total 指標數據根據時間計算出來的。併發

http_requests_received_total 指標數據是由 pod 中應用的 exporter 組件經過 /metrics 提供的,咱們的應用基於 asp.net core ,exporter 組件選用的是 prometheus-netapp

$ docker exec -t $(docker ps -f name=blog-web_blog-web -q | head -1) curl 127.0.0.1/metrics | grep http_requests_received_total
# HELP http_requests_received_total Provides the count of HTTP requests that have been processed by the ASP.NET Core pipeline.
# TYPE http_requests_received_total counter
http_requests_received_total{code="200",method="GET",controller="BlogMvc",action="CommentForm"} 5
http_requests_received_total{code="200",method="GET",controller="AggSite",action="SiteHome"} 1966

一圖勝千言(圖片來源):
asp.net

相關連接:

相關文章
相關標籤/搜索