簡體   English   中英

如何使用Istio的Prometheus配置kubernetes hpa?

[英]How to use Istio's Prometheus to configure kubernetes hpa?

我們有一個Istio集群,我們正在嘗試為Kubernetes配置水平容器自動縮放。 我們希望將請求計數用作hpa的自定義指標。 我們如何才能將Istio的Prometheus用於同一目的?

事實證明,這個問題比我預期的要復雜得多,但最終我得到了答案。

  1. 首先,您需要配置您的應用程序以提供自定義指標。 它在開發應用程序方面。 這是一個如何使用Go語言制作示例: 使用Prometheus觀看指標

  2. 其次,您需要定義應用程序部署(或Pod或任何您想要的部署)並將其部署到Kubernetes,例如:

     apiVersion: extensions/v1beta1 kind: Deployment metadata: name: podinfo spec: replicas: 2 template: metadata: labels: app: podinfo annotations: prometheus.io/scrape: 'true' spec: containers: - name: podinfod image: stefanprodan/podinfo:0.0.1 imagePullPolicy: Always command: - ./podinfo - -port=9898 - -logtostderr=true - -v=2 volumeMounts: - name: metadata mountPath: /etc/podinfod/metadata readOnly: true ports: - containerPort: 9898 protocol: TCP readinessProbe: httpGet: path: /readyz port: 9898 initialDelaySeconds: 1 periodSeconds: 2 failureThreshold: 1 livenessProbe: httpGet: path: /healthz port: 9898 initialDelaySeconds: 1 periodSeconds: 3 failureThreshold: 2 resources: requests: memory: "32Mi" cpu: "1m" limits: memory: "256Mi" cpu: "100m" volumes: - name: metadata downwardAPI: items: - path: "labels" fieldRef: fieldPath: metadata.labels - path: "annotations" fieldRef: fieldPath: metadata.annotations --- apiVersion: v1 kind: Service metadata: name: podinfo labels: app: podinfo spec: type: NodePort ports: - port: 9898 targetPort: 9898 nodePort: 31198 protocol: TCP selector: app: podinfo 

    注意字段annotations: prometheus.io/scrape: 'true' 需要請求Prometheus從資源中讀取指標。 另請注意,還有兩個其他注釋,它們具有默認值; 但是如果您在應用程序中更改了它們,則需要為它們添加正確的值:

    • prometheus.io/path :如果度量標准路徑不是/ metrics,則使用此注釋對其進行定義。
    • prometheus.io/port :在指示的端口上刮除Pod,而不是Pod的聲明的端口(如果未聲明,則默認為無端口目標)。
  3. 接下來,Istio中的Prometheus使用自己修改的用於Istio目的的配置,默認情況下,它會跳過Pod中的自定義指標。 因此,您需要對其進行一些修改。 就我而言,我從此示例中獲取Pod指標的配置,並僅針對Pod修改了Istio的Prometheus配置:

     kubectl edit configmap -n istio-system prometheus 

    我根據前面提到的示例更改了標簽的順序:

     # pod's declared ports (default is a port-free target if none are declared). - job_name: 'kubernetes-pods' # if you want to use metrics on jobs, set the below field to # true to prevent Prometheus from setting the `job` label # automatically. honor_labels: false kubernetes_sd_configs: - role: pod # skip verification so you can do HTTPS to pods tls_config: insecure_skip_verify: true # make sure your labels are in order relabel_configs: # these labels tell Prometheus to automatically attach source # pod and namespace information to each collected sample, so # that they'll be exposed in the custom metrics API automatically. - source_labels: [__meta_kubernetes_namespace] action: replace target_label: namespace - source_labels: [__meta_kubernetes_pod_name] action: replace target_label: pod # these labels tell Prometheus to look for # prometheus.io/{scrape,path,port} annotations to configure # how to scrape - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape] action: keep regex: true - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path] action: replace target_label: __metrics_path__ regex: (.+) - source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port] action: replace regex: ([^:]+)(?::\\d+)?;(\\d+) replacement: $1:$2 target_label: __address__ - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scheme] action: replace target_label: __scheme__ 

    此后,自定義指標出現在Prometheus中。 但是,請謹慎更改Prometheus配置 ,因為Istio所需的某些指標可能會消失,請仔細檢查所有內容。

  4. 現在該安裝Prometheus定制度量適配器了

    • 下載存儲庫
    • 在文件<repository-directory>/deploy/manifests/custom-metrics-apiserver-deployment.yaml更改Prometheus服務器的地址。 例如- --prometheus-url=http://prometheus.istio-system:9090/
    • 運行命令kubectl apply -f <repository-directory>/deploy/manifests一段時間后, custom.metrics.k8s.io/v1beta1 kubectl apply -f <repository-directory>/deploy/manifests custom.metrics.k8s.io/v1beta1應該出現在命令“ kubectl api-vesions”的輸出中。

    另外,使用命令kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1" | jq .檢查自定義API的輸出。 kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1" | jq . kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1/namespaces/default/pods/*/http_requests" | jq . 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" } ] } 

    如果是這樣,則可以轉到下一步。 如果不是,請在CustomMetrics kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1" | jq . | grep "pods/"查看可用於Pod的API的kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1" | jq . | grep "pods/" kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1" | jq . | grep "pods/" kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1" | jq . | grep "pods/"和http_requests kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1" | jq . | grep "http" kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1" | jq . | grep "http" kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1" | jq . | grep "http" MetricNames是根據Prometheus從Pods收集的度量標准生成的,如果它們為空,則需要朝該方向查看。

  5. 最后一步是配置HPA並對其進行測試。 因此,就我而言,我為podinfo應用程序創建了HPA,該應用程序之前已定義:

     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 

    並使用簡單的Go應用程序來測試負載:

     #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 

    一段時間后,我看到通過使用命令kubectl describe hpakubectl get hpa進行縮放的更改

我使用了使用Kubernetes Horizo​​ntal Pod Autoscaler和Prometheus確保高可用性和正常運行時間一文中有關創建自定義指標的說明

所有有用的鏈接都放在一個地方:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM