Prometheus+Grafana 入门

Prometheus

安装

  1. 下载:https://github.com/prometheus/prometheus/releases/download/v2.29.1/prometheus-2.29.1.linux-amd64.tar.gz

  2. 上传到服务器,解压

    1
    2
    tar -xvzf prometheus-2.29.1.linux-amd64.tar.gz
    mv prometheus-2.29.1.linux-amd64 prometheus
  3. 安装node_exporter,下载:https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz

    上传到服务器,解压并启动

    1
    2
    3
    4
    5
    tar -zvxf node_exporter-1.3.1.linux-amd64.tar.gz
    mv node_exporter-1.3.1.linux-amd64/ node_exporter
    nohup /usr/local/node_exporter/node_exporter &
    #检查是否启动
    netstat -nltp |grep 9100
  4. 修改Prometheus配置文件prometheus.yml

    image-20230626160623740

  5. 启动

    1
    2
    3
    ./prometheus --config.file="prometheus.yml" &
    #检查是否启动
    ps -ef |grep prometheus

使用

访问地址:http://192.168.0.153:9090/

Targets

image-20230626160844717

点开 http://192.168.0.153:9100/metrics,展示各种指标

image-20230626161040923

Graph

上面的指标可以在这里以表格/折线图的形式查询

image-20230626161206879

Grafana

安装

  1. 下载:https://dl.grafana.com/oss/release/grafana-9.2.3.linux-amd64.tar.gz

  2. 上传到服务器,解压

    1
    2
    tar -zvxf grafana-9.2.3.linux-amd64.tar.gz
    mv grafana-9.2.3/ grafana/
  3. 创建Grafana服务

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    cat > /usr/lib/systemd/system/grafana-server.service <<EOF
    [Unit]
    Description=Grafana
    After=network.target
    [Service]
    Type=notify
    ExecStart=/usr/local/grafana/bin/grafana-server -homepath /usr/local/grafana
    Restart=on-failure
    [Install]
    WantedBy=multi-user.target
    EOF
  4. 启动

    1
    2
    systemctl enable grafana-server.service
    systemctl start grafana-server.service

使用

http://192.168.0.153:3000/

默认账户/密码:admin/admin

  1. 配置数据源

    image-20230626161831120

    image-20230626161924360

  2. 导入Dashboard

    image-20230626162013671

    如导入id为9276的Dashboard:https://grafana.com/grafana/dashboards/9276-1-cpu/

    image-20230626162111842

    image-20230626162141717

整合SpringBoot应用

  1. 引入依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
    <version>1.7.2</version>
    </dependency>
  2. 增加配置

    1
    management.endpoints.web.exposure.include=*
  3. 开放actuator endpoint

    image-20230626162517471

  4. 重启,检查actuator endpoint是否生效

    image-20230626162635909

  5. 修改Prometheus配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    scrape_configs:
    - job_name: "prometheus"
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    static_configs:
    - targets: ["localhost:9090","192.168.0.153:9100"]
    - job_name: "data-fill"
    scrape_interval: 5s #每5s抓取一次
    metrics_path: '/api/actuator/prometheus' #抓取的数据url
    static_configs:
    - targets: ["192.168.0.153:6868"]
  6. 导入Dashboard

    id:4701 https://grafana.com/grafana/dashboards/4701-jvm-micrometer/

    image-20230626163026361

请作者喝瓶肥宅快乐水