基于kube-prometheus-stack配置Blackbox Exporter的ServiceMonitor问题
Got it, let's break this down. You already have a working ServiceMonitor to scrape the Blackbox Exporter's own metrics (from /metrics), but now you need to replicate that standalone Prometheus blackbox probe job using ServiceMonitor. Here's exactly how to do it:
Key Background
The standalone blackbox job doesn't scrape the exporter directly—it tells the exporter to probe your targets by hitting its /probe endpoint with target parameters. So we need a separate ServiceMonitor for this probe task (since your existing one is for the exporter's internal metrics).
The ServiceMonitor Configuration
Create this new ServiceMonitor (adjust values to match your cluster setup):
apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: name: blackbox-probe-servicemonitor labels: release: prometheus # Match the release label of your kube-prometheus-stack installation spec: # We don't need to select a Service here since we're using static targets selector: matchLabels: dummy: label # A placeholder to avoid selector errors (can be any non-existent label) endpoints: - interval: 30s path: /probe # Target the exporter's probe endpoint instead of /metrics port: http # Match the port name from your Blackbox Exporter Service scrapeTimeout: 10s params: module: [http_2xx] # Same module as your standalone config staticConfigs: - targets: - http://localhost:5000 - http://localhost:3000 - http://localhost:9090 # Your list of static targets to probe relabelings: # Mirror the relabel rules from your standalone config - sourceLabels: [__address__] targetLabel: __param_target - sourceLabels: [__param_target] targetLabel: instance - targetLabel: __address__ replacement: blackbox-exporter:9115 # Replace with your Blackbox Exporter Service address (e.g., <service-name>.<namespace>.svc.cluster.local:9115)
What Each Part Does
Let's map this back to your original standalone Prometheus config:
params: {module: [http_2xx]}: Exactly matches theparamssection in your old job.staticConfigs.targets: Your list of targets to probe, same as thestatic_configsin the standalone setup.relabelings: Directly translates yourrelabel_configs—these rules rewrite the target address to point to the Blackbox Exporter, pass the original target as atargetparameter, and set theinstancelabel to the target URL.release: prometheus: Ensures kube-prometheus-stack's Prometheus instance picks up this ServiceMonitor (adjust if your Helm release name is different, e.g.,kube-prometheus-stack).
Critical Notes
- Service Address: The
replacementvalue in the last relabel rule must be the cluster-internal address of your Blackbox Exporter Service. If the exporter is in the same namespace as this ServiceMonitor, just<service-name>:<port>works. Otherwise, use the full DNS name likeblackbox-exporter.monitoring.svc.cluster.local:9115. - Port Name: The
port: httpmust match thenamefield in your Blackbox Exporter Service's port definition (not just the port number). - Separate ServiceMonitors: Keep your original ServiceMonitor (for
/metrics) and this new one (for/probe)—they serve different purposes: one tracks the exporter's health, the other runs your probes.
Once you apply this ServiceMonitor, check your Prometheus targets page—you should see a new job (named something like monitoring/blackbox-probe-servicemonitor/0) with your static targets being probed via the Blackbox Exporter.
内容的提问来源于stack exchange,提问作者Jonas




