Prometheus ServiceMonitor

作者: root007 分类: kubernetes,prometheus 发布时间: 2019-10-11 18:51

创建一个新的serviceMonitor就创建了一个prometheus的target

[root@k8s-m1 ~]# cat 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
kind: Service
apiVersion: v1
metadata:
  namespace: monitor
  name: service-mysql-xx
  labels:
    app: service-mysql-xx
spec:
  ports:
    - protocol: TCP
      port: 9xx
      targetPort: 9xx
  type: ClusterIP
  clusterIP: None

---
kind: Endpoints
apiVersion: v1
metadata:
  namespace: monitor
  name: service-mysql-xx
  labels:
    app: service-mysql-xx
subsets:
  - addresses:
      - ip: x.x.x.x
    ports:
      - protocol: TCP
        port: 9xxx

ServiceMonitor

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
    name: service-mysql-xx
    labels:
        app: service-mysql-xx
spec:
    selector:
        matchLabels:
            app: service-mysql-xx
        namespaceSelector:
            matchNames:
            - monitor
    endpoints:
    - port: metrics
      interval: 10s
      honorLabels: true

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注