k8s中使用prometheus operator監控外部服務器部署的windows exporter
0、文檔說明
(1)Prometheus Operator是一個流行的k8s集羣監控套件,項目地址:https://github.com/prometheus-operator/kube-prometheus,在參考文檔部署前,請先確保在k8s環境中已經部署成功Prometheus Operator組件
(2)本文檔通過 ServiceMonitor 方式部署監控信息,至於其他方式的部署,這裏不做說明
(4)Windows Exporter 由 Prometheus Community 維護的一個採集 Windows 機器指標的採集器,支持 Windows Server 2008R2 以上版本或者 Windows 7 以上版本,官方地址是:https://github.com/prometheus-community/windows_exporter,當前版本是 v0.20.0。
(5)在參考本文檔之前,請先確保外部服務器已經安裝成功 windows exporter,關於windows exporter的安裝,這裏不再說明,windows exporter的下載地址如下:https://github.com/prometheus-community/windows_exporter/releases
(6)版本說明
資源名稱 | 版本信息 | 說明 |
---|---|---|
Kubernetes | v1.23.7 | k8s服務器 |
Kuboard | v3.5.2.0 | k8s連接管理工具 |
windows exporter | v0.20.0 | windows exporter版本 |
prometheus operator | v0.43.2 | prometheus operator版本 |
Grafana | v9.0.2 | Grafana版本 |
1、部署操作步驟
1.1、外部服務器安裝windows exporter
(1)Windows Exporter 在發佈的時候提供了兩種格式的文件,即 *.exe 和 *.msi 。MSI (Microsoft Installers)是 Windows 的包管理器,類似於 Linux 的 rpm。Windows Exporter 每個版本都提供一個 .msi 安裝程序。安裝程序將 windows_exporter 設置爲 Windows 服務,並在 Windows 防火牆中創建一個異常。我們在使用的時候可以選擇任意一種格式來進行安裝。下載地址如下:https://github.com/prometheus-community/windows_exporter/releases
(2)關於windows exporter的安裝,這裏不再對此說明,可以參考如下安裝:https://help.aliyun.com/document_detail/463494.html
1.2、在k8s中創建Service
在k8s容器中需要爲外部服務器上安裝的windows exporter定義一個不需要Pod的Service,從而將k8s與外界服務器建立聯繫。外部服務器的服務定義與k8s中普通服務定義非常相似,對應的yml文件如下:
---
apiVersion: v1
kind: Service
metadata:
name: windows-exporter-157
namespace: prometheus-exporter
labels:
app: windows-exporter-157
app.kubernetes.io/name: windows-exporter-157
spec:
clusterIP: None
ports:
- name: metrics
port: 9182
protocol: TCP
targetPort: 9182
說明:
(1)windows_exporter默認端口是9182,這裏配置的端口號和外部服務器安裝的windows exporter保持一致
(2)這裏部署的Service下沒有關聯pod,僅是一個單獨的service,需要建立endpoints將其外部服務與service建立關聯
1.3、爲創建的Service建立endpoints
---
apiVersion: v1
kind: Endpoints
metadata:
name: windows-exporter-157
namespace: prometheus-exporter
labels:
app: windows-exporter-157
app.kubernetes.io/name: windows-exporter-157
subsets:
- addresses:
# 這裏是外部資源列表
- ip: 10.1.121.157
ports:
- name: metrics
port: 9182
說明:
(1)Service名稱必須和Endpoints名稱一致才能使Service指向對應的資源
(2)通過採用IP指定的方式,將endpoints與外部資源關聯起來
1.4、在k8s中創建ServiceMonitor
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: windows-exporter-157
namespace: prometheus-exporter
labels:
app: windows-exporter-157
release: prometheus
spec:
selector:
matchLabels:
app: windows-exporter-157
namespaceSelector:
matchNames:
- prometheus-exporter
endpoints:
- port: metrics
interval: 1m
path: /metrics
scheme: http
說明:
(1)服務創建完成需要配置一個ServiceMonitor來監控服務監控採集地址,默認情況下監控採集地址爲 /metrics
(2)Service Monitor中endpoints配置部分的port名稱與之前部署的endpoints保持一致
1.5、在k8s中查看監控數據
1.5.1、Prometheus Targets
1.5.2、Grafana dashboard
Grafana dashboard中導入的看板ID爲:10467,grafana模板地址如下:https://grafana.com/grafana/dashboards/,windows exporter對應的監控指標以官方文檔爲準,因爲在導入模板進行數據展示的時候,發現有些監控指標已經更改了名稱,當然監控指標也可以在上述內容部署成功之後,通過prometheus的可視化界面查詢得到當前監控的指標數據(官方文檔地址:https://github.com/prometheus-community/windows_exporter/blob/master/docs/README.md),所以針對監控指標表示的意思這裏不再進行說明,感興趣的小夥伴可以參考官方說明。