Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
4.5 KiB
windows_exporter on Kubernetes
With Kubernetes supporting HostProcess containers on Windows nodes (as of v1.22, it is useful to run the windows_exporter as a container on Windows to export metrics for your Prometheus implementation. Read the Kubernetes HostProcess documentation for more information.
Requirements:
- Kubernetes 1.22+
- containerd 1.6 Beta+
- WindowsHostProcessContainers feature-gate turned on for
kube-apiserverandkubelet
IMPORTANT: This does not work unless you are specifically targeting Host Process Containers with Containerd (Docker doesn't have support). The image will build but will not be able to access the host.
Container Image
The image is a HostProcess container image based on mcr.microsoft.com/oss/kubernetes/windows-host-process-containers-base-image. One image works on every Windows version that supports HostProcess containers. The released images are listed in the Docker section of the readme.
To build your own image, use docker buildx on Linux, for example in WSL. The image only copies the binary, so it builds on Linux although it's a Windows image. Put the amd64 binary into an otherwise empty directory as windows_exporter-<version>-amd64.exe, the name the CI build uses, and build the image from that directory:
GOOS=windows GOARCH=amd64 go build -o output/windows_exporter-dev-amd64.exe ./cmd/windows_exporter
docker buildx build --platform windows/amd64 -f Dockerfile -t <your repository>/windows-exporter:dev --push output
Kubernetes Quick Start
Before beginning you need to deploy the Prometheus Operator to your cluster. As a quick start, you can use a project like https://github.com/prometheus-operator/kube-prometheus. The export itself doesn't have any dependency on the Prometheus Operator and the exporter image can be used in manual configurations.
Windows Exporter DaemonSet
This creates a pod on every Windows node. A ConfigMap holds the configuration of the Windows exporter with configuration file. Adjust the configuration file for the collectors you are interested in.
kubectl apply -f kubernetes/windows-exporter-daemonset.yaml
Note: This example manifest deploys the latest bleeding edge image
ghcr.io/prometheus-community/windows-exporter:latestbuilt from the main branch. You should update this to use a released version which you can find at https://github.com/prometheus-community/windows_exporter/releases
Configuring the firewall
The firewall on the node needs to be configured to allow connections on the node: New-NetFirewallRule -DisplayName 'windows-exporter' -Direction inbound -Profile Any -Action Allow -LocalPort 9182 -Protocol TCP
You could do this by adding an init container but if you remove the deployment at a later date you will need to remove the firewall rule manually. The following could be added to the windows-exporter-daemonset.yaml:
apiVersion: apps/v1
kind: DaemonSet
spec:
template:
spec:
initContainers:
- name: configure-firewall
image: mcr.microsoft.com/windows/powershell:lts-nanoserver-1809
command: ["powershell"]
args: ["New-NetFirewallRule", "-DisplayName", "'windows-exporter'", "-Direction", "inbound", "-Profile", "Any", "-Action", "Allow", "-LocalPort", "9182", "-Protocol", "TCP"]
Prometheus PodMonitor
Create the Pod Monitor to configure the scraping:
kubectl apply -f kubernetes/windows-exporter-podmonitor.yaml
View Metrics
Open Prometheus with
kubectl --namespace monitoring port-forward svc/prometheus-k8s 9091:9090
Navigate to the Prometheus UI and add a query to see the node CPU (replace the IP address with yours)
sum by (mode) (irate(windows_cpu_time_total{instance="10.1.0.5:9182"}[5m]))
Configuring TLS
It is possible to configure TLS of the solution using --web.config.file. Read more at https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md
