Prometheus+Grafana实时监控系统各项指标

一、监控架构设计

核心组件与数据流

  • Prometheus:时序数据采集、存储与告警规则管理
  • Node Exporter:采集主机指标(CPU、内存、磁盘、网络等)
  • 数据库Exporter:如 mysqld_exporterpostgres_exporter
  • Grafana:数据可视化与仪表盘展示
  • Alertmanager(可选):告警通知管理

二、主机环境准备

1. 系统要求

  • Linux系统(推荐CentOS 7+/Ubuntu 20.04+)
  • 开放端口:9090(Prometheus)、3000(Grafana)、9100(Node Exporter)
  • 确保所有节点时间同步(NTP服务)
# CentOS安装NTP
sudo yum install ntp
sudo systemctl start ntpd
sudo systemctl enable ntpd# Ubuntu安装NTP
sudo apt install ntp
sudo systemctl restart ntp

三、组件安装与配置

1. 安装Prometheus Server

下载二进制包
wget https://github.com/prometheus/prometheus/releases/download/v2.39.1/prometheus-2.39.1.linux-amd64.tar.gz
tar xvfz prometheus-*.tar.gz
sudo mv prometheus-2.39.1.linux-amd64 /usr/local/prometheus
创建系统服务
sudo useradd --no-create-home --shell /bin/false prometheus
sudo mkdir /etc/prometheus /var/lib/prometheus
sudo chown prometheus:prometheus /var/lib/prometheus# 创建service文件
sudo cat <<EOF > /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target[Service]
User=prometheus
Group=prometheus
ExecStart=/usr/local/prometheus/prometheus \--config.file=/etc/prometheus/prometheus.yml \--storage.tsdb.path=/var/lib/prometheus \--web.listen-address=0.0.0.0:9090Restart=always[Install]
WantedBy=multi-user.target
EOF# 配置Prometheus
sudo cp /usr/local/prometheus/prometheus.yml /etc/prometheus/
sudo chown -R prometheus:prometheus /etc/prometheus# 启动服务
sudo systemctl daemon-reload
sudo systemctl start prometheus
sudo systemctl enable prometheus

2. 部署Node Exporter(所有节点)

下载安装
wget https://github.com/prometheus/node_exporter/releases/download/v1.4.0/node_exporter-1.4.0.linux-amd64.tar.gz
tar xvfz node_exporter-*.tar.gz
sudo mv node_exporter-1.4.0.linux-amd64/node_exporter /usr/local/bin/
sudo useradd -rs /bin/false node_exporter
创建系统服务
sudo cat <<EOF > /etc/systemd/system/node_exporter.service
[Unit]
Description=Node Exporter
After=network.target[Service]
User=node_exporter
Group=node_exporter
ExecStart=/usr/local/bin/node_exporterRestart=always[Install]
WantedBy=multi-user.target
EOFsudo systemctl daemon-reload
sudo systemctl start node_exporter
sudo systemctl enable node_exporter

3. 配置Prometheus抓取规则

编辑 /etc/prometheus/prometheus.yml

scrape_configs:- job_name: 'node'static_configs:- targets: ['node1:9100', 'node2:9100', 'node3:9100']

重启Prometheus生效:

sudo systemctl restart prometheus

四、数据库监控配置(以MySQL为例)

1. 安装mysqld_exporter

wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.14.0/mysqld_exporter-0.14.0.linux-amd64.tar.gz
tar xvfz mysqld_exporter-*.tar.gz
sudo mv mysqld_exporter-0.14.0.linux-amd64/mysqld_exporter /usr/local/bin/
sudo useradd -rs /bin/false mysqld_exporter

2. 创建监控用户

CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'SecurePass123!' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';

3. 创建环境变量文件

sudo mkdir /etc/mysqld_exporter
sudo cat <<EOF > /etc/mysqld_exporter/.my.cnf
[client]
user=exporter
password=SecurePass123!
EOF

4. 创建系统服务

sudo cat <<EOF > /etc/systemd/system/mysqld_exporter.service
[Unit]
Description=MySQL Exporter
After=network.target[Service]
User=mysqld_exporter
EnvironmentFile=/etc/mysqld_exporter/.my.cnf
ExecStart=/usr/local/bin/mysqld_exporter \--config.my-cnf="%a" \--web.listen-address=0.0.0.0:9104Restart=always[Install]
WantedBy=multi-user.target
EOFsudo systemctl daemon-reload
sudo systemctl start mysqld_exporter
sudo systemctl enable mysqld_exporter

五、安装与配置Grafana

1. 安装Grafana(CentOS)

sudo tee /etc/yum.repos.d/grafana.repo <<EOF
[grafana]
name=grafana
baseurl=https://packages.grafana.com/oss/rpm
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://packages.grafana.com/gpg.key
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
EOFsudo yum install grafana
sudo systemctl start grafana-server
sudo systemctl enable grafana-server

2. 配置Grafana数据源

  1. 访问 http://<服务器IP>:3000,默认账号 admin/admin
  2. 左侧菜单 → Configuration → Data Sources → Add data source
  3. 选择 Prometheus,填写URL http://localhost:9090
  4. 点击 Save & Test

六、导入监控仪表盘

1. 主机监控仪表盘

  • Node Exporter Full:ID 1860
  • Linux Hosts Metrics:ID 11074

2. MySQL监控仪表盘

  • MySQL Overview:ID 7362
  • Percona MySQL:ID 11323

操作步骤

  1. 左侧菜单 → Create → Import
  2. 输入仪表盘ID → Load
  3. 选择Prometheus数据源 → Import

七、安全加固

1. 防火墙配置

# CentOS
sudo firewall-cmd --permanent --add-port=3000/tcp
sudo firewall-cmd --permanent --add-port=9090/tcp
sudo firewall-cmd --reload# Ubuntu
sudo ufw allow 3000/tcp
sudo ufw allow 9090/tcp
sudo ufw reload

2. Grafana反向代理(Nginx示例)

server {listen 80;server_name grafana.yourdomain.com;location / {proxy_pass http://localhost:3000;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;}
}

八、告警配置示例

1. 创建告警规则文件

sudo cat <<EOF > /etc/prometheus/alerts.yml
groups:
- name: host-alertsrules:- alert: HighMemoryUsageexpr: (node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes) / node_memory_MemTotal_bytes * 100 > 85for: 5mlabels:severity: warningannotations:summary: "内存使用率过高 (实例 {{ $labels.instance }})"description: "内存使用率超过85%持续5分钟"
EOF

2. 修改Prometheus配置

# /etc/prometheus/prometheus.yml
rule_files:- alerts.yml

重启服务:

sudo systemctl restart prometheus

九、故障排查指南

1. 服务状态检查

sudo systemctl status prometheus
sudo systemctl status node_exporter
sudo systemctl status mysqld_exporter

2. 日志查看

# Prometheus日志
journalctl -u prometheus -f# Node Exporter日志
journalctl -u node_exporter -f# MySQL Exporter日志
journalctl -u mysqld_exporter -f

十、总结

通过原生安装方式,您已构建完整的监控系统:

  • 资源监控:实时掌握CPU、内存、磁盘等指标
  • 数据库监控:跟踪查询性能、连接数、复制状态
  • 告警通知:配置阈值触发邮件/钉钉通知
  • 安全加固:通过防火墙和反向代理保护服务

后续扩展方向

  • 集成Alertmanager实现多通道告警
  • 监控Redis、Kafka等中间件
  • 部署长期存储(如Thanos)管理历史数据

资源参考

  • Prometheus官方文档
  • Grafana仪表盘库

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

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

相关文章

[密码学基础]GMT 0029-2014签名验签服务器技术规范深度解析

GMT 0029-2014签名验签服务器技术规范深度解析 引言 在数字化转型和网络安全需求激增的背景下&#xff0c;密码技术成为保障数据完整性与身份认证的核心手段。中国密码管理局发布的GMT 0029-2014《签名验签服务器技术规范》&#xff0c;为签名验签服务器的设计、开发与部署提…

多路转接select服务器

目录 select函数原型 select服务器 select的缺点 前面介绍过多路转接就是能同时等待多个文件描述符&#xff0c;这篇文章介绍一下多路转接方案中的select的使用 select函数原型 #include <sys/select.h> int select(int nfds, fd_set *readfds, fd_set *writefds, f…

QT6 源(45):分隔条 QSplitter 允许程序的用户修改布局,程序员使用 IDE时,就是分隔条的用户,以及其 QSplitter 源代码

&#xff08;1&#xff09; &#xff08;2&#xff09;本类的继承关系如下&#xff0c;所以说分隔条属于容器&#xff1a; &#xff08;3&#xff09;本类的属性&#xff1a; &#xff08;4&#xff09; 这是一份 QSplitter 的举例代码&#xff0c;注意其构造函数时候的传参&am…

VSCode PIO使用Jlink SWD烧录Stm32

一、背景 PIO的编译速度比Arduino快很多&#xff0c;同样支持Arduino的语法。VScode的自动补全和插件也能够帮助快速开发目前使用JLINK SWD的方式连接STM32 二、配置 在ini配置文件中&#xff0c;添加如下内容 [env:genericSTM32F103C8] platform ststm32 board genericS…

JavaScript 渲染内容爬取:Puppeteer 入门

在现代网络应用中&#xff0c;许多网页内容是通过 JavaScript 渲染生成的&#xff0c;传统的爬虫工具往往难以获取这些动态内容。Puppeteer 作为一种强大的浏览器自动化工具&#xff0c;为这一问题提供了优雅的解决方案。本文将带你入门 Puppeteer&#xff0c;介绍如何安装、启…

卷积神经网络:视觉炼金术士的数学魔法

引言&#xff1a;当数学遇见视觉炼金术 在人工智能的奇幻世界里&#xff0c;卷积神经网络&#xff08;CNN&#xff09;犹如掌握视觉奥秘的炼金术士&#xff0c;将原始像素的"铅块"淬炼成认知的"黄金"。这种融合数学严谨性与生物灵感的算法架构&#xff0c…

Android Cordova 开发 - Cordova 快速入门(Cordova 环境配置、Cordova 第一个应用程序)

一、Cordova 1、Cordova 概述 Cordova 是使用 HTML&#xff0c;CSS 和 JavaScript 构建混合移动应用程序的平台 2、Cordova 特征 &#xff08;1&#xff09;命令行界面&#xff08;Cordova CLI&#xff09; 这是可用于启动项目&#xff0c;构建不同平台的进程&#xff0c;…

ubuntu18.04启动不了修复

参考: 虚拟机里的Ubuntu18.4启动时进入到grub rescue救援模式&#xff08;无法正常进入到系统&#xff09;&#xff0c;ls查看后只有一个硬盘和分区&#xff0c;且无法找到/boot/grub文件【已解决】_ubuntu grub rescue-CSDN博客 本人fdisk错误使用,导致了grub启动不了 第一步…

SpringBoot3设置maven package直接打包成二进制可执行文件

注意事项 SpringBoot普通native打包顺序clean compile spring-boot:process-aot native:compile 使用以下配置只会的打包顺序clean package&#xff08;注意&#xff1a;使用此配置以后打包会有编译后的class文件、jar包、original源文件、二进制可执行文件【Linux是无后缀的包…

【华为】防火墙双击热备-之-主备模式-单外网线路

FW1和FW2的业务接口都工作在三层&#xff0c;上行连接二层交换机。上行交换机连接运营商的接入点&#xff0c;运营商为企业分配的IP地址为100.100.100.2。现在希望FW1和FW2以主备备份方式工作。正常情况下&#xff0c;流量通过FW1转发&#xff1b;当FW1出现故障时&#xff0c;流…

MYSQL之表的操作

1. 创建表 语法: CREATE TABLE table_name ( field1 datatype, field2 datatype, field3 datatype ) character set 字符集 collate 校验规则 engine 存储引擎; field 表示列名, datatype 表示列的类型character set 字符集, 如果没有指定字符集, 则以所在数据库的字符集为…

RAG进阶:Chroma开源的AI原生向量数据库

一、Chroma 核心概念与优势 1. 什么是 Chroma&#xff1f; Chroma 是一款开源的向量数据库&#xff0c;专为高效存储和检索高维向量数据设计。其核心能力在于语义相似性搜索&#xff0c;支持文本、图像等嵌入向量的快速匹配&#xff0c;广泛应用于大模型上下文增强&#xff0…

店匠科技摘得 36 氪“2025 AI Partner 创新大奖”

全场景 AI 方案驱动跨境电商数智化跃迁 4 月 18 日,36 氪 2025 AI Partner 大会于上海盛大开幕。大会紧扣“Super App 来了”主题,全力探寻 AI 时代的全新变量,探索 AI 领域下一个超级应用的无限可能性。在此次大会上,跨境电商独立站 SaaS 平台店匠科技(Shoplazza)凭借“店匠跨…

SQL技术终极指南:从内核原理到超大规模应用

一、DDL核心应用场景与最佳实践 1.1 表结构设计场景矩阵 业务场景核心语法要素典型实现案例电商用户画像JSON字段虚拟列索引CREATE TABLE users (id INT, profile JSON, AS (profile->>$.age) VIRTUAL, INDEX idx_age((profile->>$.age)))物联网时序数据分区表压…

吴恩达深度学习作业CNN之ResNet实现(Pytorch)

课程中认识许多CNN架构。首先是经典网络&#xff1a; LeNet-5AlexNetVGG 之后是近年来的一些网络&#xff1a; ResNetInceptionMobileNet 经典网络 LeNet-5 LeNet-5是用于手写数字识别&#xff08;识别0~9的阿拉伯数字&#xff09;的网络。它的结构如下&#xff1a; 网络…

FPGA入门学习Day1——设计一个DDS信号发生器

目录 一、DDS简介 &#xff08;一&#xff09;基本原理 &#xff08;二&#xff09;主要优势 &#xff08;三&#xff09;与传统技术的对比 二、FPGA存储器 &#xff08;一&#xff09;ROM波形存储器 &#xff08;二&#xff09;RAM随机存取存储器 &#xff08;三&…

SqlSugar与Entity Framework (EF)的SWOT分析

以下是基于 SWOT 分析法 对 SqlSugar 和 Entity Framework (EF) 的特性对比&#xff1a; SqlSugar 优势 (Strengths) 高性能&#xff1a; SqlSugar 以轻量化设计著称&#xff0c;执行速度更快&#xff0c;适合对性能要求较高的场景。在大数据量操作和复杂查询中表现优异。 易…

学习记录:DAY16

Maven 进阶与前端实战 前言 二轮考核的内容下来了&#xff0c;由整体项目构建转为实现特定模块的功能。对细节的要求更高了&#xff0c;而且有手搓线程池、手搓依赖注入等进阶要求&#xff0c;又有得学力。嘻嘻&#xff0c;太简单了&#xff0c;只要我手搓 Spring Boot 框架……

深度学习--卷积神经网络调整学习率

文章目录 前言一、学习率1、什么学习率2、什么是调整学习率3、目的 二、调整方法1、有序调整1&#xff09;有序调整StepLR(等间隔调整学习率)2&#xff09;有序调整MultiStepLR(多间隔调整学习率)3&#xff09;有序调整ExponentialLR (指数衰减调整学习率)4&#xff09;有序调整…

【消息队列RocketMQ】四、RocketMQ 存储机制与性能优化

一、RocketMQ 存储机制详解 1.1 存储文件结构​ RocketMQ 的存储文件主要分布在store目录下&#xff0c;该目录是在broker.conf配置文件中通过storePathRootDir参数指定的&#xff0c;默认路径为${user.home}/store 。主要包含以下几种关键文件类型&#xff1a;​ 1.1.1 Comm…