Prometheus

Prometheus 是一個開源監控系統和時間序列資料庫。您可以將 Prometheus 與 Istio 搭配使用,以記錄追蹤 Istio 健康狀況以及服務網格中應用程式的指標。您可以使用 GrafanaKiali 等工具將指標視覺化。

安裝

選項 1:快速開始

Istio 提供一個基本的範例安裝,以便快速啟動並執行 Prometheus

$ kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.24/samples/addons/prometheus.yaml

這會將 Prometheus 部署到您的叢集中。這僅用於示範目的,並未針對效能或安全性進行調整。

選項 2:可自訂安裝

請參考 Prometheus 文件開始將 Prometheus 部署到您的環境中。請參閱組態設定,以取得有關設定 Prometheus 來抓取 Istio 部署的更多資訊。

設定

在 Istio 網格中,每個元件都會公開一個發射指標的端點。Prometheus 的運作方式是抓取這些端點並收集結果。這是透過 Prometheus 組態檔案進行設定的,該檔案控制要查詢哪些端點、查詢的埠和路徑、TLS 設定等。

若要收集整個網格的指標,請設定 Prometheus 來抓取

  1. 控制平面 (istiod 部署)
  2. 入口和出口閘道
  3. Envoy sidecar
  4. 使用者應用程式 (如果它們公開 Prometheus 指標)

為了簡化指標的設定,Istio 提供兩種操作模式。

選項 1:指標合併

為了簡化設定,Istio 可以完全透過 prometheus.io 註釋來控制抓取。這使得 Istio 抓取可以與標準組態 (例如 Helm stable/prometheus 圖表提供的組態) 開箱即用。

此選項預設為啟用,但在 安裝期間傳遞 --set meshConfig.enablePrometheusMerge=false 可以停用它。啟用後,適當的 prometheus.io 註釋將會新增到所有資料平面 Pod,以設定抓取。如果這些註釋已存在,它們將會被覆寫。使用此選項,Envoy sidecar 會將 Istio 的指標與應用程式指標合併。合併的指標將從 :15020/stats/prometheus 抓取。

此選項會以純文字公開所有指標。

在下列情況下,此功能可能不符合您的需求

  • 您需要使用 TLS 抓取指標。
  • 您的應用程式公開的指標名稱與 Istio 指標相同。例如,您的應用程式指標公開 istio_requests_total 指標。如果應用程式本身正在執行 Envoy,可能會發生這種情況。
  • 您的 Prometheus 部署未設定為根據標準的 prometheus.io 註釋進行抓取。

如果需要,可以透過在 Pod 上新增 prometheus.istio.io/merge-metrics: "false" 註釋,來停用每個工作負載的此功能。

選項 2:自訂抓取配置

若要設定現有的 Prometheus 實例來抓取 Istio 產生的統計資料,需要新增幾個作業。

  • 若要抓取 Istiod 統計資料,可以新增以下範例作業來抓取其 http-monitoring
- job_name: 'istiod'
  kubernetes_sd_configs:
  - role: endpoints
    namespaces:
      names:
      - istio-system
  relabel_configs:
  - source_labels: [__meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name]
    action: keep
    regex: istiod;http-monitoring
  • 若要抓取 Envoy 統計資料,包括 sidecar 代理和閘道代理,可以新增以下作業來抓取以 -envoy-prom 結尾的埠
- job_name: 'envoy-stats'
  metrics_path: /stats/prometheus
  kubernetes_sd_configs:
  - role: pod

  relabel_configs:
  - source_labels: [__meta_kubernetes_pod_container_port_name]
    action: keep
    regex: '.*-envoy-prom'

TLS 設定

控制平面、閘道和 Envoy sidecar 指標都將透過明文進行抓取。但是,應用程式指標將遵循為工作負載設定的任何Istio 驗證原則

  • 如果您使用 STRICT 模式,則需要設定 Prometheus 使用 Istio 憑證進行抓取,如下所述。
  • 如果您使用 PERMISSIVE 模式,則工作負載通常會接受 TLS 和明文。但是,Prometheus 無法傳送 Istio 的 PERMISSIVE 模式所需的特殊 TLS 變體。因此,您必須不要在 Prometheus 中設定 TLS。
  • 如果您使用 DISABLE 模式,則 Prometheus 不需要 TLS 組態。

為 Prometheus 佈建 Istio 憑證的一種方法是注入一個 sidecar,它將輪換 SDS 憑證並將其輸出到可以與 Prometheus 共用的磁碟區。但是,sidecar 不應攔截 Prometheus 的請求,因為 Prometheus 的直接端點存取模型與 Istio 的 sidecar 代理模型不相容。

若要達成此目的,請在 Prometheus 伺服器容器上設定憑證磁碟區掛載

containers:
  - name: prometheus-server
    ...
    volumeMounts:
      mountPath: /etc/prom-certs/
      name: istio-certs
volumes:
  - emptyDir:
      medium: Memory
    name: istio-certs

然後將以下註釋新增至 Prometheus 部署 Pod 範本,並使用sidecar 注入來部署它。這會將 sidecar 設定為將憑證寫入共用磁碟區,但不會設定流量重新導向

spec:
  template:
    metadata:
      annotations:
        traffic.sidecar.istio.io/includeInboundPorts: ""   # do not intercept any inbound ports
        traffic.sidecar.istio.io/includeOutboundIPRanges: ""  # do not intercept any outbound traffic
        proxy.istio.io/config: |  # configure an env variable `OUTPUT_CERTS` to write certificates to the given folder
          proxyMetadata:
            OUTPUT_CERTS: /etc/istio-output-certs
        sidecar.istio.io/userVolumeMount: '[{"name": "istio-certs", "mountPath": "/etc/istio-output-certs"}]' # mount the shared volume at sidecar proxy

最後,將抓取作業的 TLS 環境設定如下

scheme: https
tls_config:
  ca_file: /etc/prom-certs/root-cert.pem
  cert_file: /etc/prom-certs/cert-chain.pem
  key_file: /etc/prom-certs/key.pem
  insecure_skip_verify: true  # Prometheus does not support Istio security naming, thus skip verifying target pod certificate

最佳實務

對於較大的網格,進階設定可能有助於 Prometheus 擴展。請參閱將 Prometheus 用於生產規模監控以取得更多資訊。

此資訊是否有用?
您是否有任何改進建議?

感謝您的回饋!