Prometheus与Grafana安装

1.Promethus的安装 1.1 下载并安装Prometheus 在Prometheus的GitHub仓库找到最新版本,https://github.com/prometheus/prometheus 下载并解压。 # macos arm版 curl -O https://github.com/

1.Promethus的安装

1.1 下载并安装Prometheus

在Prometheus的GitHub仓库找到最新版本,https://github.com/prometheus/prometheus 下载并解压。

# macos arm版
curl -O https://github.com/prometheus/prometheus/releases/download/v2.54.0/prometheus-2.54.0.darwin-arm64.tar.gz

# linux amd64版
wget https://github.com/prometheus/prometheus/releases/download/v2.54.0/prometheus-2.54.0.linux-amd64.tar.gz

# 解压
tar -zxvf prometheus-*.tar.gz

使用下面的命令去启动Prometheus,通过--config.file=./prometheus.yml指定Prometheus启动时使用的配置文件,通过--web.listen-address=":10090"配置指定端口号在10090启动。

./prometheus --config.file=./prometheus.yml --web.listen-address=":10090"

# 后台启动(指定端口号)
nohup ./prometheus --config.file=./prometheus.yml --web.listen-address=:10090 > info.log &

接着通过http://localhost:10090可以进入到Prometheus的首页。

1.2 配置Prometheus指标的抓取目标接口

使用Prometheus当然是想要实现监控指标的抓取,那么如何进行抓取的配置呢?此时我们就需要进行配置Prometheus的启动配置文件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:18080"]
    metrics_path: "/actuator/prometheus"

上面的配置当中,下面这部分是抓取接口的配置的接口,static_configs.target配置的是抓取的目标服务器的IP和端口号,metrics_path配置的是抓取的目标服务器的指标的接口(默认是/metrics),如果不是默认那么需要手动进行配置,比如我们使用spring-boot-actuator的方式进行接入,那么我们可以将接口配置成为/actuator/prometheus

# 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:18080"]
    metrics_path: "/actuator/prometheus"

通过http://localhost:10090/targets可以查看Prometheus的抓取目标的状态。

1.3 Prometheus界面指标的搜索

prometheus-targets-preview.png

http://localhost:10090页面当中,可以根据指标名进行指标值的查找。

prometheus-metrics-search.png

1.4 Prometheus的Exporter接入

在Prometheus当中,Exporter是一个桥接工具,负责从各种各样的应用当中采集指标,并通过HTTP接口的方式,将指标暴露给Prometheus进行指标的采集。

  • MySQL Exporter负责暴露MySQL Server的相关指标信息,通过用户连接MySQL Server从而查询到对应的指标信息,并将指标以Prometheus的格式使用HTTP接口的方式进行暴露。
  • Node Exporter负责将Linux服务器的相关指标(CPU、内存、硬盘、网络),以Prometheus的格式使用HTTP接口的方式进行暴露。

1.4.1 Prometheus配置MySQL指标抓取

可以使用Exporter的方式进行MySQL Exporter的指标的暴露,MySQL Exporter的Github仓库地址:https://github.com/prometheus/mysqld_exporter

首先我们需要在MySQL当中创建一个用户,使用这个用户专门让Prometheus登录进行指标的抓取。

CREATE USER 'mysql_exporter'@'localhost' IDENTIFIED BY '{password}';
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
FLUSH PRIVILEGES;

接着我们需要去Githubhttps://github.com/prometheus/mysqld_exporter/releases当中找到合适的版本的Exporter,并解压安装。

# macos arm版本
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.1/mysqld_exporter-0.15.1.darwin-arm64.tar.gz

# macos amd64版本
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.1/mysqld_exporter-0.15.1.darwin-amd64.tar.gz

# macos linux amd64版本
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.1/mysqld_exporter-0.15.1.linux-amd64.tar.gz

# 解压
tar -xvzf mysqld_exporter-*.tar.gz

接着我们需要创建mysqld_exporter的配置文件.my.cnf

cd config
sudo vim .my.cnf

我们需要往配置文件当中写入如下内容,其中的user和password是我们上面创建MySQL用户的信息。

[client]
user=exporter
password=your_password
host=localhost

接着我们可以通过如下的命令,启动mysqld_exporter

./mysqld_exporter --config.my-cnf=config/.my.cnf 

# 后台运行
nohup ./mysqld_exporter --config.my-cnf=config/.my.cnf > info.log &

启动日志如下,我们发现它会在9104端口上启动。

ts=2024-09-01T14:01:59.437Z caller=mysqld_exporter.go:220 level=info msg="Starting mysqld_exporter" version="(version=0.15.1, branch=HEAD, revision=cc349684494b5038ec5a52233bdca9eb9291e6f2)"
ts=2024-09-01T14:01:59.437Z caller=mysqld_exporter.go:221 level=info msg="Build context" build_context="(go=go1.21.5, platform=linux/amd64, user=root@d89c15b9f5ad, date=20231212-07:55:09, tags=unknown)"
ts=2024-09-01T14:01:59.437Z caller=mysqld_exporter.go:233 level=info msg="Scraper enabled" scraper=global_status
ts=2024-09-01T14:01:59.437Z caller=mysqld_exporter.go:233 level=info msg="Scraper enabled" scraper=global_variables
ts=2024-09-01T14:01:59.437Z caller=mysqld_exporter.go:233 level=info msg="Scraper enabled" scraper=slave_status
ts=2024-09-01T14:01:59.437Z caller=mysqld_exporter.go:233 level=info msg="Scraper enabled" scraper=info_schema.query_response_time
ts=2024-09-01T14:01:59.437Z caller=mysqld_exporter.go:233 level=info msg="Scraper enabled" scraper=info_schema.innodb_cmp
ts=2024-09-01T14:01:59.437Z caller=mysqld_exporter.go:233 level=info msg="Scraper enabled" scraper=info_schema.innodb_cmpmem
ts=2024-09-01T14:01:59.438Z caller=tls_config.go:274 level=info msg="Listening on" address=[::]:9104
ts=2024-09-01T14:01:59.438Z caller=tls_config.go:277 level=info msg="TLS is disabled." http2=false address=[::]:9104

当然,我们也可以通过命令行参数的方式去自定义MySQL Exporter的启动端口:

nohup ./mysqld_exporter --config.my-cnf=config/.my.cnf --web.listen-address=":9110" > info.log &

接着我们需要回到Prometheus的目录下修改Prometheus的配置文件,从而配置Prometheus去抓取mysqld_exporter的暴露的接口。

# 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:8090"]
    metrics_path: "/actuator/prometheus"

    # mysqld_exporter
  - job_name: "prometheus-mysqld-exporter"
    static_configs:
      - targets: ["localhost:9104"]
    metrics_path: "/metrics"

接着,我们就可以在Prometheus/Grafana的页面上查询到mysqld_exporter相关的指标,也可以使用curl http://localhost:9104/metrics在命令行查看到具体的指标信息。

1.4.2 Prometheus配置Linux指标接入

Linux指标,可以通过Node Exporter的方式接入Prometheus。Node Exporter的Github地址如下:https://github.com/prometheus/node_exporter,可以在https://github.com/prometheus/node_exporter/releases当中找到对应的版本进行下载。

常见的安装命令如下:

# macos amd64版本
wget https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.darwin-arm64.tar.gz

# maox amd64版本
wget https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.darwin-amd64.tar.gz

# linux amd64版本
wget https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz

# 解压
tar -xvzf node_exporter-*.tar.gz

使用如下的命令启动node_exporter,默认会在9100端口启动(当然我们也可以自定义端口号启动)。

nohup ./node_exporter > info.log &

# 自定义端口号启动
nohup ./node_exporter --web.listen-address=":9120" > info.log &

接着我们在Prometheus当中配置node_exporter的9100抓取端口,以便Node Exporter当中暴露的指标可以被Prometheus进行抓取到。

# 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:8090"]
    metrics_path: "/actuator/prometheus"

    # node-exporter
  - job_name: "node-exporter"
    static_configs:
      - targets: ["localhost:9100"]
    metrics_path: "/metrics"

接着,就可以在Prometheus/Grafana当中查看到相关的指标,也可以使用curl http://localhost:9100/metrics在命令行查看相关的指标信息。

2.Grafana安装并接入Prometheus数据源

2.1 Grafana下载和安装

Grafana官网下载链接https://grafana.com/get/?tab=self-managed

grafana-download-page-1.png

进入下载页面之后,根据操作系统选择对应的版本,使用安装脚本进行安装。

grafana-download-page-2.png

Grafana安装流程如下:

# macos下载
curl -O https://dl.grafana.com/enterprise/release/grafana-enterprise-11.2.0.darwin-amd64.tar.gz

# linux下载
wget https://dl.grafana.com/enterprise/release/grafana-enterprise-11.2.0.linux-amd64.tar.gz

# 解压
tar -zxvf grafana-enterprise-*.tar.gz

启动Grafana:

cd bin
./grafana-server

Grafana默认在3000端口号启动,通过http://localhost:3000可以打开Grafana首页,默认登录账户admin、登录密码admin,接着修改默认密码之后进入Grafana首页。

grafana-server-start-process.png

2.2 Grafana接入Prometheus数据源

在进入Grafana首页之后,需要可以点击左侧Tab栏,添加数据源。

grafana-home-page-datasource.png

在添加数据源页面,将Promoetheus作为Datasource进行添加。

grafana-add-data-source-prometheus.png

在页面当中填写Prometheus服务器的URL地址,比如http://localhost:10090

grafana-add-data-source-prometheus-option.png

在配置好抓取的Prometheus服务器的接口之后,可以将页面往下拉,找到Save & test测试抓取目标配置是否正常。

grafana-add-data-source-prometheus-option-save-test.png

2.3 Grafana监控面板的搭建

从首页的Tab栏当中找到Dashboards,并新建一个Dashboard。

grafana-add-dashboard.png

选择Add visualization新建Pannel。

grafana-add-dashboard-1.png

选择Prometheus数据源

grafana-add-dashboard-2.png

接着会进入到下面的界面,这个界面是用于进行监控面板的配置页面。

grafana-add-dashboard-3.png

接着,就可以在左下角,选择监控指标名称(会根据Prometheus当中存储的指标进行自动推荐,也可以根据名字进行搜索),选择完成之后并点击Run queries

grafana-add-dashboard-4.png

点击Run queries就可以查看到指标展示到面板当中,在界面的右侧可以对当前监控面板的相关信息进行配置。

grafana-add-dashboard-5.png

2.4 Grafana官网提供的常用监控Dashboard

Grafana当中,通过Json版式的方式配置监控的Dashboard。

我们的业务系统指标,如果想要每个指标都自己绘制以便,往往比较繁琐,需要一个个添加,并应用函数。实际上在Grafana官网当中,也提供了很多开源的比较炫酷的Dashboard,参考地址如下:https://grafana.com/grafana/dashboards/,在官网下载Json版式即可应用到我们自己的系统的面板当中。

下面举几个Grafana官网提供的Dashboard的例子:

下面是引入JVM Micrometer的Json版式之后的样式,https://grafana.com/grafana/dashboards/4701-jvm-micrometer/

grafana-micrometer-jvm-dashboard.png

下面是引入MySQL的Dashboard的Json版式之后的样式,涵盖了MySQL的各种各样的监控指标(网络、锁、线程、QPS),https://grafana.com/grafana/dashboards/7362-mysql-overview/

grafana-mysql-dashboard.png

Comment