kube-prometheus ServiceMonitor使用
原理其实就是创建一个新的serviceMonitor
后,prometheus
会创建一个target
例子:ServiceMonitor.yaml
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
labels:
k8s-app: xxx-exporter
name: xxx
namespace: prometheus
spec:
endpoints:
- interval: 15s
port: xxx-exporter
jobLabel: xxx-exporter-monitor
namespaceSelector:
matchNames:
- monitor #目标服务的namespaces
selector:
matchLabels:
k8s-app: xx-exporter # 目标服务的labels
namespaceSelector: any: true
表示选择所有的
namespace
上面ServiceMonitor
是针对k8s集群内部服务监控,想监控集群外部的exporter通Endpoints
实现
为外部 exporter 服务设置 service:
apiVersion: v1
kind: Service
metadata:
labels:
app: monitoring-external-node-exporter
app.kubernetes.io/name: node-exporter
release: monitoring
name: monitoring-external-node-exporter
namespace: monitoring
spec:
type: ClusterIP
ports:
- name: http-metrics
port: 39xxx
protocol: TCP
targetPort: 39xxx
---
apiVersion: v1
kind: Endpoints
metadata:
name: monitoring-external-node-exporter
labels:
app: monitoring-external-node-exporter
app.kubernetes.io/name: node-exporter
release: monitoring
namespace: monitoring
subsets:
- addresses:
- ip: xx.xx.xx.xx
- ip: xx.xx.xx.xx
ports:
- name: http-metrics
port: 39xxx
protocol: TCP
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
labels:
app: monitoring-external-node-exporter
release: monitoring
name: monitoring-external-node-exporter-sm
namespace: monitoring
spec:
endpoints:
- interval: 30s
port: http-metrics
path: /metrics
authorization:
basicAuth:
username:
name: external-node-exporter-basic-auth
key: user
password:
name: external-node-exporter-basic-auth
key: password
jobLabel: node-exporter
namespaceSelector:
matchNames:
- monitoring
selector:
matchLabels:
app: monitoring-external-node-exporter
release: monitoring