K8s -- 通過自定義prometheus數(shù)據(jù)實(shí)現(xiàn)k8s hpa

轉(zhuǎn):https://blog.csdn.net/yevvzi/article/details/79561150

核心指標(biāo)管道

從 Kubernetes 1.8 開始,資源使用指標(biāo)(如容器 CPU 和內(nèi)存使用率)通過 Metrics API 在 Kubernetes 中獲取。
這些指標(biāo)可以直接被用戶訪問(例如通過使用 kubectl top 命令),或由集群中的控制器使用(例如,Horizontal Pod Autoscale
可以使用這些指標(biāo)作出決策)。

Resource Metrics API

通過 Metrics API,您可以獲取指定 node 或 pod 當(dāng)前使用的資源量。這個(gè) API 不存儲指標(biāo)值,
因此想要獲取某個(gè)指定 node 10 分鐘前的資源使用量是不可能的。

Metrics API 和其他的 API 沒有什么不同:

它可以通過與 /apis/metrics.k8s.io/ 路徑下的其他 Kubernetes API 相同的端點(diǎn)來發(fā)現(xiàn)
它提供了相同的安全性、可擴(kuò)展性和可靠性保證
Metrics API 在 k8s.io/metrics 倉庫中定義。您可以在這里找到關(guān)于Metrics API 的更多信息。

注意: Metrics API 需要在集群中部署 Metrics Server。否則它將不可用。

Metrics Server

Metrics Server 實(shí)現(xiàn)了Resource Metrics API

Metrics Server 是集群范圍資源使用數(shù)據(jù)的聚合器。
從 Kubernetes 1.8 開始,它作為一個(gè) Deployment 對象默認(rèn)部署在由 kube-up.sh 腳本創(chuàng)建的集群中。
如果您使用了其他的 Kubernetes 安裝方法,您可以使用 Kubernetes 1.7+ (請參閱下面的詳細(xì)信息)
中引入的 deployment yamls 文件來部署。

Metrics Server 從每個(gè)節(jié)點(diǎn)上的 Kubelet 公開的 Summary API 中采集指標(biāo)信息。

通過在主 API server 中注冊的 Metrics Server Kubernetes 聚合器 來采集指標(biāo)信息, 這是在 Kubernetes 1.7 中引入的。

在 設(shè)計(jì)文檔 中可以了解到有關(guān) Metrics Server 的更多信息。

custom metrics api

該API允許消費(fèi)者訪問任意度量描述Kubernetes資源。

API的目的是通過監(jiān)測管道供應(yīng)商,在其度量存儲解決方案之上實(shí)現(xiàn)。

如果你想實(shí)現(xiàn)這個(gè)API API服務(wù)器,請參閱kubernetes-incubator/custom-metrics-apiserver庫,其中包含需要建立這樣一個(gè)API服務(wù)器基礎(chǔ)設(shè)施
其中包含設(shè)置這樣一個(gè)API服務(wù)器所需的基本基礎(chǔ)設(shè)施。

Import Path: k8s.io/metrics/pkg/apis/custom_metrics.

custom metrics apiserver

custom metrics apiserver是為了實(shí)現(xiàn)k8s自定義監(jiān)控指標(biāo)
的框架。

HPA

自動(dòng)伸縮是一種根據(jù)資源使用情況自動(dòng)伸縮工作負(fù)載的方法。
自動(dòng)伸縮在Kubernetes中有兩個(gè)維度:cluster Autoscaler處理節(jié)點(diǎn)擴(kuò)容操作和Horizontal Pod Autoscaler自動(dòng)縮放rs或rc中的pod。
cluster Autoscaler和Horizontal Pod Autoscaler一起可用于動(dòng)態(tài)調(diào)整的計(jì)算能力以及并行性的水平,你的系統(tǒng)需要滿足sla。
雖然cluster Autoscaler高度依賴于托管集群的云提供商的底層功能,但是HPA可以獨(dú)立于您的IaaS/PaaS提供商進(jìn)行操作。

在Kubernetes v1.1中首次引入了hpa特性,自那時(shí)起已經(jīng)有了很大的發(fā)展。
hpa第一個(gè)版本基于觀察到的CPU利用率,后續(xù)版本支持基于內(nèi)存使用。
在Kubernetes 1.6中引入了一個(gè)新的API自定義指標(biāo)API,它允許HPA訪問任意指標(biāo)。
Kubernetes 1.7引入了聚合層,允許第三方應(yīng)用程序通過注冊為API附加組件來擴(kuò)展Kubernetes API。
自定義指標(biāo)API以及聚合層使得像Prometheus這樣的監(jiān)控系統(tǒng)可以向HPA控制器公開特定于應(yīng)用程序的指標(biāo)。

hpa 實(shí)現(xiàn)了一個(gè)控制環(huán),可以周期性的從資源指標(biāo)API查詢特定應(yīng)用的CPU/MEM信息。

image.png

實(shí)戰(zhàn)
以下是關(guān)于Kubernetes 1.9或更高版本的HPA v2配置的分步指南。您將安裝提供核心指標(biāo)的度量服務(wù)器附加組件,
然后您將使用一個(gè)演示應(yīng)用程序來展示基于CPU和內(nèi)存使用的pod自動(dòng)伸縮。在指南的第二部分,
您將部署Prometheus和一個(gè)自定義API服務(wù)器。您將使用聚合器層注冊自定義API服務(wù)器,然后使用演示應(yīng)用程序提供的自定義度量配置HPA。

前提

  • go 1.8+
  • clone k8s-prom-hpa repo
cd $GOPATH
git clone https://github.com/stefanprodan/k8s-prom-hpa

安裝 Metrics Server

Kubernetes Metrics Server是一個(gè)集群范圍的資源使用數(shù)據(jù)聚合器,是Heapster的繼承者。
metrics服務(wù)器通過從kubernet.summary_api收集數(shù)據(jù)收集節(jié)點(diǎn)和pod的CPU和內(nèi)存使用情況。
summary API是一個(gè)內(nèi)存有效的API,用于將數(shù)據(jù)從Kubelet/cAdvisor傳遞到metrics server。

image.png

如果在v1版本的HPA中,您將需要Heapster提供CPU和內(nèi)存指標(biāo),在HPA v2和Kubernetes 1.8中,
只有度量服務(wù)器是需要的,而水平-pod-autoscaler-use-rest-客戶機(jī)是打開的。
在Kubernetes 1.9中默認(rèn)啟用HPA rest客戶端。GKE 1.9附帶了預(yù)先安裝的指標(biāo)服務(wù)器。

kube-system命名空間總部署metrics-server

kubectl create -f ./metrics-server

一分鐘后,度量服務(wù)器開始報(bào)告節(jié)點(diǎn)和Pod的CPU和內(nèi)存使用情況。
查看nodes指標(biāo)

kubectl get --raw "/apis/metrics.k8s.io/v1beta1/nodes" | jq .

查看pod指標(biāo)

kubectl get --raw "/apis/metrics.k8s.io/v1beta1/pods" | jq .

基于CPU和內(nèi)存使用的自動(dòng)縮放

你將使用一個(gè)基于golang的小程序測試hpa.

部署podinfo到默認(rèn)命名空間

kubectl create -f ./podinfo/podinfo-svc.yaml,./podinfo/podinfo-dep.yaml

http://<K8S_PUBLIC_IP>:31198通過nodeport訪問podinfo

接下來定義一個(gè)HPA,保持最小兩個(gè)副本和最大十個(gè)如果CPU平均超過80%或如果內(nèi)存超過200mi。

apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: podinfo
spec:
  scaleTargetRef:
    apiVersion: extensions/v1beta1
    kind: Deployment
    name: podinfo
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      targetAverageUtilization: 80
  - type: Resource
    resource:
      name: memory
      targetAverageValue: 200Mi

創(chuàng)建HPA

kubectl create -f ./podinfo/podinfo-hpa.yaml

幾秒鐘之后,HPA控制器與metrics server聯(lián)系,然后取出CPU和內(nèi)存使用情況。

kubectl get hpa

NAME      REFERENCE            TARGETS                      MINPODS   MAXPODS   REPLICAS   AGE
podinfo   Deployment/podinfo   2826240 / 200Mi, 15% / 80%   2         10        2          5m

為了提高CPU使用率、運(yùn)行rakyll/hey進(jìn)行壓力測試

#install hey
go get -u github.com/rakyll/hey

#do 10K requests
hey -n 10000 -q 10 -c 5 http://<K8S_PUBLIC_IP>:31198/

你可以通過以下命令獲取HPA event

$ kubectl describe hpa

Events:
  Type    Reason             Age   From                       Message
  ----    ------             ----  ----                       -------
  Normal  SuccessfulRescale  7m    horizontal-pod-autoscaler  New size: 4; reason: cpu resource utilization (percentage of request) above target
  Normal  SuccessfulRescale  3m    horizontal-pod-autoscaler  New size: 8; reason: cpu resource utilization (percentage of request) above target

先將podinfo移除一會兒,稍后將再次部署:

kubectl delete -f ./podinfo/podinfo-hpa.yaml,./podinfo/podinfo-dep.yaml,./podinfo/podinfo-svc.yaml

安裝Custom Metrics Server
為了根據(jù)custom metrics進(jìn)行擴(kuò)展,您需要有兩個(gè)組件。一個(gè)從應(yīng)用程序中收集指標(biāo)并將其存儲為Prometheus時(shí)間序列數(shù)據(jù)庫的組件。
第二個(gè)組件將Kubernetes自定義指標(biāo)API擴(kuò)展到由收集的k8s-prometheus-adapter提供的指標(biāo)。

image.png

您將在專用命名空間中部署Prometheus和adapter。

創(chuàng)建monitoring命名空間

kubectl create -f ./namespaces.yaml

將 Prometheus v2部署到monitoring命名空間:
如果您部署到GKE,您可能會得到一個(gè)錯(cuò)誤:從服務(wù)器(禁止)中出錯(cuò):創(chuàng)建這個(gè)錯(cuò)誤將幫助您解決這個(gè)問題:RBAC on GKE。

kubectl create -f ./prometheus

生成由Prometheus adapter所需的TLS證書:

make certs

部署Prometheus自定義api適配器

kubectl create -f ./custom-metrics-api

列出由prometheus提供的自定義指標(biāo):

kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1" | jq .

獲取monitoring命名空間中所有pod的FS信息:

kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1/namespaces/monitoring/pods/*/fs_usage_bytes" | jq .

基于自定義指標(biāo)的自動(dòng)擴(kuò)容
創(chuàng)建podinfo nodeport服務(wù)并在default命名空間中部署:

kubectl create -f ./podinfo/podinfo-svc.yaml,./podinfo/podinfo-dep.yaml

podinfo應(yīng)用程序的暴露了一個(gè)自定義的度量http_requests_total。普羅米修斯適配器刪除_total后綴標(biāo)記度量作為一個(gè)計(jì)數(shù)器度量

從自定義度量API獲取每秒的總請求數(shù):

kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1/namespaces/default/pods/*/http_requests" | jq .
{
  "kind": "MetricValueList",
  "apiVersion": "custom.metrics.k8s.io/v1beta1",
  "metadata": {
    "selfLink": "/apis/custom.metrics.k8s.io/v1beta1/namespaces/default/pods/%2A/http_requests"
  },
  "items": [
    {
      "describedObject": {
        "kind": "Pod",
        "namespace": "default",
        "name": "podinfo-6b86c8ccc9-kv5g9",
        "apiVersion": "/__internal"
      },
      "metricName": "http_requests",
      "timestamp": "2018-01-10T16:49:07Z",
      "value": "901m"
    },
    {
      "describedObject": {
        "kind": "Pod",
        "namespace": "default",
        "name": "podinfo-6b86c8ccc9-nm7bl",
        "apiVersion": "/__internal"
      },
      "metricName": "http_requests",
      "timestamp": "2018-01-10T16:49:07Z",
      "value": "898m"
    }
  ]
}

m代表milli-units,例如,901m意味著milli-requests。

創(chuàng)建一個(gè)HPA,如果請求數(shù)超過每秒10當(dāng)將擴(kuò)大podinfo數(shù)量:

apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: podinfo
spec:
  scaleTargetRef:
    apiVersion: extensions/v1beta1
    kind: Deployment
    name: podinfo
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Pods
    pods:
      metricName: http_requests
      targetAverageValue: 10

在default命名空間部署podinfo HPA:

kubectl create -f ./podinfo/podinfo-hpa-custom.yaml

過幾秒鐘HPA從標(biāo)準(zhǔn)的API取得http_requests的值:

kubectl get hpa

NAME      REFERENCE            TARGETS     MINPODS   MAXPODS   REPLICAS   AGE
podinfo   Deployment/podinfo   899m / 10   2         10        2          1m

用25每秒請求數(shù)給podinfo服務(wù)加壓

#install hey
go get -u github.com/rakyll/hey

#do 10K requests rate limited at 25 QPS
hey -n 10000 -q 5 -c 5 http://<K8S-IP>:31198/healthz

幾分鐘后,HPA開始擴(kuò)大部署。

kubectl describe hpa

Name:                       podinfo
Namespace:                  default
Reference:                  Deployment/podinfo
Metrics:                    ( current / target )
  "http_requests" on pods:  9059m / 10
Min replicas:               2
Max replicas:               10

Events:
  Type    Reason             Age   From                       Message
  ----    ------             ----  ----                       -------
  Normal  SuccessfulRescale  2m    horizontal-pod-autoscaler  New size: 3; reason: pods metric http_requests above target

以每秒當(dāng)前的請求速率,部署將永遠(yuǎn)無法達(dá)到10個(gè)Pod的最大值。三副本足以讓RPS在10每pod.

負(fù)載測試結(jié)束后,HPA向下擴(kuò)展部署到初始副本。

Events:
  Type    Reason             Age   From                       Message
  ----    ------             ----  ----                       -------
  Normal  SuccessfulRescale  5m    horizontal-pod-autoscaler  New size: 3; reason: pods metric http_requests above target
  Normal  SuccessfulRescale  21s   horizontal-pod-autoscaler  New size: 2; reason: All metrics below target

你可能已經(jīng)注意到,自動(dòng)定標(biāo)器不使用峰值立即做出反應(yīng)。默認(rèn)情況下,指標(biāo)每30秒同步一次,
并且擴(kuò)展/收縮當(dāng)3-5分鐘沒有重新擴(kuò)展發(fā)生變化時(shí)。在這種方式中,HPA防止快速執(zhí)行并保留了指標(biāo)生效時(shí)間

總結(jié)

不是所有的系統(tǒng)都可以依靠CPU/內(nèi)存使用指標(biāo)單獨(dú)滿足SLA,大多數(shù)Web和移動(dòng)后端需要以每秒請求處理任何突發(fā)流量進(jìn)行自動(dòng)縮放。
對于ETL應(yīng)用程序,可能會由于作業(yè)隊(duì)列長度超過某個(gè)閾值而觸發(fā)自動(dòng)縮放,等等。
通過prometheus檢測你應(yīng)用程序的正確指,并為自動(dòng)是很所提供正確指標(biāo),您可以微調(diào)您的應(yīng)用程序更好地處理突發(fā)和確保高可用性。

參考

https://github.com/kubernetes/metrics

https://github.com/kubernetes-incubator/custom-metrics-apiserver

https://github.com/kubernetes-incubator/metrics-server

https://kubernetes.io/docs/tasks/debug-application-cluster/core-metrics-pipeline/

https://github.com/kubernetes/community/blob/master/contributors/design-proposals/instrumentation/resource-metrics-api.md

https://github.com/kubernetes/community/blob/master/contributors/design-proposals/instrumentation/metrics-server.md

https://github.com/stefanprodan/k8s-prom-hpa

https://github.com/DirectXMan12/k8s-prometheus-adapter

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容