使用Prometheus搭建监控系统

使用Prometheus搭建监控系统

Prometheus搭建

安装Prometheus

prometheus.io/download/ 下载安装包

// 解压
tar xvfz prometheus-2.35.0.linux-amd64.tar.gz

// 运行
cd prometheus-2.35.0.linux-amd64
nohup ./prometheus --config.file=prometheus.yml --web.listen-address=:10090(端口可以改变) &

// 校验运行是否成功
访问http://ip:10090
复制代码

配置Prometheus

原有的yml文件

# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    static_configs:
    - targets: ['localhost:9090']
复制代码

Prometheus 默认的配置文件分为四大块:

  • global 块:Prometheus 的全局配置,比如 scrape_interval 表示 Prometheus 多久抓取一次数据,evaluation_interval 表示多久检测一次告警规则;
  • alerting 块:关于 Alertmanager 的配置,这个我们后面再看;
  • rule_files 块:告警规则,这个我们后面再看;
  • scrape_config 块:这里定义了 Prometheus 要抓取的目标,我们可以看到默认已经配置了一个名称为 prometheus 的 job,这是因为 Prometheus 在启动的时候也会通过 HTTP 接口暴露自身的指标数据,这就相当于 Prometheus 自己监控自己,虽然这在真正使用 Prometheus 时没啥用处,但是我们可以通过这个例子来学习如何使用 Prometheus;可以访问 http://localhost:9090/metrics 查看 Prometheus 暴露了哪些指标;

注意事项

服务器10090端口是否暴露可用

查看已经开放的端口:
firewall-cmd --list-ports

开启端口
firewall-cmd --zone=public --add-port=10090(这个端口是暴露的端口)/tcp --permanent

重启firewall
firewall-cmd --reload 
复制代码

Grafana搭建

grafana.com/grafana/dow… 下载安装包

// 解压
tar xvfz grafana-enterprise-8.5.0.linux-amd64.tar.gz

// 运行
cd ./grafana-8.5.0/bin
nohup ./grafana-server &

// 开放端口3000(参照之前操作)

// 检查是否启动成功
访问http://ip:3000 登录用户名密码admin/admin
复制代码

添加Prometheus数据源

Grafana-1.png Grafana-2.png

服务器信息监控

安装服务器监控插件

prometheus.io/download/ 下载node_exporter插件

// 解压
tar xvfz node_exporter-1.3.1.linux-amd64.tar.gz

// 运行
cd ./node_exporter-1.3.1.linux-amd64
nohup ./node_exporter &

// 开放端口9100

// 检查是否启动成功
访问http://ip:9100/metrics 
复制代码

配置Prometheus拉取服务节点信息

// 修改配置文件
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['ip:10090']
  - job_name: 'server'
    static_configs:
      - targets: ['ip:9100']

// 重启Prometheus
复制代码

访问http://ip:10090 检查是否生效 node-export-1.png

配置Grafana仪表盘

grafana.com/grafana/das… 下载node服务器监控仪表盘模板,然后将模板导入到Grafana中 import-1.png import-2.png 最终仪表盘效果 node-info-show.png

mysql信息监控

安装MySql信息监控插件

prometheus.io/download/ 下载mysql_exporter插件

// 解压
tar xvfz mysqld_exporter-0.14.0.linux-amd64.tar.gz

cd ./mysqld_exporter-0.14.0.linux-amd64
// 生成监听数据库信息文件 my.cnf 并进行以下配置
[client]
host=***
port=***
user=root
password=root

// 启动插件
nohup ./mysqld_exporter --web.listen-address=0.0.0.0:9104 --config.my-cnf=my.cnf &

// 开放端口9104

// 检查是否启动成功
访问http://ip:9104/metrics 
复制代码

配置Prometheus拉取服务节点信息

// 修改配置文件
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['ip:10090']
  - job_name: 'server'
    static_configs:
      - targets: ['ip:9100']
  增加mysql的监听
  - job_name: 'mysql'
    static_configs:
      - targets: ['ip:9104']     

// 重启Prometheus
复制代码

配置Grafana仪表盘

最终仪表盘效果 mysql-info-show.png

RabbitMQ信息监控

安装RabbitMQ信息监控插件

github.com/kbudde/rabb… 下载安装包

// 解压
tar xvfz rabbitmq_exporter_1.0.0-RC13_linux_amd64.tar.gz

// 启动插件
RABBIT_USER=zat RABBIT_PASSWORD=zat123 OUTPUT_FORMAT=json PUBLIC_PORT=9419 RABBIT_URL=http://localhost:15672 nohup ./rabbitmq_exporter &

// 开放端口9419

// 检查是否启动成功
访问http://ip:9419/metrics 
复制代码

配置Prometheus拉取服务节点信息

// 修改配置文件
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['ip:10090']
  - job_name: 'server'
    static_configs:
      - targets: ['ip:9100']
  - job_name: 'mysql'
    static_configs:
      - targets: ['ip:9104']
   增加rabbit的监听配置
  - job_name: "rabbit"
    static_configs:
      - targets: ["ip:9419"]
     

// 重启Prometheus
复制代码

配置Grafana仪表盘

最终仪表盘效果 rabbit-info-show.png

Redis信息监控

安装Redis信息监控插件

github.com/oliver006/r… 下载安装包

// 解压
tar xvfz redis_exporter-v1.37.0.linux-amd64.tar.gz

// 启动插件
cd ./redis_exporter-v1.37.0.linux-amd64
nohup ./redis_exporter -redis.addr redis的IP:6379 -redis.password xxx &

// 开放端口9121

// 检查是否启动成功
访问http://ip:9121/metrics 
复制代码

配置Prometheus拉取服务节点信息

// 修改配置文件
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['ip:10090']
  - job_name: 'server'
    static_configs:
      - targets: ['ip:9100']
  - job_name: 'mysql'
    static_configs:
      - targets: ['ip:9104']
  - job_name: "rabbit"
    static_configs:
      - targets: ["ip:9419"]
  增加对redis信息的监听
  - job_name: "redis"
    static_configs:
      - targets: ["localhost:9121"]

// 重启Prometheus
复制代码

配置Grafana仪表盘

最终仪表盘效果 redis-info-show.png

java应用服务监控(基于eureka)

引入暴露接口的jar包

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.5.12</version>
        </dependency>
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
            <version>1.7.10</version>
        </dependency>
复制代码

修改服务的配置

management:
  endpoints:
    web:
      exposure:
        # 将 Actuator 的 /actuator/prometheus 端点暴露出来
        include: 'prometheus,metrics,info,health'
  metrics:
    tags:
      # 为指标设置一个Tag,这里设置为应用名,Tag是Prometheus提供的一种能力,从而实现更加灵活的筛选
      application: ${spring.application.name}

# 方便prometheus使用注册中心实现服务发现
eureka:
  instance:
    metadata-map:
      "prometheus.scrape": "true"
      "prometheus.path": "/actuator/prometheus"
      "prometheus.port": ${server.port}
      "sys.module": "base-test"
复制代码

配置Prometheus拉取服务节点信息

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['ip:10090']
  - job_name: 'server'
    static_configs:
      - targets: ['ip:9100']
  - job_name: 'mysql'
    static_configs:
      - targets: ['ip:9104']
  - job_name: "rabbit"
    static_configs:
      - targets: ["ip:9419"]
  - job_name: "redis"
    static_configs:
      - targets: ["ip:9121"]
  - job_name: "ta"
  // 访问权限
    basic_auth:
      username: xxx
      password: xxx
    eureka_sd_configs:
    // 注册中心
      - server: "http://xxx:xxx@localhost:8761/eureka"
    relabel_configs:
    // 暴露的服务信息接口替换
      - source_labels: ["__meta_eureka_app_instance_metadata_prometheus_path"]
        action: replace
        target_label: __metrics_path__
        regex: (.+)
      - source_labels: ["__meta_eureka_app_instance_metadata_sys_module"]
        action: replace
        target_label: sys_module
        regex: (.+)
复制代码

配置Grafana仪表盘

最终仪表盘效果 java-info-show.png

参考文献

dbaplus.cn/news-134-40… bbs.huaweicloud.com/blogs/26883… www.modb.pro/db/15435 www.cnblogs.com/caoweixiong… blog.51cto.com/root/303495…

猜你喜欢

转载自juejin.im/post/7095578954660053006