promethus operator监控扩展之serviceMonitor

serviceMonitor 是通过对service 获取数据的一种方式。

  1. promethus-operator可以通过serviceMonitor 自动识别带有某些 label 的service ,并从这些service 获取数据。
  2. serviceMonitor 也是由promethus-operator 自动发现的。

以下是mysql 监控 的实例

1. 下载 mysql-exprotor

git clone https://github.com/helm/charts.git
在 stab 中有很多的exporter, mysql 官方的也在里面

2.部署 mysql-exprotor

创建数据库账号 并 赋权

CREATE USER '<用户名>'@'localhost' IDENTIFIED BY '<密码>' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';

使用helm 创建 mysql-exprotor

helm install --name  mysql-moneyfeed-master  stable/prometheus-mysql-exporter --set mysql.user="<用户名>",mysql.pass="<密码>",mysql.host="<主机名>",mysql.port="<端口>" --namespace monitoring

3.部署 serviceMonitor

serviceMonitor 的 labbels 非常的关键,promethus 通过标签发现serviceMonitor。
执行

kubectl get prometheus ack-prometheus-operator-prometheus  -n monitoring -o yaml

找到以下配置信息

serviceMonitorSelector:
    matchLabels:
      release: ack-prometheus-operator

release: ack-prometheus-operator 就是serviceMonitor 需要配置的label

serviceMonitor.yaml

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor 
metadata:
  labels:
    app: ack-prometheus-operator-mysql-exporter
    heritage: Tiller
    release: ack-prometheus-operator   # prometheus 通过该label 来发现该serviceMonitor
  name: ack-prometheus-operator-mysql-exporter
  namespace: monitoring
spec:
  jobLabel: RDS-exporter
  selector:
    matchLabels:
      app: prometheus-mysql-exporter   # 该serviceMonitor 通过标签选择器 来自动发现exporter 的sevice
  namespaceSelector:
    matchNames:
    - monitoring
  endpoints:
  - port: mysql-exporter     # service 端口
    interval: 30s 
    honorLabels: true

创建资源
serviceMonitor.yaml

kubectl apply -f serviceMonitor.yaml
原文地址:https://www.cnblogs.com/pythonPath/p/12505457.html