MySQL主从监控配置-Helm部署方案
一、背景简介
生产环境的MySQL监控面临着部署复杂、标签不统一、管理困难等问题。传统方式中每个MySQL实例都需要单独部署一个exporter,部署方式繁琐,采集的指标标签不统一,无法在监控图表上快速检索查看。
本方案采用Helm部署mysql-exporter实现MySQL监控采集,可以灵活增加mysql监控实例,大大简化了运维管理复杂度。
二、部署文件准备
2.1. 必需文件清单
| 文件类型 | 文件名称 | 说明 | 
|---|---|---|
| Helm Charts | prometheus-mysql-exporter-2.10.0.tgz | 
prometheus-mysql-exporter chart部署包 | 
| 配置文件 | mysql-exporter-values.yaml | 
mysql-exporter配置文件 | 
2.2. 环境要求
- Kubernetes集群环境
 - Helm 3.x已安装配置
 - Prometheus Operator已部署
 - 具备集群管理权限
 
三、配置步骤
3.1. MySQL监控账号创建
MySQL监控账号用于采集MySQL监控数据,需要具备特定的权限以确保能够获取完整的监控指标。
(1)创建监控用户
CREATE USER 'exporter'@'%' IDENTIFIED BY 'your-secure-password' WITH MAX_USER_CONNECTIONS 3;
(2)授权必要权限
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'%';
FLUSH PRIVILEGES;
(3)权限说明
| 权限类型 | 用途说明 | 
|---|---|
PROCESS | 
查看进程列表,获取连接状态信息 | 
REPLICATION CLIENT | 
获取主从复制状态信息 | 
SELECT | 
查询performance_schema等系统表 | 
3.2. 配置文件修改
修改mysql-exporter-values.yaml配置文件中的关键配置项:
(1)多目标配置结构
serviceMonitor:
  enabled: true
  additionalLabels: {}
  jobLabel: ""
  targetLabels: []
  podTargetLabels: []
  metricRelabelings: []
  relabelings: []
  
  # 启用多目标采集
  multipleTarget:
    enabled: true
    targets:
    - endpoint: 172.16.1.15
      name: app-db-write-master
      port: 3306
      user: exporter
      password: your-secure-password
    - endpoint: 172.16.1.17
      name: app-db-backup-master
      port: 3306
      user: exporter
      password: your-secure-password
      
  # 共享认证配置(可选)
  sharedSecret:
    enabled: false
    name: ""
(2)配置参数详解
| 配置项 | 示例值 | 说明 | 
|---|---|---|
endpoint | 
172.16.1.15 | 
MySQL实例的IP地址或域名 注意:使用域名配置多个IP时需使用具体IP避免数据混乱  | 
name | 
app-db-write-master | 
节点标识名称,用于监控页面筛选 命名规范:平台-用途-角色  | 
port | 
3306 | 
MySQL端口,默认3306,可选配置 | 
user | 
exporter | 
监控账号用户名 | 
password | 
your-secure-password | 
监控账号密码 注意:避免使用 #等特殊字符 | 
(3)命名规范示例
四、部署实施
4.1. 部署命令
在具备Helm环境的生产服务器上执行部署:
# 部署MySQL监控
helm install prometheus-mysql-exporter \
  prometheus-mysql-exporter-2.10.0.tgz \
  --version 2.10.0 \
  -n monitoring \
  -f mysql-exporter-values.yaml
4.2. 部署验证
(1)检查Pod状态
# 查看Pod状态
kubectl get pods -n monitoring | grep mysql-exporter
# 查看Pod详细信息
kubectl describe pod <mysql-exporter-pod-name> -n monitoring
(2)验证Service和ServiceMonitor
# 查看Service
kubectl get service -n monitoring | grep mysql-exporter
# 查看ServiceMonitor
kubectl get servicemonitor -n monitoring | grep mysql-exporter
(3)验证指标采集
# 端口转发测试
kubectl port-forward service/prometheus-mysql-exporter 9104:9104 -n monitoring
# 访问指标端点
curl http://localhost:9104/metrics
4.3. 安全清理
五、监控配置管理
5.1. 添加新的MySQL实例
(1)获取当前配置
# 导出当前配置
helm get values prometheus-mysql-exporter -n monitoring > current-values.yaml
(2)修改配置添加新实例
# 在targets中添加新的MySQL实例
targets:
- endpoint: 172.16.1.18
  name: app-cluster-write-master
  user: exporter
  password: your-secure-password
(3)更新部署
# 更新Helm部署
helm upgrade prometheus-mysql-exporter \
  prometheus-mysql-exporter-2.10.0.tgz \
  --version 2.10.0 \
  -n monitoring \
  -f current-values.yaml
  
# 清理临时配置文件
rm current-values.yaml
5.2. 移除MySQL实例
# 导出配置并编辑
helm get values prometheus-mysql-exporter -n monitoring > temp-values.yaml
# 编辑文件移除不需要的targets配置
vim temp-values.yaml
# 应用更新
helm upgrade prometheus-mysql-exporter \
  prometheus-mysql-exporter-2.10.0.tgz \
  --version 2.10.0 \
  -n monitoring \
  -f temp-values.yaml
# 清理文件
rm temp-values.yaml
5.3. 完全卸载
# 卸载Helm部署
helm uninstall prometheus-mysql-exporter -n monitoring
# 清理相关Secret(如果需要)
kubectl delete secret prometheus-mysql-exporter -n monitoring
六、监控指标说明
6.1. 核心MySQL指标
6.2. 告警规则配置
groups:
- name: mysql_alerts
  rules:
  - alert: MySQLDown
    expr: mysql_up == 0
    for: 1m
    labels:
      severity: critical
    annotations:
      summary: "MySQL实例不可用"
      description: "{{ $labels.instance }} MySQL实例已下线"
      
  - alert: MySQLSlaveLag
    expr: mysql_slave_lag_seconds > 300
    for: 2m
    labels:
      severity: warning
    annotations:
      summary: "MySQL主从延迟过高"
      description: "{{ $labels.instance }} 主从延迟 {{ $value }} 秒"
      
  - alert: MySQLConnectionsHigh
    expr: mysql_global_status_threads_connected / mysql_global_variables_max_connections > 0.8
    for: 2m
    labels:
      severity: warning
    annotations:
      summary: "MySQL连接数过高"
      description: "{{ $labels.instance }} 连接使用率超过80%"
七、故障排查
7.1. 常见问题
7.2. 日志排查
# 查看exporter日志
kubectl logs -f deployment/prometheus-mysql-exporter -n monitoring
# 查看Prometheus日志
kubectl logs -f prometheus-prometheus-kube-prometheus-prometheus-0 -n monitoring
# 检查ServiceMonitor状态
kubectl describe servicemonitor prometheus-mysql-exporter -n monitoring
总结
通过Helm部署MySQL监控方案具有以下优势:
- 简化部署:一次性配置多个MySQL实例监控
 - 统一管理:集中化的配置管理和版本控制
 - 安全存储:敏感信息通过Kubernetes Secret安全存储
 - 灵活扩展:支持动态添加和移除监控实例
 - 标准化:统一的命名规范和标签体系
 
本方案大大降低了MySQL监控的运维复杂度,提升了监控系统的可维护性和可扩展性。
参考文档
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 逐光の博客!
 评论




