首先,你需要创建一个Service Monitor,它是一个自定义的K8s资源。servicemonitor.yaml
只需在 manifests 文件夹中创建一个。
因为当我们在 K8s 上部署时,我们无法访问 Prometheus.yaml 文件来提及目标,所以我们创建了 servicemonitor,它反过来将目标添加到 Prometheus.yaml 文件中的 scrap_config 中。您可以从这里了解更多信息。
这是在 Prometheus 中公开 Flask App 指标的示例servicemonitor.yaml
文件。
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: flask-metrics
namespace: prometheus # namespace where prometheus is running
labels:
app: flask-app
release: prom # name of the release
# ( VERY IMPORTANT: You need to know the correct release name by viewing
# the servicemonitor of Prometheus itself: Without the correct name,
# Prometheus cannot identify the metrics of the Flask app as the target.)
spec:
selector:
matchLabels:
# Target app service
app: flask-app # same as above
release: prom # same as above
endpoints:
- interval: 15s # scrape interval
path: /metrics # path to scrape
port: http # named port in target app
namespaceSelector:
matchNames:
- flask # namespace where the app is running
还要将此发布标签添加到服务和部署文件的元数据和规范部分。
如果您遇到 Prometheus 显示目标而不显示端点的情况,请查看以下内容:https ://github.com/prometheus-operator/prometheus-operator/issues/3053
一些有用的链接: