为什么我的 Prometheus Operator ServiceMonitor 需要一个“release”标签才能正确 运行?
Why is it that my Prometheus Operator ServiceMonitor needs a `release` label to run properly?
当我部署这个时:
kind: ServiceMonitor
apiVersion: monitoring.coreos.com/v1
metadata:
labels:
app: complaints-monitor
release: prometheus # <---------
name: complaints-monitor-svc-monitor
namespace: default
spec:
endpoints:
- path: /metrics
port: web
namespaceSelector:
matchNames:
- default
selector:
matchLabels:
app: complaints-monitor
我的服务显示在 prometheus 目标下并正确运行。
如果我改为应用以下内容,目标就会消失:
kind: ServiceMonitor
apiVersion: monitoring.coreos.com/v1
metadata:
labels:
app: complaints-monitor
name: complaints-monitor-svc-monitor
namespace: default
spec:
endpoints:
- path: /metrics
port: web
namespaceSelector:
matchNames:
- default
selector:
matchLabels:
app: complaints-monitor
查了kube-prometheus-stack
的values.yaml
才发现这个问题
## If true, a nil or {} value for prometheus.prometheusSpec.serviceMonitorSelector will cause the
## prometheus resource to be created with selectors based on values in the helm deployment,
## which will also match the servicemonitors created
##
serviceMonitorSelectorNilUsesHelmValues: true
## ServiceMonitors to be selected for target discovery.
## If {}, select all ServiceMonitors
##
serviceMonitorSelector: {}
## Example which selects ServiceMonitors with label "prometheus" set to "somelabel"
# serviceMonitorSelector:
# matchLabels:
# prometheus: somelabel
当您使用 helm 安装 kube-prometheus-stack
时,它会将标签 release: <prometheus-installed-namespace>
添加到 Kubernetes 资源。
如果设置 serviceMonitorSelectorNilUsesHelmValues
是 true
一个 select 或者将被添加到 prometheus.prometheusSpec.serviceMonitorSelector
,在你的情况下是 release: prometheus
.
您应该能够看到 select 或 prometheus 到 select serviceMonitor 使用命令
kubectl -n <prometheus-installed-namespace> get prometheus -o yaml
并搜索 serviceMonitorSelector
.
您有两种选择可以在不添加 release
标签的情况下使其正常工作
设置serviceMonitorSelectorNilUsesHelmValues
为false
,那么prometheus会select所有serviceMonitor
或者,将 serviceMonitorSelector
设置为您喜欢的任何标签。
当我部署这个时:
kind: ServiceMonitor
apiVersion: monitoring.coreos.com/v1
metadata:
labels:
app: complaints-monitor
release: prometheus # <---------
name: complaints-monitor-svc-monitor
namespace: default
spec:
endpoints:
- path: /metrics
port: web
namespaceSelector:
matchNames:
- default
selector:
matchLabels:
app: complaints-monitor
我的服务显示在 prometheus 目标下并正确运行。
如果我改为应用以下内容,目标就会消失:
kind: ServiceMonitor
apiVersion: monitoring.coreos.com/v1
metadata:
labels:
app: complaints-monitor
name: complaints-monitor-svc-monitor
namespace: default
spec:
endpoints:
- path: /metrics
port: web
namespaceSelector:
matchNames:
- default
selector:
matchLabels:
app: complaints-monitor
查了kube-prometheus-stack
的values.yaml
才发现这个问题
## If true, a nil or {} value for prometheus.prometheusSpec.serviceMonitorSelector will cause the
## prometheus resource to be created with selectors based on values in the helm deployment,
## which will also match the servicemonitors created
##
serviceMonitorSelectorNilUsesHelmValues: true
## ServiceMonitors to be selected for target discovery.
## If {}, select all ServiceMonitors
##
serviceMonitorSelector: {}
## Example which selects ServiceMonitors with label "prometheus" set to "somelabel"
# serviceMonitorSelector:
# matchLabels:
# prometheus: somelabel
当您使用 helm 安装 kube-prometheus-stack
时,它会将标签 release: <prometheus-installed-namespace>
添加到 Kubernetes 资源。
如果设置 serviceMonitorSelectorNilUsesHelmValues
是 true
一个 select 或者将被添加到 prometheus.prometheusSpec.serviceMonitorSelector
,在你的情况下是 release: prometheus
.
您应该能够看到 select 或 prometheus 到 select serviceMonitor 使用命令
kubectl -n <prometheus-installed-namespace> get prometheus -o yaml
并搜索 serviceMonitorSelector
.
您有两种选择可以在不添加 release
标签的情况下使其正常工作
设置
serviceMonitorSelectorNilUsesHelmValues
为false
,那么prometheus会select所有serviceMonitor或者,将
serviceMonitorSelector
设置为您喜欢的任何标签。