- 云原生
- 可观测性
- 指标监控
- 监控大盘
- 告警
【免费下载链接】kube-prometheus
Use Prometheus to monitor Kubernetes and applications running on Kubernetes
导读
本文讲解如何在 kube-prometheus 集群监控栈中纳入 Windows 工作节点,核心是两条路线:基于 Kubernetes v1.22+ HostProcess 容器与 containerd 运行时的windows-hostprocessaddon(部署 windows_exporter 到 Windows 节点内),以及面向 Docker 运行时的windowsaddon(利用 additional scrape configuration 静态抓取节点端口)。读完本文,你将掌握两种方案的 jsonnet 接入写法、可调参数与源码级工作原理,能够按自己的集群运行时环境选型并落地 Windows 节点监控。
前提条件与方案选型
kube-prometheus 的 Windows 监控能力由两个 addon 提供,选择依据是集群中 Windows 节点的容器运行时:
| 条件 | 推荐方案 | addon 文件 |
|---|---|---|
| Windows 节点使用containerd运行时,且 Kubernetes ≥ v1.22(支持 HostProcess 容器) | windows-hostprocess addon | addons/windows-hostprocess.libsonnet |
| Windows 节点使用Docker运行时 | windows addon | addons/windows.libsonnet |
两个 addon 都会引入 kubernetes-mixin 中的 Windows 面板(dashboards)与告警规则(rules),但数据采集方式完全不同:
- containerd 方案:以 HostProcess 形式在 Windows 节点内运行 windows_exporter 容器,由 kube-prometheus 生成 DaemonSet + ConfigMap + PodMonitor,Prometheus 直接发现并抓取;
- Docker 方案:Docker 化的 Windows 节点无法在 Pod 内运行 windows_exporter,因此该 addon不部署任何 exporter,而是通过 Prometheus 的 additional scrape configuration 生成静态抓取配置,指向你在节点上自行部署的 windows_exporter(通常监听 NodePort)。该 addon 只负责把规则、面板和抓取配置注入到 Prometheus 中,节点 IP 与端口需要你显式提供。
补充:文档所引用的官方说明中,windows_exporter 的 kubernetes 部署方式基于 containerd 运行时;如果你正以 Docker 运行时运行 Windows 节点,请走第二种方案。
方案一:基于 HostProcess 容器的 windows-hostprocess addon
工作原理
windows-hostprocess.libsonnet会生成三类资源(从 addons/windows-hostprocess.libsonnet 源码可见):
DaemonSet(kind: DaemonSet):在每个 Windows 节点上运行 windows_exporter 容器。关键点包括:
securityContext.windowsOptions.hostProcess: true且runAsUserName: 'NT AUTHORITY\\system',即以系统权限运行 HostProcess 容器(addons/windows-hostprocess.libsonnet);hostNetwork: true直接使用宿主机网络,端口hostPort与containerPort均为配置端口(默认 9182)(addons/windows-hostprocess.libsonnet);- 通过 initContainer
configure-firewall调用 PowerShell 的New-NetFirewallRule在节点防火墙放行 TCP 入站端口(默认 9182)(addons/windows-hostprocess.libsonnet); nodeSelector: {'kubernetes.io/os': 'windows'}只调度到 Windows 节点,并带os=windows:NoSchedule容忍(addons/windows-hostprocess.libsonnet);- 更新策略为 RollingUpdate,
maxUnavailable: 10%(addons/windows-hostprocess.libsonnet)。
ConfigMap:向容器注入
config.yml,内容为collectors.enabled,默认启用cpu,logical_disk,net,os,system,container,memory采集器(addons/windows-hostprocess.libsonnet)。容器启动参数使用%CONTAINER_SANDBOX_MOUNT_POINT%引用该配置并设置 textfile 目录。PodMonitor:以
monitoring.coreos.com/v1定义抓取端点,interval默认 30s、scrapeTimeout默认 15s,并通过 relabeling 把__meta_kubernetes_pod_node_name写为instance标签(addons/windows-hostprocess.libsonnet)。
同时,addon 会把 mixin 的 Windows 面板注入 Grafana,把 Windows 规则注入 PrometheusRule,并通过windowsExporterSelector: 'job="windows-exporter"'将面板/规则与采集作业关联(addons/windows-hostprocess.libsonnet)。
最小接入 jsonnet
将以下内容写入你自己的 jsonnet 文件(完整可编译版本见 examples/windows-hostprocess.jsonnet):
local kp = (import 'kube-prometheus/main.libsonnet') + (import 'kube-prometheus/addons/windows-hostprocess.libsonnet') + { values+:: { windowsExporter+:: { image: "ghcr.io/prometheus-community/windows-exporter", version: "0.21.0", }, }, }; { ['windows-exporter-' + name]: kp.windowsExporter[name] for name in std.objectFields(kp.windowsExporter) }常用配置项
依据 addons/windows-hostprocess.libsonnet 的 defaults,可在values.windowsExporter中覆盖以下字段:
| 字段 | 默认值 | 说明 |
|---|---|---|
image | (必填,示例为ghcr.io/prometheus-community/windows-exporter) | windows_exporter 镜像 |
version | (必填,示例为0.21.0) | 镜像版本标签 |
namespace | values.common.namespace(monitoring) | 部署命名空间 |
resources | requests300m/200Mi,limits200Mi | 容器资源配额 |
collectorsEnabled | cpu,logical_disk,net,os,system,container,memory | 启用的采集器列表 |
scrapeTimeout | 15s | 抓取超时 |
interval | 30s | 抓取间隔 |
listenAddress | 127.0.0.1 | 监听地址 |
port | 9182 | 抓取端口(防火墙放行、containerPort、hostPort 均使用它) |
注意name默认固定为windows-exporter,面板与规则的job="windows-exporter"选择器依赖该值;如果修改,需同步调整 mixin 选择器。
方案二:面向 Docker 运行时的 windows addon
工作原理
windows.libsonnet(addons/windows.libsonnet)不产生 DaemonSet,而是:
- 定义一个名为
windows-exporter的抓取作业(job_name),其static_configs中的targets必须由你填写,格式为"<节点IP>:<端口>"(源码中默认targets: [error 'must provide targets array']强制要求提供,addons/windows.libsonnet); - 使用 relabel 规则把目标地址写为
instance标签(addons/windows.libsonnet); - 生成一个名为
prometheus-<name>-additional-scrape-config的 Secret,stringData['prometheus-additional.yaml']为渲染后的抓取配置(addons/windows.libsonnet); - 在 Prometheus 的
spec.additionalScrapeConfigs中引用该 Secret,实现 additional scrape configuration(addons/windows.libsonnet),并同样注入 Grafana 面板与 PrometheusRule,选择器为job="windows-exporter"。
因此该方案要求你:在 Windows 节点上自行部署 windows_exporter 并监听某端口,再把节点 IP 与端口填入targets。
最小接入 jsonnet
完整可编译版本见 examples/windows.jsonnet,核心片段:
local kp = (import 'kube-prometheus/main.libsonnet') + (import 'kube-prometheus/addons/windows.libsonnet') + { values+:: { windowsScrapeConfig+:: { static_configs: { targets: ["10.240.0.65:5000", "10.240.0.63:5000"], }, }, }, };把targets换成你的实际节点 IP 与 windows_exporter 监听端口即可。注意在完整示例中,static_configs是以数组形式给出的(static_configs: [{ targets: [...] }],examples/windows.jsonnet),本文档中演示的写法意在表达同样的语义,建议以完整示例的数组结构为准,以便后续可追加多组目标。
把 jsonnet 编译并部署到集群
两个 addon 都基于 kube-prometheus 的标准 jsonnet 工作流。以仓库自带的完整示例为例:
准备工具链:
jsonnet(go install github.com/google/go-jsonnet/cmd/jsonnet@latest)与gojsontoyaml(go install github.com/brancz/gojsontoyaml@latest);安装依赖:在项目目录执行
jb init与jb install github.com/prometheus-operator/kube-prometheus/jsonnet/kube-prometheus@main,生成vendor/(详见 docs/customizing.md);生成 manifests:用 jsonnet 渲染你的 Windows 接入文件并转成 YAML。以完整示例为模板时,
windows-hostprocess.jsonnet会额外输出windows-exporter-daemonset、windows-exporter-configmap、windows-exporter-podmonitor等文件;应用部署(参考 README.md 的 Quickstart):
kubectl apply --server-side -f manifests/setup kubectl wait --for condition=Established --all CustomResourceDefinition --namespace=monitoring kubectl apply -f manifests/其中
manifests/setup包含命名空间与 CRD,PodMonitor 依赖 CRD 就绪后再创建,因此分两步执行更稳妥。
验证与排障要点
- containerd 方案:确认 DaemonSet 已调度到 Windows 节点(
kubectl get ds -n monitoring),Pod 状态 Running,且防火墙 initContainer 执行成功;随后在 Prometheus 的 Targets 页面应能看到windows-exporter作业及其 PodMonitor 端点; - Docker 方案:确认附加抓取配置已生效(Prometheus 配置页
prometheus-additional.yaml),Targets 页面出现你填写的IP:端口;若出现context deadline exceeded,检查节点防火墙是否放行对应端口、windows_exporter 进程是否存活; - 面板与告警是否加载:Grafana 中应出现来自 kubernetes-mixin 的 Windows 面板,Prometheus 的 Rules 页面应包含 Windows 相关规则,二者均以
job="windows-exporter"作为指标选择依据。
小结
kube-prometheus 的 Windows 监控能力是"按运行时分流"的:containerd + HostProcess 方案体验最顺滑,addon 自动完成 DaemonSet、防火墙、配置注入与 PodMonitor 全链路;Docker 方案则退化为"静态抓取 + 面板规则",需要你自行维护 windows_exporter 进程并显式提供节点 IP/端口。选对 addon、填对参数,即可把 Windows 节点纳入现有 Prometheus 监控体系。相关可继续深入阅读的资料:windows-hostprocess 完整示例、windows 完整示例、kube-prometheus 自定义指南 以及仓库预编译的 manifests 目录。
- 云原生
- 可观测性
- 指标监控
- 监控大盘
- 告警
【免费下载链接】kube-prometheus
Use Prometheus to monitor Kubernetes and applications running on Kubernetes
相关推荐
hongyangWeixinArticles自定义View合集:20个炫酷UI效果实现原理
hongyangWeixinArticles自定义View合集:20个炫酷UI效果实现原理 hongyangWeixinArticles是一个记录微信公众号"h
如何使用Prometheus Operator监控Windows节点:跨平台监控完整指南
如何使用Prometheus Operator监控Windows节点:跨平台监控完整指南 Prometheus Operator是一款在Kubernetes上创
云原生可观测性终极指南:使用kube-prometheus监控NodeDiskStats节点磁盘统计指标
终极指南:使用kube prometheus监控NodeDiskStats节点磁盘统计指标 kube prometheus 是 Kubernetes 生态系统中
云原生可观测性指标监控监控大盘告警
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考