跳至主要内容

選擇器和抓取設定

注意

Rancher-Istio 將在 Rancher v2.12.0 中被棄用;請改用 SUSE Rancher Application Collection 建置的 Istio 以獲得增強的安全性(包含在 SUSE Rancher Prime 訂閱中)。

詳細資訊可以在此公告中找到。

Monitoring 應用程式設定 prometheus.prometheusSpec.ignoreNamespaceSelectors=false,這預設會啟用跨所有命名空間的監控。

這確保您可以檢視在具有 istio-injection=enabled 標籤的命名空間中部署的資源的流量、指標和圖表。

如果您想將 Prometheus 限制為特定的命名空間,請設定 prometheus.prometheusSpec.ignoreNamespaceSelectors=true。一旦您這樣做,您必須執行一些額外的設定才能繼續監控您的資源。

透過將 ignoreNamespaceSelectors 設定為 True 來限制監控特定命名空間​

要將監控限制為特定的命名空間,您將編輯 ignoreNamespaceSelectors Helm chart 選項。您將在安裝或升級 Monitoring Helm chart 時設定此選項:

  1. 在安裝或升級 Monitoring Helm chart 時,編輯 values.yml 並設定 prometheus.prometheusSpec.ignoreNamespaceSelectors=true。
  2. 完成安裝或升級。

結果: Prometheus 將被限制為特定的命名空間,這意味著需要設定以下設定之一才能繼續在各種儀表板中檢視資料

讓 Prometheus 能夠偵測其他命名空間中的資源​

當 prometheus.prometheusSpec.ignoreNamespaceSelectors=true 時,有兩種不同的方法讓 Prometheus 能夠偵測其他命名空間中的資源:

  • 監控特定命名空間: 在包含您想要抓取的目標的命名空間中新增 Service Monitor 或 Pod Monitor。
  • 跨命名空間監控: 在您的 rancher-monitoring 實例中新增 additionalScrapeConfig,以抓取所有命名空間中的所有目標。

監控特定命名空間:建立 Service Monitor 或 Pod Monitor​

此選項允許您定義您希望在特定命名空間中監控哪些特定服務或 Pod。

可用性的權衡是您必須為每個命名空間建立 service monitor 或 pod monitor,因為您無法跨命名空間監控。

先決條件:

為 <your namespace> 定義 ServiceMonitor 或 PodMonitor。以下提供了一個範例 ServiceMonitor。

  1. 點擊 ☰ > Cluster Management。
  2. 前往您建立的叢集並點擊 Explore。
  3. 在頂部導航列中,開啟 kubectl shell。
  4. 如果 ServiceMonitor 或 PodMonitor 檔案儲存在您的叢集本地,執行 kubectl create -f <name of service/pod monitor file>.yaml。
  5. 如果 ServiceMonitor 或 PodMonitor 未儲存在本地,執行 cat<< EOF | kubectl apply -f -,將檔案內容貼到終端機,然後執行 EOF 來完成命令。
  6. 執行 kubectl label namespace <your namespace> istio-injection=enabled 以啟用 envoy sidecar 注入。

結果: <your namespace> 可以被 prometheus 抓取。

Istio Proxies 的 Service Monitor 範例
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: envoy-stats-monitor
namespace: istio-system
labels:
monitoring: istio-proxies
spec:
selector:
matchExpressions:
- {key: istio-prometheus-ignore, operator: DoesNotExist}
namespaceSelector:
any: true
jobLabel: envoy-stats
endpoints:
- path: /stats/prometheus
targetPort: 15090
interval: 15s
relabelings:
- sourceLabels: [__meta_kubernetes_pod_container_port_name]
action: keep
regex: '.*-envoy-prom'
- action: labeldrop
regex: "__meta_kubernetes_pod_label_(.+)"
- sourceLabels: [__meta_kubernetes_namespace]
action: replace
targetLabel: namespace
- sourceLabels: [__meta_kubernetes_pod_name]
action: replace
targetLabel: pod_name

跨命名空間監控:將 ignoreNamespaceSelectors 設定為 False​

這透過為 Prometheus 提供額外的抓取設定來啟用跨命名空間的監控。

可用性的權衡是 Prometheus 的所有 additionalScrapeConfigs 都在單一 Secret 中維護。如果在安裝 Istio 之前已使用 additionalScrapeConfigs 部署監控,這可能會使升級變得困難。

  1. 在安裝或升級 Monitoring Helm chart 時,編輯 values.yml 並將 prometheus.prometheusSpec.additionalScrapeConfigs 陣列設定為下面提供的 Additional Scrape Config。
  2. 完成安裝或升級。

結果: 所有具有 istio-injection=enabled 標籤的命名空間都將被 prometheus 抓取。

Additional Scrape Config
- job_name: 'istio/envoy-stats'
scrape_interval: 15s
metrics_path: /stats/prometheus
kubernetes_sd_configs:
- role: pod
relabel_configs:
- source_labels: [__meta_kubernetes_pod_container_port_name]
action: keep
regex: '.*-envoy-prom'
- source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
action: replace
regex: ([^:]+)(?::\d+)?;(\d+)
replacement: $1:15090
target_label: __address__
- action: labelmap
regex: __meta_kubernetes_pod_label_(.+)
- source_labels: [__meta_kubernetes_namespace]
action: replace
target_label: namespace
- source_labels: [__meta_kubernetes_pod_name]
action: replace
target_label: pod_name