Day 12: GPU Monitoring
Goal: Collect the DCGM Exporter’s metrics with Prometheus, evaluate them with PromQL, display them in Grafana and build alerts from them. Along the way, check honestly what mapping GPUs to Pods under DRA achieves in the lab, and expose an alert that can never fire · Duration: approx. 90 min · Prerequisite: Days 7 and 8
Briefing
- The DCGM Exporter has been running as an operand of the GPU Operator since Day 7. It queries every GPU via DCGM (Data Center GPU Manager) for utilization, memory, temperature, power and error counters and serves the values on port 9400 in Prometheus format.
- Prometheus collects them, Grafana
displays them, PrometheusRules turn them into alerts.
Everything is installed with the
kube-prometheus-stackchart. - Honesty rule for today: The whole chain from exporter to alert is real software. The values themselves are simulated by Mokka. Utilization fluctuates randomly between about 10 and 45 percent; temperature, power and clock stay constant, and none of it depends on whether a Pod is “computing” on the GPU.
Mokka-GPUs ──DCGM──▶ DCGM Exporter :9400 ──ServiceMonitor──▶ Prometheus ──▶ Grafana
└──▶ PrometheusRule ──▶ Alert
Drill 1: What the Exporter Delivers
mkdir -p ~/gpu/tag12 && cd ~/gpu/tag12
kubectl -n gpu-operator port-forward svc/nvidia-dcgm-exporter 9400:9400 >/dev/null 2>&1 &
sleep 3
curl -s localhost:9400/metrics | grep -v '^#' | awk -F'{' '{print $1}' | sort | uniq -c
curl -s localhost:9400/metrics | grep '^DCGM_FI_DEV_GPU_UTIL' | head -1
kill %1Expected:
8 DCGM_FI_DEV_CORRECTABLE_REMAPPED_ROWS
8 DCGM_FI_DEV_DEC_UTIL
8 DCGM_FI_DEV_ENC_UTIL
8 DCGM_FI_DEV_FB_FREE
8 DCGM_FI_DEV_FB_RESERVED
8 DCGM_FI_DEV_FB_USED
8 DCGM_FI_DEV_GPU_TEMP
8 DCGM_FI_DEV_GPU_UTIL
8 DCGM_FI_DEV_MEMORY_TEMP
8 DCGM_FI_DEV_MEM_CLOCK
8 DCGM_FI_DEV_MEM_COPY_UTIL
8 DCGM_FI_DEV_NVLINK_BANDWIDTH_TOTAL
8 DCGM_FI_DEV_PCIE_REPLAY_COUNTER
8 DCGM_FI_DEV_POWER_USAGE
8 DCGM_FI_DEV_ROW_REMAP_FAILURE
8 DCGM_FI_DEV_SM_CLOCK
8 DCGM_FI_DEV_TOTAL_ENERGY_CONSUMPTION
8 DCGM_FI_DEV_UNCORRECTABLE_REMAPPED_ROWS
DCGM_FI_DEV_GPU_UTIL{gpu="0",UUID="GPU-12345678-1234-1234-1234-123456780000",pci_bus_id="00000000:07:00.0",device="nvidia0",modelName="NVIDIA A100-SXM4-40GB",Hostname="gpu-worker2",DCGM_FI_DRIVER_VERSION="580.65.06"} 38
18 metrics for each of the 8 GPUs, the 144 lines from Day 7. The most important ones:
| Metric | Meaning | Unit |
|---|---|---|
DCGM_FI_DEV_GPU_UTIL |
share of time the GPU is computing | % |
DCGM_FI_DEV_FB_USED, _FREE |
used and free GPU memory | MiB |
DCGM_FI_DEV_GPU_TEMP |
temperature | °C |
DCGM_FI_DEV_POWER_USAGE |
power draw | W |
DCGM_FI_DEV_*_REMAPPED_ROWS,
ROW_REMAP_FAILURE |
memory errors the GPU did or did not absorb with spare rows | count |
Every line carries UUID, model and node as labels. That way each measurement can be mapped to exactly one GPU, including a device from the ResourceSlice of Day 8.
Save your progress? Sign up for free
Drill 2: Prometheus and Grafana
A slim kube-prometheus-stack without Alertmanager and
Node Exporter. The two …NilUsesHelmValues: false settings
make Prometheus pick up ServiceMonitors and rules from all namespaces,
not only those with the chart’s label:
cd ~/gpu/tag12
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update prometheus-community
cat > monitoring-werte.yaml <<'EOF'
alertmanager:
enabled: false
nodeExporter:
enabled: false
prometheus:
prometheusSpec:
serviceMonitorSelectorNilUsesHelmValues: false
ruleSelectorNilUsesHelmValues: false
retention: 2d
grafana:
adminPassword: lab-grafana-123
EOF
helm install monitoring prometheus-community/kube-prometheus-stack --version 93.9.0 -n monitoring --create-namespace -f monitoring-werte.yaml --wait --timeout 600s >/dev/null
kubectl -n monitoring get podsExpected:
NAME READY STATUS RESTARTS AGE
monitoring-grafana-7bd85fb796-4vkjx 3/3 Running 0 2m25s
monitoring-kube-prometheus-operator-6b77dd6c5b-zkpf4 1/1 Running 0 2m25s
monitoring-kube-state-metrics-7d94c4cbd8-bdd2d 1/1 Running 0 2m25s
prometheus-monitoring-kube-prometheus-prometheus-0 2/2 Running 0 2m6s
Prometheus does not know about the DCGM Exporter yet. The GPU Operator creates the ServiceMonitor for it when the ClusterPolicy asks for it:
kubectl patch clusterpolicy cluster-policy --type=merge -p '{"spec":{"dcgmExporter":{"serviceMonitor":{"enabled":true,"interval":"15s"}}}}'
sleep 60
kubectl -n gpu-operator get servicemonitor
kubectl -n monitoring port-forward svc/monitoring-kube-prometheus-prometheus 9090:9090 >/dev/null 2>&1 &
sleep 3
curl -s localhost:9090/api/v1/targets | jq -r '.data.activeTargets[] | .labels.job + " " + .health' | sort | uniq -cExpected:
clusterpolicy.nvidia.com/cluster-policy patched
NAME AGE
gpu-operator 41s
nvidia-dcgm-exporter 39s
1 apiserver up
2 coredns up
1 gpu-operator up
1 kube-controller-manager down
1 kube-etcd down
3 kube-proxy down
1 kube-scheduler down
1 kube-state-metrics up
9 kubelet up
1 monitoring-grafana up
1 monitoring-kube-prometheus-operator up
2 monitoring-kube-prometheus-prometheus up
1 nvidia-dcgm-exporter up
nvidia-dcgm-exporter up. The down targets
are components of the kind control plane that only listen on
127.0.0.1: a quirk of kind that has nothing to do with
GPUs. The port-forward on 9090 stays open for the next drills.
Save your progress? Sign up for free
Drill 3: Questions with PromQL
A small helper function for the terminal so every query stays on one line:
q() { curl -s localhost:9090/api/v1/query --data-urlencode "query=$1" | jq -r '.data.result[] | ((.metric.gpu // .metric.modelName // "") + " " + .value[1])'; }
q 'DCGM_FI_DEV_GPU_UTIL'
q 'avg_over_time(DCGM_FI_DEV_GPU_UTIL[5m])' | head -3
q 'sum by (modelName) (DCGM_FI_DEV_FB_FREE) / 1024'
q 'sum(DCGM_FI_DEV_POWER_USAGE)'
q 'max(DCGM_FI_DEV_GPU_TEMP)'Expected (your utilization will differ):
0 28
1 26
2 26
3 42
4 23
5 22
6 43
7 45
0 25
1 20
2 27.5
NVIDIA A100-SXM4-40GB 315.4453125
576
33
| Question | PromQL |
|---|---|
| How busy is each GPU right now? | DCGM_FI_DEV_GPU_UTIL |
| And on average over the last 5 minutes? | avg_over_time(…[5m]) |
| How much memory is free per model? | sum by (modelName) (DCGM_FI_DEV_FB_FREE) / 1024
(GiB) |
| How much power does the node draw? | sum(DCGM_FI_DEV_POWER_USAGE) |
8 × 40 GiB, minus reserved areas, gives the 315 GiB of free memory. 576 W is 8 × 72 W, a plausible idle value for an A100, which Mokka sets as a fixed value.
Save your progress? Sign up for free
Drill 4: Which Pod Uses Which GPU?
For per-team chargeback, this is the most important question. The
exporter can answer it by asking the kubelet which Pod has which GPU
(Pod Resources API) and labeling the metrics with pod and
namespace. Since Day 8, however, DRA hands out the GPUs, no
longer the device plugin. There is a separate option for that:
P=$(kubectl -n gpu-operator get pod -l app=nvidia-dcgm-exporter -o name)
kubectl -n gpu-operator exec $P -- dcgm-exporter --help 2>&1 | grep -A1 'kubernetes-enable-dra'
kubectl patch clusterpolicy cluster-policy --type=merge -p '{"spec":{"dcgmExporter":{"env":[{"name":"KUBERNETES_ENABLE_DRA","value":"true"}]}}}'Expected:
--kubernetes-enable-dra Capture metrics associated with GPUs managed by Kubernetes Dynamic Resource Allocation (DRA) API. (default: false) [$KUBERNETES_ENABLE_DRA]
--disable-startup-validate Disable validation checks during startup. Can be useful for running in minimal environments or testing (default: false) [$DISABLE_STARTUP_VALIDATE]
clusterpolicy.nvidia.com/cluster-policy patched
A Pod with a ResourceClaim as on Day 8, and after the exporter restarts, the test:
kubectl create ns tag12
sed -e 's/namespace: tag08/namespace: tag12/' ~/gpu/tag08/claim-eine.yaml | kubectl apply -f -
sed -e 's/namespace: tag08/namespace: tag12/' ~/gpu/tag08/pod-eine.yaml | kubectl apply -f -
kubectl -n tag12 wait --for=condition=Ready pod/training --timeout=60s
sleep 60
kubectl -n gpu-operator port-forward svc/nvidia-dcgm-exporter 9400:9400 >/dev/null 2>&1 &
sleep 3
curl -s localhost:9400/metrics | grep -c 'pod="'
kubectl -n tag12 get resourceclaim eine-gpu -o jsonpath='{.status.allocation.devices.results[0].device}{"\n"}'Expected:
namespace/tag12 created
resourceclaim.resource.k8s.io/eine-gpu created
pod/training created
pod/training condition met
0
gpu-2
Not a single line from the exporter carries a pod label.
The test deliberately goes straight to the exporter, not to Prometheus,
because a trap lurks there:
curl -s localhost:9090/api/v1/query --data-urlencode 'query=DCGM_FI_DEV_GPU_UTIL{gpu="2"}' | jq -r '.data.result[0].metric | .pod + " " + .namespace + " " + .container'Expected:
nvidia-dcgm-exporter-lhth7 gpu-operator nvidia-dcgm-exporter
In Prometheus every series has a pod label, but it is
the exporter’s Pod, which the ServiceMonitor attaches
during scraping. Anyone who computes costs per Pod from it assigns all
GPUs to a single Pod in the gpu-operator namespace.
Tried it: Neither with a claim created from a Pod request for
nvidia.com/gpu(Day 8, Drill 6) nor with a dedicated ResourceClaim did the exporter (version 4.5.3) map Pods in the lab. The log reports “Kubernetes metrics collection enabled!”, but also that no ServiceAccount token is available to it. Whether this is due to Mokka, the exporter or the operator’s configuration could not be determined here. With the device plugin (before Day 8), the mapping is the usual, well-documented feature.
The path that always works goes via the UUID: the claim names the device, the ResourceSlice its UUID, and the UUID is a label on every measurement:
uuid=$(kubectl get resourceslice -o json | jq -r '.items[].spec.devices[] | select(.name=="gpu-2") | .attributes.uuid.string')
echo $uuid
q "DCGM_FI_DEV_GPU_UTIL{UUID=\"$uuid\"}"
kubectl delete ns tag12Expected (your utilization will differ):
GPU-12345678-1234-1234-1234-123456780002
2 41
namespace "tag12" deleted
This is the foundation on which platforms build their own chargeback: a small exporter reads claims and ResourceSlices and provides a “claim → UUID” metric, and Prometheus joins it with the DCGM values.
Save your progress? Sign up for free
Drill 5: Grafana
NVIDIA publishes a ready-made dashboard for the DCGM Exporter on grafana.com (number 12239). Import it via the Grafana API, with Prometheus as the data source:
cd ~/gpu/tag12
curl -sL https://grafana.com/api/dashboards/12239/revisions/latest/download -o dcgm-dashboard.json
jq -r '.title, (.__inputs | map(.name) | join(",")), (.panels | length)' dcgm-dashboard.json
kubectl -n monitoring port-forward svc/monitoring-grafana 3000:80 >/dev/null 2>&1 &
sleep 3
jq -n --slurpfile d dcgm-dashboard.json '{dashboard: $d[0], overwrite: true, folderUid: "", inputs: [{name: "DS_PROMETHEUS", type: "datasource", pluginId: "prometheus", value: "prometheus"}]}' > import.json
curl -s -u admin:lab-grafana-123 -H 'Content-Type: application/json' -X POST localhost:3000/api/dashboards/import -d @import.json | jq -r '.title + " " + .importedUrl'Expected:
NVIDIA DCGM Exporter Dashboard
DS_PROMETHEUS
8
NVIDIA DCGM Exporter Dashboard /d/Oxed_c6Wz/nvidia-dcgm-exporter-dashboard
In the browser via an SSH tunnel from your machine
(ssh -L 3300:localhost:3000 [email protected], then
http://localhost:3300, user admin, password
lab-grafana-123):
Temperature, power and clock stand still with Mokka, hence the flat lines. And one detail deserves a second look: the “GPU Power Total” tile shows 4.61 kW, while your own query in Drill 3 returned 576 W. The dashboard’s query computes something different from what the title promises. A ready-made dashboard is a starting point, not a measuring instrument. Every number that feeds into a decision should be recomputed once with your own query.
Save your progress? Sign up for free
Drill 6: Alerts
Two rules: one for GPUs that are too hot and one for GPUs that are allocated but barely used. For platform teams, the second is often more valuable than the first, because a GPU that a Pod blocks without using it costs just as much as one that computes.
cd ~/gpu/tag12
cat > gpu-alarme.yaml <<'EOF'
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
name: gpu-alarme
namespace: monitoring
spec:
groups:
- name: gpu
rules:
- alert: GPUZuHeiss
expr: DCGM_FI_DEV_GPU_TEMP > 80
for: 2m
labels:
severity: kritisch
annotations:
summary: "GPU {{ $labels.gpu }} auf {{ $labels.Hostname }} hat {{ $value }} Grad"
- alert: GPUKaumGenutzt
expr: avg_over_time(DCGM_FI_DEV_GPU_UTIL[5m]) < 50
for: 1m
labels:
severity: info
annotations:
summary: "GPU {{ $labels.gpu }} im Mittel nur {{ $value | humanize }} % ausgelastet"
EOF
kubectl apply -f gpu-alarme.yaml
sleep 100
curl -s localhost:9090/api/v1/rules | jq -r '.data.groups[] | select(.name=="gpu") | .rules[] | .name + " " + .state + " " + (.alerts | length | tostring)'
curl -s localhost:9090/api/v1/alerts | jq -r '.data.alerts[] | select(.labels.alertname=="GPUKaumGenutzt") | .state + " " + .annotations.summary' | head -3Expected:
prometheusrule.monitoring.coreos.com/gpu-alarme created
GPUZuHeiss inactive 0
GPUKaumGenutzt firing 8
firing GPU 1 im Mittel nur 19.78 % ausgelastet
firing GPU 2 im Mittel nur 27.89 % ausgelastet
firing GPU 3 im Mittel nur 32.44 % ausgelastet
GPUKaumGenutzt (“GPU barely used”) fires for all eight
GPUs because Mokka never pushes them above 45 percent. In production,
you join this rule with the allocation (only GPUs that belong to a Pod)
and send it to the team, not to the on-call rotation. Without
Alertmanager, the alerts only end up in Prometheus today; that is enough
for Day 13.
Save your progress? Sign up for free
Exercise: Speed Drills
- How many GPUs does Prometheus have metrics from? Just the number. Target time: 30 s
- Is the DCGM Exporter’s target healthy? Target time: 30 s
- For how many GPUs is
GPUKaumGenutztfiring? Target time: 30 s
Solution
curl -s localhost:9090/api/v1/query --data-urlencode 'query=count(DCGM_FI_DEV_GPU_UTIL)' | jq -r '.data.result[0].value[1]'
curl -s localhost:9090/api/v1/targets | jq -r '.data.activeTargets[] | select(.labels.job=="nvidia-dcgm-exporter") | .health'
curl -s localhost:9090/api/v1/alerts | jq '[.data.alerts[] | select(.state=="firing" and .labels.alertname=="GPUKaumGenutzt")] | length'Expected:
8
up
8
Save your progress? Sign up for free
Challenge
A colleague has written an alert for full GPU memory and proudly reports: “Running for a week, never a false alarm.”
cd ~/gpu/tag12
cat > speicher-alarm.yaml <<'EOF'
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
name: gpu-speicher
namespace: monitoring
spec:
groups:
- name: gpu-speicher
rules:
- alert: GPUSpeicherVoll
expr: DCGM_FI_DEV_FB_USED_BYTES / (DCGM_FI_DEV_FB_USED_BYTES + DCGM_FI_DEV_FB_FREE_BYTES) > 0.9
for: 5m
labels:
severity: warnung
EOF
kubectl apply -f speicher-alarm.yaml
sleep 45
curl -s localhost:9090/api/v1/rules | jq -r '.data.groups[] | select(.name=="gpu-speicher") | .rules[] | .name + " health=" + .health + " state=" + .state'Expected:
prometheusrule.monitoring.coreos.com/gpu-speicher created
GPUSpeicherVoll health=ok state=inactive
Healthy and quiet. Prove that this alert can never fire, and fix it.
Hint: What does the expression itself return, without the comparison?
Solution
curl -s localhost:9090/api/v1/query --data-urlencode 'query=DCGM_FI_DEV_FB_USED_BYTES' | jq -c '.data.result | length'
curl -s localhost:9090/api/v1/label/__name__/values | jq -r '.data[]' | grep '^DCGM_FI_DEV_FB'
curl -s localhost:9090/api/v1/query --data-urlencode 'query=absent(DCGM_FI_DEV_FB_USED_BYTES)' | jq -c '.data.result[0].value[1]'Expected:
0
DCGM_FI_DEV_FB_FREE
DCGM_FI_DEV_FB_RESERVED
DCGM_FI_DEV_FB_USED
"1"
The metric DCGM_FI_DEV_FB_USED_BYTES does not exist; it
is called DCGM_FI_DEV_FB_USED (in MiB). An expression over
a nonexistent metric is not an error for Prometheus but an empty result,
and an empty result never fires. health=ok only means: the
rule can be evaluated. absent() returns 1 when
a series does not exist, and that is exactly how you check it.
sed -i 's/DCGM_FI_DEV_FB_USED_BYTES/DCGM_FI_DEV_FB_USED/g; s/DCGM_FI_DEV_FB_FREE_BYTES/DCGM_FI_DEV_FB_FREE/g' speicher-alarm.yaml
grep expr speicher-alarm.yaml
kubectl apply -f speicher-alarm.yaml
q 'DCGM_FI_DEV_FB_USED / (DCGM_FI_DEV_FB_USED + DCGM_FI_DEV_FB_FREE)' | head -3Expected:
expr: DCGM_FI_DEV_FB_USED / (DCGM_FI_DEV_FB_USED + DCGM_FI_DEV_FB_FREE) > 0.9
prometheusrule.monitoring.coreos.com/gpu-speicher configured
0 0
1 0
2 0
Now the expression returns a value for every GPU (0, because Mokka
does not use any memory), and the rule fires as soon as a GPU goes above
90 percent. Good practice: for every alert, add a second rule
absent(<metric>) that reports when the data
disappears, and trigger every new alert deliberately once before you
trust it.
Save your progress? Sign up for free
Checkpoint
kubectl get clusterpolicy cluster-policy -o jsonpath='{.spec.dcgmExporter.serviceMonitor.enabled} {.spec.dcgmExporter.env[0].name}={.spec.dcgmExporter.env[0].value}{"\n"}'
kubectl -n monitoring get prometheusrule gpu-alarme gpu-speicher -o nameExpected:
true KUBERNETES_ENABLE_DRA=true
prometheusrule.monitoring.coreos.com/gpu-alarme
prometheusrule.monitoring.coreos.com/gpu-speicher
Clean-up
Prometheus, Grafana and the alerts stay for Day 13. Only the port-forwards go:
kill %1 %2 %3 2>/dev/null
jobsExpected: no output.
Interview Note
- Which GPU metrics do you watch first? Utilization
(
GPU_UTIL), used memory (FB_USED), temperature and power, plus the error counters for memory rows and Xid events (Day 13). - How do you charge back GPU usage per team? Via the mapping GPU → Pod → namespace. With the device plugin, the DCGM Exporter provides it itself; with DRA, the reliable path goes via claim, ResourceSlice and UUID.
- Why does an alert never fire? Often because its
expression is empty: wrong metric name, wrong labels. You check that
with
absent()and a deliberate test trigger.
Enterprise Note
- “Allocated but barely used” is the most expensive state of a GPU. Many platforms report this value weekly per team and tie it to quotas (Day 10): whoever uses little over time gets less.
- Real utilization measurement is more than
GPU_UTIL. Profiling metrics such asDCGM_FI_PROF_SM_ACTIVEandDCGM_FI_PROF_PIPE_TENSOR_ACTIVEshow whether the compute units are really working. They cost more and must be enabled in the exporter’s collector list. - Dashboards from the internet should be checked before use: queries, units, sums. The difference between 576 W and 4.61 kW from Drill 5 is not a rounding error on a power bill.