

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 監控
<a name="windows-monitoring"></a>

Prometheus，一個[漸進式 CNCF 專案](https://www.cncf.io/projects/)是到目前為止最熱門的監控系統，具有與 Kubernetes 的原生整合。Prometheus 會收集容器、Pod、節點和叢集的相關指標。此外，Prometheus 利用 AlertsManager，可讓您編寫警示程式，在叢集發生錯誤時警告您。Prometheus 會將指標資料儲存為由指標名稱和索引鍵/值對識別的時間序列資料。Prometheus 包括離開使用稱為 PromQL 的語言進行查詢，Prometheus 查詢語言很短。

Prometheus 指標集合的高階架構如下所示：

![\[Prometheus 指標集合\]](http://docs.aws.amazon.com/zh_tw/eks/latest/best-practices/images/windows/prom.png)


Prometheus 使用提取機制，並使用匯出工具從目標抓取指標，並使用 [kube 狀態指標從 Kubernetes API 抓取指標](https://github.com/kubernetes/kube-state-metrics)。這表示應用程式和服務必須公開包含 Prometheus 格式指標的 HTTP(S) 端點。然後，Prometheus 會根據其組態，定期從這些 HTTP(S) 端點提取指標。

匯出工具可讓您使用第三方指標做為 Prometheus 格式指標。Prometheus 匯出工具通常部署在每個節點上。如需匯出者的完整清單，請參閱 Prometheus [匯出者](https://prometheus.io/docs/instrumenting/exporters/)。雖然[節點匯出器](https://github.com/prometheus/node_exporter)適用於匯出 Linux 節點的主機硬體和作業系統指標，但不適用於 Windows 節點。

當您使用穩定的 [Prometheus Helm Chart](https://github.com/prometheus-community/helm-charts) 時，在**具有 Windows 節點的混合節點 EKS 叢集**中，您會在 Windows 節點上看到失敗的 Pod，因為此匯出工具不適用於 Windows。您需要分開處理 Windows 工作者集區，而是在 [Windows 工作者節點群組上安裝 Windows 匯出工具](https://github.com/prometheus-community/windows_exporter)。

若要設定 Windows 節點的 Prometheus 監控，您需要在 Windows 伺服器本身下載並安裝 WMI 匯出工具，然後在 Prometheus 組態檔案的湊集組態內設定目標。[版本頁面](https://github.com/prometheus-community/windows_exporter/releases)提供所有可用的 .msi 安裝程式，以及個別的功能集和錯誤修正。安裝程式會將 windows\$1exporter 設定為 Windows 服務，並在 Windows 防火牆中建立例外狀況。如果在沒有任何參數的情況下執行安裝程式，匯出程式將以已啟用收集器、連接埠等的預設設定執行。

您可以查看本指南的**排程最佳實務**章節，其中建議使用污點/容錯或 RuntimeClass 來選擇性地將節點匯出器僅部署到 linux 節點，而 Windows 匯出器在您引導節點或使用您選擇的組態管理工具 （例如 Chef、Ansible、SSM 等） 時安裝在 Windows 節點上。

請注意，與節點匯出器安裝為協助程式集 的 linux 節點不同，在 Windows 節點上，WMI 匯出器會安裝在主機本身。匯出者將匯出指標，例如 CPU 用量、記憶體和磁碟 I/O 用量，也可以用來監控 IIS 網站和應用程式、網路介面和服務。

Windows\$1exporter 預設會公開已啟用收集器的所有指標。這是收集指標以避免錯誤的推薦方法。不過，對於進階使用，Windows\$1exporter 可以傳遞選用的收集器清單來篩選指標。在 Prometheus 組態中的 collect【】 參數可讓您執行此操作。

Windows 的預設安裝步驟包括在引導程序期間使用引數下載和啟動匯出程式做為服務，例如您要篩選的收集器。

```
> Powershell Invoke-WebRequest https://github.com/prometheus-community/windows_exporter/releases/download/v0.13.0/windows_exporter-0.13.0-amd64.msi -OutFile <DOWNLOADPATH>

> msiexec /i <DOWNLOADPATH> ENABLED_COLLECTORS="cpu,cs,logical_disk,net,os,system,container,memory"
```

根據預設，指標可以在連接埠 9182 的 /metrics 端點上抓取。此時，Prometheus 可以透過將下列 scrape\$1config 新增至 Prometheus 組態來取用指標

```
scrape_configs:
    - job_name: "prometheus"
      static_configs:
        - targets: ['localhost:9090']
    ...
    - job_name: "wmi_exporter"
      scrape_interval: 10s
      static_configs:
        - targets: ['<windows-node1-ip>:9182', '<windows-node2-ip>:9182', ...]
```

使用 重新載入 Prometheus 組態

```
> ps aux | grep prometheus
> kill HUP <PID>
```

新增目標的更好建議方式是使用稱為 ServiceMonitor 的自訂資源定義，該定義是做為 [Prometheus 運算子](https://github.com/prometheus-operator/kube-prometheus/releases)的一部分】，為 ServiceMonitor 物件和控制器提供定義，以啟用我們定義的 ServiceMonitors，並自動建置所需的 Prometheus 組態。

ServiceMonitor 會明確指定應如何監控 Kubernetes 服務群組，用來定義您希望從 Kubernetes 內抓取指標的應用程式。在 ServiceMonitor 中，我們指定 Kubernetes 標籤，讓操作員用來識別 Kubernetes 服務，進而識別我們想要監控的 Pod。

為了利用 ServiceMonitor，請為 Windows 節點建立指向特定 Windows 目標的端點物件、無周邊服務和 ServiceMontor。

```
apiVersion: v1
kind: Endpoints
metadata:
  labels:
    k8s-app: wmiexporter
  name: wmiexporter
  namespace: kube-system
subsets:
- addresses:
  - ip: NODE-ONE-IP
    targetRef:
      kind: Node
      name: NODE-ONE-NAME
  - ip: NODE-TWO-IP
    targetRef:
      kind: Node
      name: NODE-TWO-NAME
  - ip: NODE-THREE-IP
    targetRef:
      kind: Node
      name: NODE-THREE-NAME
  ports:
  - name: http-metrics
    port: 9182
    protocol: TCP

---
apiVersion: v1
kind: Service ##Headless Service
metadata:
  labels:
    k8s-app: wmiexporter
  name: wmiexporter
  namespace: kube-system
spec:
  clusterIP: None
  ports:
  - name: http-metrics
    port: 9182
    protocol: TCP
    targetPort: 9182
  sessionAffinity: None
  type: ClusterIP

---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor ##Custom ServiceMonitor Object
metadata:
  labels:
    k8s-app: wmiexporter
  name: wmiexporter
  namespace: monitoring
spec:
  endpoints:
  - interval: 30s
    port: http-metrics
  jobLabel: k8s-app
  namespaceSelector:
    matchNames:
    - kube-system
  selector:
    matchLabels:
      k8s-app: wmiexporter
```

如需 運算子和 ServiceMonitor 使用方式的詳細資訊，請參閱官方[運算子](https://github.com/prometheus-operator/kube-prometheus)文件。請注意，Prometheus 確實支援使用許多[服務探索選項的動態目標探索](https://prometheus.io/blog/2015/06/01/advanced-service-discovery/)。