昨天晚上通過壓測驗證了 HPA 部署成功了。
所使用的 HPA 配置文件如下:
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 時,自動進行擴容。
使用下面的壓測命令發起了 100 個並發請求。
hey -c 100 -z 5m http://hostname
隨后 HPA 自動將對應的 pod 副本由 2 個擴容至 8 個。
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
獲取到的(這個指標名稱是由提供指標數據的應用決定的)。
# 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 。
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-net 。
$ 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
一圖勝千言(圖片來源):
相關鏈接: