Skip to content

MySQL Exporter 部署

在 Kubernetes 上部署 mysqld-exporter,將 MySQL 執行時指標匯出至 Prometheus。

概述

MySQL Exporter(prom/mysqld-exporter)是 Prometheus 官方推薦的 MySQL 監控方案,透過連線至 MySQL 伺服器採集連線數、查詢效能、複寫延遲等關鍵指標。

部署前需在 MySQL 端建立專用的 exporter 帳號並授予最小必要權限,密碼則透過 Kubernetes Secret 管理,避免明文出現在 Deployment YAML 中。與其他 Exporter 不同,MySQL Exporter 不依賴 Helm Chart,改以直接撰寫 Deployment + Service YAML 的方式部署,參考 Prometheus Exporter 部署模式 的標準架構。

核心內容

MySQL 帳號設計

MySQL Exporter 需要三項權限:

  • PROCESS:查看執行中的 query 與 thread 狀態
  • REPLICATION CLIENT:採集複寫延遲(slave_status
  • SELECT:讀取系統資料表(information_schema 等)

帳號應限制最大連線數(建議 3),並用 IP 網段白名單限制來源,減少攻擊面。

密碼注入方式

密碼透過 Kubernetes Secret 存入,Deployment 以 secretKeyRef 方式注入環境變數 MYSQLD_EXPORTER_PASSWORD,Exporter 啟動時自動讀取。連線帳號與 MySQL 主機地址則以 --mysqld.username--mysqld.address 啟動參數傳入。

注意: --mysqld.address 的 IP 必須在 MySQL 帳號允許的來源網段內,否則連線會被 MySQL 拒絕。

預設 Collector

mysqld-exporter 預設啟用的 collector(global_statusglobal_variablesslave_status)已涵蓋一般監控需求,包含 MySQL 連線數、查詢統計、複寫狀態等,無需額外指定 flag 即可開始採集。

關鍵要點

  • 帳號建立時指定 WITH MAX_USER_CONNECTIONS 3,防止 Exporter 佔用過多連線
  • 密碼透過 Kubernetes Secret 管理,Secret key 為 MYSQLD_EXPORTER_PASSWORD
  • Metrics 埠號為 9104,與 MySQL 官方 Exporter 規範一致
  • 設定 livenessProbereadinessProbe 確保 Exporter 健康狀態可被 K8s 管理
  • 部署於 monitoring namespace,與其他 Exporter 統一管理

實際應用

部署完成後,在 PrometheusextraScrapeConfigs 中新增 scrape job,指向 mysqld-exporter.monitoring.svc.cluster.local:9104,即可開始採集 MySQL 指標並在 Grafana 中建立監控儀表板。

部署設定參考

以下為實際部署時使用的完整設定,供日後查詢與複製使用。

環境參數

項目
MySQL 主機10.30.196.60:3306
Exporter 帳號exporter
Exporter 密碼JRG63t8t
允許連線網段10.30.196.%
Imageprom/mysqld-exporter:latest
Metrics 埠號9104
K8s Namespacemonitoring
Secret 名稱mysqld-exporter-secret

MySQL 帳號建立 SQL

sql
CREATE USER 'exporter'@'10.30.196.%' IDENTIFIED BY 'JRG63t8t' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'10.30.196.%';

Kubernetes Secret

bash
kubectl create secret generic mysqld-exporter-secret \
  --from-literal=MYSQLD_EXPORTER_PASSWORD='JRG63t8t' \
  -n monitoring

Kubernetes YAML(Service + Deployment)

yaml
apiVersion: v1
kind: Service
metadata:
  name: mysqld-exporter
  namespace: monitoring
  labels:
    app.kubernetes.io/name: mysqld-exporter
spec:
  selector:
    app.kubernetes.io/name: mysqld-exporter
  ports:
    - name: metrics
      port: 9104
      targetPort: 9104
      protocol: TCP
  type: ClusterIP

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysqld-exporter
  namespace: monitoring
  labels:
    app.kubernetes.io/name: mysqld-exporter
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: mysqld-exporter
  template:
    metadata:
      labels:
        app.kubernetes.io/name: mysqld-exporter
    spec:
      containers:
        - name: mysqld-exporter
          image: prom/mysqld-exporter:latest
          args:
            - "--mysqld.address=10.30.196.60:3306"
            - "--mysqld.username=exporter"
          env:
            - name: MYSQLD_EXPORTER_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysqld-exporter-secret
                  key: MYSQLD_EXPORTER_PASSWORD
          ports:
            - name: metrics
              containerPort: 9104
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /
              port: 9104
            initialDelaySeconds: 10
            timeoutSeconds: 5
          readinessProbe:
            httpGet:
              path: /
              port: 9104
            initialDelaySeconds: 5
            timeoutSeconds: 2

相關概念

來源