Prometheus实战教程:k8s平台-Mysql监控案例

配置文件优化后的 Prometheus 自动发现 MySQL 实例的完整 YAML 文件。该配置包括:

  1. MySQL Exporter 部署:使用 ConfigMap 提供 MySQL 连接信息。
  2. 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_passwordmysqld-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. 部署步骤

  1. 创建 ConfigMap

    kubectl apply -f mysql-exporter-config.yaml
  2. 部署 MySQL Exporter:

    kubectl apply -f mysql-exporter-deployment.yaml
  3. 更新 Prometheus 配置文件(prometheus.yml),添加 MySQL 的自动发现配置。

  4. 重启 Prometheus 以加载新配置。


8. 验证

  1. 检查 mysql-exporter 容器日志:

    kubectl logs <mysql-exporter-pod-name> -c mysql-exporter
    • 确保没有错误日志。
  2. 检查 Pod 状态:

    kubectl get pods
    • 确保 mysql-exporter 容器处于 Running 状态。
  3. 访问 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."

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/83766.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

基于区块链技术的智能汽车诊断与性能分析

我是穿拖鞋的汉子&#xff0c;魔都中坚持长期主义的汽车电子工程师。 老规矩&#xff0c;分享一段喜欢的文字&#xff0c;避免自己成为高知识低文化的工程师&#xff1a; 钝感力的“钝”&#xff0c;不是木讷、迟钝&#xff0c;而是直面困境的韧劲和耐力&#xff0c;是面对外界…

文字溢出省略号显示

一、 单行文字溢出、省略号显示 二、 多行文字溢出&#xff0c;省略号显示 有较大的兼容性问题&#xff0c;适用于Webkit为内核的浏览器软件&#xff0c;或者移动端的&#xff08;大部分也是webkit&#xff09; 此效果建议后端人员开发 三、图片底侧空白缝隙的修复技巧&#…

JavaScript 中使用 Elasticsearch 的正确方式,第一部分

作者&#xff1a;来自 Elastic Jeffrey Rengifo 讲解如何用 JavaScript 创建一个可用于生产环境的 Elasticsearch 后端。 想获得 Elastic 认证&#xff1f;看看下一期 Elasticsearch 工程师培训什么时候开始吧&#xff01; Elasticsearch 拥有大量新功能&#xff0c;能帮助你…

RAG-MCP:突破大模型工具调用瓶颈,告别Prompt膨胀

大语言模型&#xff08;LLM&#xff09;的浪潮正席卷全球&#xff0c;其强大的自然语言理解、生成和推理能力&#xff0c;为各行各业带来了前所未有的机遇。然而&#xff0c;正如我们在之前的探讨中多次提及&#xff0c;LLM并非万能。它们受限于训练数据的时效性和范围&#xf…

鸿蒙OSUniApp制作一个小巧的图片浏览器#三方框架 #Uniapp

利用UniApp制作一个小巧的图片浏览器 最近接了个需求&#xff0c;要求做一个轻量级的图片浏览工具&#xff0c;考虑到多端适配的问题&#xff0c;果断选择了UniApp作为开发框架。本文记录了我从0到1的开发过程&#xff0c;希望能给有类似需求的小伙伴一些参考。 前言 移动互联…

Python爬虫实战:获取taobao网最新rtx5060ti显卡销量数据并分析,为消费者做参考

一、系统定义与技术架构 1.1 系统定义 本系统是基于 Python 开发的电商数据采集与分析工具,旨在通过模拟用户行为实现淘宝平台 50 系列显卡(以 RTX 5060 Ti 为例)销售数据的自动化获取、清洗、分析及可视化。核心功能包括: 自动登录:通过 Selenium 模拟浏览器操作完成账…

OCframework编译Swift

建一个OC的framework&#xff1a; 需要对外暴露的OC文件&#xff0c;需要放到OC的.h文件中 framework中&#xff0c;OC类&#xff0c;调用framework中的Swift类&#xff1a; #import "WowAudioFocus/WowAudioFocus-Swift.h" //02 #import "{工程名}/{工程…

每日算法 -【Swift 算法】Two Sum 问题:从暴力解法到最优解法的演进

【Swift 算法】Two Sum 问题&#xff1a;从暴力解法到最优解法的演进 本文通过“Two Sum”问题&#xff0c;带你了解如何从最直观的暴力解法&#xff0c;逐步优化到高效的哈希表解法&#xff0c;并对两者进行对比&#xff0c;适合算法入门和面试准备。 &#x1f4a1; 问题描述 …

【保姆级】Nginx简介以及安装

Nginx简介 ​ Nginx是一个高性能的HTTP和反向代理web服务器&#xff0c;同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔赛索耶夫为俄罗斯访问量第二的Rambler.ru站点&#xff08;俄文&#xff1a;Рамблер&#xff09;开发的&#xff0c;第一个公开版本0.1.0发布于20…

C++(25): 标准库 <deque>

目录 1、 核心概念 2. 基本语法 3. 特点 4. 特有成员函数 5. 内存与性能 6. 示例代码 7. 成员函数列表 8. 使用场景 9. 注意事项 1、 核心概念 双端队列(Double-Ended Queue,deque) 是一种允许在队列头部和尾部高效插入和删除元素的线性数据结构,同时支持随机访问。…

软件设计师关系代数和元组演算(关联、笛卡尔积、除、映射、分段等问题)考点分析——求三连

一、考点分值占比与趋势分析 综合知识历年统计表 年份考题数量分值分值占比考察重点2018334%自然连接、投影、选择2019222.67%笛卡尔积、条件筛选2020111.33%属性列计算2021334%关系运算综合应用2022222.67%元组演算表达式2023222.67%差运算、连接类型2024111.33%除法运算应用…

卸载云枢(MacOS 版)

删除 APP 和相关文件 sudo chflags -R noschg /Applications/Yunshu.app 2>/dev/null sudo rm -rf /Applications/Yunshu.app sudo rm -rf /Library/Application\ Support/EagleCloud sudo rm -rf /Library/LaunchAgents/com.eagleyun.endpoint.agent.plist sudo rm -rf /L…

在 Ubuntu 20.04 中使用 init.d 或者systemd实现开机自动执行脚本

Ubuntu 20 默认使用的是 systemd 系统管理器&#xff0c;但传统的 SysV Init&#xff08;/etc/init.d/&#xff09;脚本依然兼容并可用。本文将介绍如何通过 init.d 写脚本来在开机时自动设置某个 GPIO&#xff08;如 GPIO407&#xff09;为高电平&#xff0c;适用于嵌入式系统…

苹果的人工智能领域慢热

每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗&#xff1f;订阅我们的简报&#xff0c;深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同&#xff0c;从行业内部的深度分析和实用指南中受益。不要错过这个机会&#xff0c;成为AI领…

FastAPI使用@app.get/@app.post等装饰器注册路由无效404 Not Found

&#xff08;一&#xff09;问题描述 使用app.get注册路由&#xff0c;前端发送请求访问路径和路由一致&#xff0c;但一直显示404 Not Found&#xff0c;检查了好几遍&#xff0c;确认没有访问路径不一致的问题。 在Swagger文档里查看&#xff0c;也没有找到这个新添加的接口。…

制作我的计算器

1. 界面布局 新建项目 MyCalculator&#xff0c;开始布局。 2. 静态布局 代码如下&#xff1a; // etc/pages/Index.ets Entry Component struct Index {build() {Column() {/*** 运算区*/Column() {TextInput({ text: 12x13 }).height(100%).fontSize(32).enabled(false).f…

2025-5-17Vue3快速上手

1、ref对比reactive 区别第2点&#xff1a;本质是指针指向问题 整体修改reactive的数据时&#xff0c;有坑 使用原则需要根据项目原本的代码灵活参考 如果要更新的数据是从服务器获取回来的&#xff0c;用Object.assign是好方法&#xff0c;需要注意的是&#xff1a;Object.a…

深度学习---模型预热(Model Warm-Up)

一、基本概念与核心定义 模型预热是指在机器学习模型正式训练或推理前&#xff0c;通过特定技术手段使模型参数、计算图或运行环境提前进入稳定状态的过程。其本质是通过预处理操作降低初始阶段的不稳定性&#xff0c;从而提升后续任务的效率、精度或性能。 核心目标&#xf…

加载渲染geojson数据

本节我们学习如何在cesium中加载geojson数据 想要加载geojson数据首先要有数据源,我们以中国地图为例 复制数据的geo api 在cesium的官网库中查询 可以看到如何在cesium中导入数据的方法 //加载geojson数据let dataGeo Cesium.GeoJsonDataSource.load("https://geo.dat…

python:pymysql概念、基本操作和注入问题讲解

python&#xff1a;pymysql分享目录 一、概念二、数据准备三、安装pymysql四、pymysql使用&#xff08;一&#xff09;使用步骤&#xff08;二&#xff09;查询操作&#xff08;三&#xff09;增&#xff08;四&#xff09;改&#xff08;五&#xff09;删 五、关于pymysql注入…