配置文件優化后的 Prometheus 自動發現 MySQL 實例的完整 YAML 文件。該配置包括:
- MySQL Exporter 部署:使用
ConfigMap
提供 MySQL 連接信息。 - Prometheus 自動發現:通過 Kubernetes 服務發現自動抓取 MySQL 實例。
1、mysql 配置文件 (mysql-deployment.yaml)
apiVersion: apps/v1
kind: Deployment
metadata:name: mysqllabels:app: mysql
spec:replicas: 3selector:matchLabels:app: mysqltemplate:metadata:labels:app: mysqlannotations:prometheus.io/scrape: "true" # 允許 Prometheus 抓取prometheus.io/port: "9104" # MySQL Exporter 暴露的端口spec:containers:- name: mysqlimage: harbor.fq.com/public/mysql:9.1.0 # 使用官方 MySQL 鏡像env:- name: MYSQL_ROOT_PASSWORDvalue: "password" # 設置 MySQL root 密碼ports:- containerPort: 3306 # MySQL 默認端口
---
apiVersion: v1
kind: Service
metadata:name: mysql-servicelabels:app: mysqlannotations:prometheus.io/scrape: "true" # 允許 Prometheus 抓取prometheus.io/port: "9104" # MySQL Exporter 暴露的端口
spec:selector:app: mysqlports:- name: mysqlprotocol: TCPport: 3306targetPort: 3306
cat mysql-statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:name: mysqllabels:app: mysql
spec:serviceName: "mysql"replicas: 1selector:matchLabels:app: mysqltemplate:metadata:labels:app: mysqlannotations:prometheus.io/scrape: "true" # 允許 Prometheus 抓取prometheus.io/port: "9104" # MySQL Exporter 暴露的端口spec:containers:- name: mysqlimage: harbor.fq.com/public/mysql:9.1.0 # MySQL 鏡像env:- name: MYSQL_ROOT_PASSWORDvalue: "password" # 設置 MySQL root 密碼ports:- containerPort: 3306 # MySQL 默認端口volumeMounts:- name: mysql-datamountPath: /var/lib/mysql # MySQL 數據存儲路徑volumes:- name: mysql-dataemptyDir: {} # 使用空目錄,不持久化數據---
apiVersion: v1
kind: Service
metadata:name: mysql-servicelabels:app: mysqlannotations:prometheus.io/scrape: "true" # 允許 Prometheus 抓取prometheus.io/port: "9104" # MySQL Exporter 暴露的端口
spec:selector:app: mysqlports:- name: mysqlprotocol: TCPport: 3306targetPort: 3306type: ClusterIP # 內部服務
2、登錄mysql,并創建‘mysql_exporter’用戶
2.1、查看mysql容器名稱,登錄到容器內
[root@k8s-master01 example]# kubectl get pod
NAME READY STATUS RESTARTS AGE
kuard-d574f5b78-r2l77 1/1 Running 0 278d
mysql-0 1/1 Running 0 6s[root@k8s-master01 example]# kubectl exec -it mysql-0 -- bash
bash-5.1#
2.2、確保 mysql_exporter
用戶存在**
使用 MySQL root 用戶登錄并檢查 mysql_exporter
用戶:
SELECT user, host FROM mysql.user WHERE user = 'mysql_exporter';
2.3、如果沒有該用戶,則創建:
CREATE USER 'mysql_exporter'@'%' IDENTIFIED BY 'your_password';
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'mysql_exporter'@'%';
FLUSH PRIVILEGES;
實操:
CREATE USER 'mysql_exporter'@'%' IDENTIFIED BY 'mysql123!';
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'mysql_exporter'@'%';
FLUSH PRIVILEGES;
![[IMG-5、k8s平臺:mysql 監控案例-20250318102405307.png]]
mysql> SELECT user, host FROM mysql.user WHERE user = 'mysql_exporter';
Empty set (0.00 sec)mysql> CREATE USER 'mysql_exporter'@'%' IDENTIFIED BY 'mysql123!';
Query OK, 0 rows affected (0.01 sec)mysql> GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'mysql_exporter'@'%';
Query OK, 0 rows affected (0.01 sec)mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)mysql> SELECT user, host FROM mysql.user WHERE user = 'mysql_exporter';
+----------------+------+
| user | host |
+----------------+------+
| mysql_exporter | % |
+----------------+------+
1 row in set (0.00 sec)mysql>
? 注意:確保 your_password
與 mysqld-exporter
配置的密碼匹配。
4. MySQL Exporter 配置文件 (mysql-exporter-config.yaml
)
apiVersion: v1
kind: ConfigMap
metadata:name: mysql-exporter-config
data:.my.cnf: |-[client]user = mysql_exporterpassword = mysql123![client.servers]user = mysql_exporterpassword = mysql123!
5. MySQL Exporter 部署文件 (mysql-exporter-deployment.yaml
)
apiVersion: apps/v1
kind: Deployment
metadata:name: mysql-exporterlabels:app: mysql-exporter
spec:replicas: 1selector:matchLabels:app: mysql-exportertemplate:metadata:labels:app: mysql-exporterannotations:prometheus.io/scrape: "true" # 允許 Prometheus 抓取prometheus.io/port: "9104" # MySQL Exporter 暴露的端口spec:volumes:- name: mysql-exporter-configconfigMap:name: mysql-exporter-configitems:- key: .my.cnfpath: .my.cnfcontainers:- name: mysql-exporterimage: harbor.fq.com/prometheus/mysql-exporter:v0.16.0command:- mysqld_exporter- --config.my-cnf=/etc/mysql-exporter/.my.cnf # 指定配置文件路徑securityContext:runAsUser: 0 # 以 root 用戶運行ports:- containerPort: 9104 # MySQL Exporter 默認端口volumeMounts:- name: mysql-exporter-configmountPath: /etc/mysql-exporter/.my.cnfsubPath: .my.cnf
---
apiVersion: v1
kind: Service
metadata:name: mysql-exporter-servicelabels:app: mysql-exporterannotations:prometheus.io/scrape: "true" # 允許 Prometheus 抓取prometheus.io/port: "9104" # MySQL Exporter 暴露的端口
spec:selector:app: mysql-exporterports:- protocol: TCPport: 9104targetPort: 9104type: ClusterIP
6. Prometheus 自動發現配置 (prometheus.yml
)
scrape_configs:- job_name: 'mysql'kubernetes_sd_configs:- role: endpoints # 從 Kubernetes Endpoints 發現服務relabel_configs:# 只抓取帶有 `prometheus.io/scrape: "true"` 注解的服務- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scrape]action: keepregex: true# 替換目標地址為服務的 IP 和指定端口(9104)- source_labels: [__meta_kubernetes_endpoint_address_target_kind, __meta_kubernetes_endpoint_address_target_name]action: keepregex: Pod;(.*mysql-exporter.*) # 僅抓取名稱包含 "mysql-exporter" 的 Pod- source_labels: [__meta_kubernetes_pod_ip]action: replacetarget_label: __address__replacement: $1:9104 # 指定 MySQL Exporter 的端口為 9104# 添加 Kubernetes 服務的 app 標簽- source_labels: [__meta_kubernetes_service_label_app]action: replacetarget_label: app# 添加 Kubernetes 命名空間標簽- source_labels: [__meta_kubernetes_namespace]action: replacetarget_label: namespace# 添加 Kubernetes 服務名稱標簽- source_labels: [__meta_kubernetes_service_name]action: replacetarget_label: service# 添加 Kubernetes Pod 名稱標簽- source_labels: [__meta_kubernetes_pod_name]action: replacetarget_label: pod# 添加 Kubernetes 節點名稱標簽- source_labels: [__meta_kubernetes_pod_node_name]action: replacetarget_label: node# 添加實例標簽(用于區分不同的 MySQL 實例)- source_labels: [__meta_kubernetes_pod_ip]action: replacetarget_label: instance
7. 部署步驟
-
創建
ConfigMap
:kubectl apply -f mysql-exporter-config.yaml
-
部署 MySQL Exporter:
kubectl apply -f mysql-exporter-deployment.yaml
-
更新 Prometheus 配置文件(
prometheus.yml
),添加 MySQL 的自動發現配置。 -
重啟 Prometheus 以加載新配置。
8. 驗證
-
檢查
mysql-exporter
容器日志:kubectl logs <mysql-exporter-pod-name> -c mysql-exporter
- 確保沒有錯誤日志。
-
檢查 Pod 狀態:
kubectl get pods
- 確保
mysql-exporter
容器處于Running
狀態。
- 確保
-
訪問 Prometheus Web UI(
http://<prometheus-server>:9090
),查看 Targets 頁面,確認 MySQL 目標已被發現。 ![[IMG-5、k8s平臺:mysql 監控案例-20250317170341907.png]]
9. 生產環境建議
- 高可用性:部署多個 MySQL Exporter 實例,并使用 Kubernetes 的
HorizontalPodAutoscaler
實現自動擴展。 - 監控告警:設置 MySQL 關鍵指標的告警規則(如連接數、慢查詢等)。
- 資源限制:為 MySQL Exporter 設置資源限制(CPU 和內存)。
- 日志管理:收集 MySQL Exporter 的日志,便于排查問題。
10. 示例告警規則 (mysql-alerts.yml
)
groups:- name: mysql_alertsrules:- alert: MySQLDownexpr: mysql_up == 0for: 1mlabels:severity: criticalannotations:summary: "MySQL is down"description: "MySQL instance {{ $labels.instance }} is down."- alert: HighMySQLConnectionsexpr: mysql_global_status_connections > 1000for: 5mlabels:severity: warningannotations:summary: "High number of MySQL connections"description: "MySQL instance {{ $labels.instance }} has more than 1000 connections."- alert: HighMySQLSlowQueriesexpr: mysql_global_status_slow_queries > 10for: 5mlabels:severity: warningannotations:summary: "High number of slow queries on MySQL"description: "MySQL instance {{ $labels.instance }} has more than 10 slow queries."