题记
记录一次ServiceMonitor
使用笔记, 一开始没用对导致target无法添加成功。
https://github.com/prometheus-operator/prometheus-operator/blob/master/Documentation/user-guides/getting-started.md#include-servicemonitors
究其原因是ServiceMonitor
的选择器没有正确匹配到service上。
ServiceMonitor
apiVersion: v1
kind: Service
metadata:
name: org0-orderer1
labels:
app: org0-orderer1 #这里一定要加上,否则识别不到
spec:
ports:
- name: general
port: 6050
protocol: TCP
- name: operations
port: 8443
protocol: TCP
- name: admin
port: 9443
protocol: TCP
selector:
app: org0-orderer1
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: monitor-org0-orderer1
namespace: monitoring
labels:
release: mypro #Prometheus所选择的标签(目前没看明白有什么用)
spec:
namespaceSelector: #监控的pod所在名称空间
matchNames:
- monitoring
selector:
matchLabels:
app: org0-orderer1 #这里指定了label的名称
endpoints:
- port: operations
找了一下关于这个选择器的定义,好吧,果然是严格匹配模式
serviceMonitorSelector:
description: ServiceMonitors to be selected for target discovery.
*Deprecated:* if neither this nor podMonitorSelector are specified,
configuration is unmanaged.
properties:
matchExpressions:
description: matchExpressions is a list of label selector requirements.
The requirements are ANDed.
items:
description: A label selector requirement is a selector that
contains values, a key, and an operator that relates the key
and values.
properties:
key:
description: key is the label key that the selector applies
to.
type: string
operator:
description: operator represents a key's relationship to
a set of values. Valid operators are In, NotIn, Exists
and DoesNotExist.
type: string
values:
description: values is an array of string values. If the
operator is In or NotIn, the values array must be non-empty.
If the operator is Exists or DoesNotExist, the values
array must be empty. This array is replaced during a strategic
merge patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
matchLabels:
additionalProperties:
type: string
description: matchLabels is a map of {
key,value} pairs. A single
{
key,value} in the matchLabels map is equivalent to an element
of matchExpressions, whose key field is "key", the operator
is "In", and the values array contains only "value". The requirements
are ANDed.
type: object
type: object