1. 环境搭建

    • 安装 Prometheus

      • 下载安装包:从 Prometheus 官方网站(https://prometheus.io/download/)下载适合你操作系统的二进制文件。例如,在 Linux 系统下:
       wget https://github.com/prometheus/prometheus/releases/download/v2.34.0/prometheus - 2.34.0.linux - amd64.tar.gz
  • 解压并配置

       tar -zxvf prometheus - 2.34.0.linux - amd64.tar.gz
       cd prometheus - 2.34.0.linux - amd64

编辑prometheus.yml配置文件,这是 Prometheus 的核心配置文件。可以在其中定义要监控的目标,例如监控本地主机的节点信息(Node Exporter)和应用程序暴露的自定义指标。以下是一个简单的示例:

       global:
         scrape_interval: 15s # 抓取间隔
       scrape_configs:
         - job_name: 'node'
           static_configs:
             - targets: ['localhost:9100'] # 假设Node Exporter在9100端口暴露指标
         - job_name: 'my_app'
           static_configs:
             - targets: ['localhost:8080/metrics'] # 假设应用在8080端口的/metrics路径暴露指标
  • 启动 Prometheus

      ./prometheus --config.file=prometheus.yml
  • 安装 Grafana

    • 下载安装包:从 Grafana 官方网站(https://grafana.com/grafana/download)下载适合你操作系统的安装包。例如,对于 Linux 系统下的安装包:
       wget https://dl.grafana.com/oss/release/grafana - 8.3.0 - 1.x86_64.rpm
  • 安装并启动

       sudo yum -y install grafana - 8.3.0 - 1.x86_64.rpm
       sudo systemctl start grafana - server
  1. Prometheus 数据采集与存储

  • 指标采集:Prometheus 按照prometheus.yml配置文件中定义的scrape_interval,定期从目标(如localhost:9100的 Node Exporter 和localhost:8080/metrics的应用自定义指标)抓取指标数据。这些指标数据是基于 Prometheus 的指标格式定义的,例如node_cpu_usage(节点 CPU 使用率)、my_app_request_count(应用请求计数)等。
  • 数据存储:Prometheus 将采集到的指标数据存储在本地的时间序列数据库中。存储的数据结构基于时间戳和指标名称,以及对应的标签(用于区分不同维度的数据)。例如,node_cpu_usage{instance="localhost:9100", cpu="cpu0"}表示在localhost:9100这个实例上,CPU0 的使用率指标。
  1. Grafana 数据可视化

  • 添加数据源:

    • 登录 Grafana Web 界面(默认地址是http://localhost:3000,初始用户名和密码是admin)。
    • Configuration - Data Sources中添加 Prometheus 数据源。填写 Prometheus 服务器的地址(如http://localhost:9090),然后点击Save & Test确保连接成功。
  • 创建仪表盘(Dashboard):

    • 在 Grafana 中,可以通过导入已有的仪表盘模板或者自己创建仪表盘。例如,要创建一个简单的节点 CPU 使用率监控仪表盘,点击+ - Dashboard - Add new panel
    • 在面板编辑中,选择数据源为之前添加的 Prometheus,在查询编辑器(Query Editor)中输入 PromQL(Prometheus Query Language)查询,如node_cpu_usage来获取节点 CPU 使用率指标。
    • 根据需要设置图表的类型(如折线图、柱状图等)、时间范围、坐标轴标签等可视化参数,然后保存仪表盘。
  1. 实现自动监控报警

  • 配置 Prometheus Alertmanager

    • 下载安装 Alertmanager:从 Prometheus 官方网站(https://prometheus.io/download/)下载 Alertmanager 二进制文件。例如,在 Linux 系统下:
       wget https://github.com/prometheus/alertmanager/releases/download/v0.23.0/alertmanager - 0.23.0.linux - amd64.tar.gz
  • 解压并配置

       tar -zxvf alertmanager - 0.23.0.linux - amd64.tar.gz
       cd alertmanager - 0.23.0.linux - amd64

编辑alertmanager.yml配置文件,这是 Alertmanager 的核心配置文件。以下是一个简单的配置示例,用于发送邮件报警:

       global:
         smtp_smarthost: 'smtp.example.com:587' # 邮件服务器地址和端口
         smtp_from: 'alerts@example.com' # 发件人邮箱
         smtp_auth_username: 'alerts@example.com' # 用于认证的用户名
         smtp_auth_password: 'password' # 认证密码
       route:
         receiver: 'email - receiver'
         group_by: ['alertname']
         group_wait: 30s
         group_interval: 5m
         repeat_interval: 1h
       receivers:
         - name: 'email - receiver'
           email_configs:
             - to: 'admin@example.com' # 收件人邮箱
               subject: 'Prometheus Alert'
  • 启动 Alertmanager

      ./alertmanager --config.file=alertmanager.yml
  • 在 Prometheus 中配置报警规则:

    • 编辑 Prometheus 的prometheus.yml配置文件,添加报警规则文件的路径,例如:
       rule_files:
         - "alert.rules.yml"
  • 创建alert.rules.yml文件,定义报警规则。例如,当节点 CPU 使用率超过 80% 时触发报警:
       groups:
         - name: cpu - usage - alerts
           rules:
             - alert: HighCPUUsage
               expr: node_cpu_usage > 0.8
               for: 2m # 持续时间达到2分钟才触发报警
               labels:
                 severity: critical
               annotations:
                 summary: "High CPU Usage Detected"
                 description: "CPU usage on the node has exceeded 80% for more than 2 minutes."

默认端口:9090

文档:https://prometheus.ac.cn/docs/prometheus/latest/getting_started/#using-the-expression-browser

使用说明:https://blog.csdn.net/qq_31725371/article/details/114697770