prometheus监控mysql

1、在mysql中创建监控用户

CREATE USER 'exporter'@'%' IDENTIFIED BY '123456' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'%';
GRANT SELECT ON performance_schema.* TO 'exporter'@'%';
flush privileges;

2、在k8s上安装mysqld_exported
镜像来源:prom/mysqld-exporter:v0.14.0

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysqld-exporter
  namespace: namespace-01  ##命名空间
spec:
  replicas: 1
  selector:
    matchLabels:
      k8s-app: mysqld-exporter
  template:
    metadata:
      labels:
        k8s-app: mysqld-exporter
    spec:
      containers:
      - name: mysqld-exporter
        image: prom/mysqld-exporter:v0.14.0
        env:
        - name: DATA_SOURCE_NAME
          value: exporter:123456@(127.0.0.1:3306)/
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 9104
---
apiVersion: v1
kind: Service
metadata:
  name: mysqld-exporter
  namespace: namespace-01   ##命名空间
  labels:
    k8s-app: mysqld-exporter
spec:
  type: ClusterIP
  selector:
    k8s-app: mysqld-exporter
  ports:
  - name: api
    port: 9104
    protocol: TCP

3、在prometheus的配置文件中增加采集信息

- file_sd_configs:
    - files:
      - mysql.yml
    job_name: MySQL
    metrics_path: /metrics
    relabel_configs:
    - source_labels: [__address__]
      regex: (.*)
      target_label: __address__
      replacement: $1

增加mysql.yml文件

- labels:
    instance: 127.0.0.1:3306 # grafana显示的实例的别名
  targets:
  - 127.0.0.1:30211 # mysqld_exporter暴露的端口

你可能感兴趣的:(prometheus,mysql,prometheus)