可觀測性最佳實務

使用 Prometheus 進行生產規模監控

針對使用 Prometheus 的 Istio 網格進行生產規模監控,建議的方法是使用階層式聯盟(hierarchical federation),並結合一系列的記錄規則(recording rules)

雖然安裝 Istio 預設不會部署 Prometheus,但入門指南中會安裝Prometheus 整合指南所述的 選項 1:快速開始 Prometheus 部署。此 Prometheus 部署特意配置了非常短的保留時間(6 小時)。快速開始的 Prometheus 部署也設定為從網格中運行的每個 Envoy 代理收集指標,並使用一組關於其來源的標籤(instancepodnamespace)來擴充每個指標。

Architecture for production monitoring of Istio using Prometheus.
使用 Istio 進行生產規模的 Istio 監控

透過記錄規則進行工作負載層級彙總

為了聚合跨實例和 Pod 的指標,請使用以下記錄規則更新預設的 Prometheus 設定。

groups:
- name: "istio.recording-rules"
  interval: 5s
  rules:
  - record: "workload:istio_requests_total"
    expr: |
      sum without(instance, kubernetes_namespace, kubernetes_pod_name) (istio_requests_total)

  - record: "workload:istio_request_duration_milliseconds_count"
    expr: |
      sum without(instance, kubernetes_namespace, kubernetes_pod_name) (istio_request_duration_milliseconds_count)

  - record: "workload:istio_request_duration_milliseconds_sum"
    expr: |
      sum without(instance, kubernetes_namespace, kubernetes_pod_name) (istio_request_duration_milliseconds_sum)

  - record: "workload:istio_request_duration_milliseconds_bucket"
    expr: |
      sum without(instance, kubernetes_namespace, kubernetes_pod_name) (istio_request_duration_milliseconds_bucket)

  - record: "workload:istio_request_bytes_count"
    expr: |
      sum without(instance, kubernetes_namespace, kubernetes_pod_name) (istio_request_bytes_count)

  - record: "workload:istio_request_bytes_sum"
    expr: |
      sum without(instance, kubernetes_namespace, kubernetes_pod_name) (istio_request_bytes_sum)

  - record: "workload:istio_request_bytes_bucket"
    expr: |
      sum without(instance, kubernetes_namespace, kubernetes_pod_name) (istio_request_bytes_bucket)

  - record: "workload:istio_response_bytes_count"
    expr: |
      sum without(instance, kubernetes_namespace, kubernetes_pod_name) (istio_response_bytes_count)

  - record: "workload:istio_response_bytes_sum"
    expr: |
      sum without(instance, kubernetes_namespace, kubernetes_pod_name) (istio_response_bytes_sum)

  - record: "workload:istio_response_bytes_bucket"
    expr: |
      sum without(instance, kubernetes_namespace, kubernetes_pod_name) (istio_response_bytes_bucket)

  - record: "workload:istio_tcp_sent_bytes_total"
    expr: |
      sum without(instance, kubernetes_namespace, kubernetes_pod_name) (istio_tcp_sent_bytes_total)

  - record: "workload:istio_tcp_received_bytes_total"
    expr: |
      sum without(instance, kubernetes_namespace, kubernetes_pod_name) (istio_tcp_received_bytes_total)

  - record: "workload:istio_tcp_connections_opened_total"
    expr: |
      sum without(instance, kubernetes_namespace, kubernetes_pod_name) (istio_tcp_connections_opened_total)

  - record: "workload:istio_tcp_connections_closed_total"
    expr: |
      sum without(instance, kubernetes_namespace, kubernetes_pod_name) (istio_tcp_connections_closed_total)

使用工作負載層級彙總指標進行聯合

要建立 Prometheus 聯盟,請修改您生產環境中 Prometheus 的設定,以抓取 Istio Prometheus 的聯盟端點。

將以下工作新增到您的設定中

- job_name: 'istio-prometheus'
  honor_labels: true
  metrics_path: '/federate'
  kubernetes_sd_configs:
  - role: pod
    namespaces:
      names: ['istio-system']
  metric_relabel_configs:
  - source_labels: [__name__]
    regex: 'workload:(.*)'
    target_label: __name__
    action: replace
  params:
    'match[]':
    - '{__name__=~"workload:(.*)"}'
    - '{__name__=~"pilot(.*)"}'

如果您使用 Prometheus Operator,請改用以下設定

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: istio-federation
  labels:
    app.kubernetes.io/name: istio-prometheus
spec:
  namespaceSelector:
    matchNames:
    - istio-system
  selector:
    matchLabels:
      app: prometheus
  endpoints:
  - interval: 30s
    scrapeTimeout: 30s
    params:
      'match[]':
      - '{__name__=~"workload:(.*)"}'
      - '{__name__=~"pilot(.*)"}'
    path: /federate
    targetPort: 9090
    honorLabels: true
    metricRelabelings:
    - sourceLabels: ["__name__"]
      regex: 'workload:(.*)'
      targetLabel: "__name__"
      action: replace

使用記錄規則最佳化指標收集

除了僅使用記錄規則來聚合 Pod 和實例之外,您可能還需要使用記錄規則來產生專為您現有儀表板和警報量身定制的聚合指標。以這種方式優化您的收集,除了更快的查詢效能之外,還可以大幅節省生產 Prometheus 實例中的資源消耗。

例如,假設有一個自訂監控儀表板使用了以下 Prometheus 查詢

  • 依目的地服務名稱和命名空間,過去一分鐘內的請求總速率平均值

    sum(irate(istio_requests_total{reporter="source"}[1m]))
    by (
        destination_canonical_service,
        destination_workload_namespace
    )
    
  • 依來源和目的地服務名稱和命名空間,過去一分鐘內的 P95 用戶端延遲平均值

    histogram_quantile(0.95,
      sum(irate(istio_request_duration_milliseconds_bucket{reporter="source"}[1m]))
      by (
        destination_canonical_service,
        destination_workload_namespace,
        source_canonical_service,
        source_workload_namespace,
        le
      )
    )
    

可以使用 istio 前綴將以下記錄規則集新增到 Istio Prometheus 設定中,以方便識別這些指標以進行聯盟。

groups:
- name: "istio.recording-rules"
  interval: 5s
  rules:
  - record: "istio:istio_requests:by_destination_service:rate1m"
    expr: |
      sum(irate(istio_requests_total{reporter="destination"}[1m]))
      by (
        destination_canonical_service,
        destination_workload_namespace
      )
  - record: "istio:istio_request_duration_milliseconds_bucket:p95:rate1m"
    expr: |
      histogram_quantile(0.95,
        sum(irate(istio_request_duration_milliseconds_bucket{reporter="source"}[1m]))
        by (
          destination_canonical_service,
          destination_workload_namespace,
          source_canonical_service,
          source_workload_namespace,
          le
        )
      )

然後,將更新生產 Prometheus 實例以從 Istio 實例聯合

  • 符合子句 {__name__=~"istio:(.*)"}

  • 具有以下值的指標重新標記設定: regex: "istio:(.*)"

然後,將原始查詢替換為

  • istio_requests:by_destination_service:rate1m

  • avg(istio_request_duration_milliseconds_bucket:p95:rate1m)

這些資訊對您有幫助嗎?
您有任何改進建議嗎?

感謝您的回饋!