Prometheus+Grafana监控集群

mac2025-09-11  6

Prometheus+Grafana监控集群

1.安装Grafana 1.1下载并安装 下载地址:https://grafana.com/grafana/download

选择最新版本进行安装,安装网站的提示运行脚本即可。 离线快速安装部署:

wget https://dl.grafana.com/oss/release/grafana-6.3.3-1.x86_64.rpm sudo yum localinstall grafana-6.3.3-1.x86_64.rpm

1.2 启动grafana 安装完成后,grafana服务默认已安装,配置文件/etc/grafana/grafana.ini,如需要修改路径及端口,可在该文件中修改。

需要修改;domain = localhost改为本机IP。 启动grafana

1.3 登录grafana 访问页面http://IP:3000,默认账户密码是admin/admin,首次登录建议修改密码。

2.安装Prometheus 2.1 Prometheus主程序安装 Prometheus主程序,主要是负责存储、抓取、聚合、和查询方面 官网下载地址: https://prometheus.io/download/

** 下载*/

wget https://github.com/prometheus/prometheus/releases/download/v2.12.0/prometheus-2.12.0.linux-amd64.tar.gz

/** 解压*/

tar -zxvf prometheus-2.12.0.linux-amd64.tar.gz

2.2 启动prometheus主程序 /** 生产环境启动*/

nohup ./prometheus --config.file=prometheus.yml --web.enable-lifecycle --storage.tsdb.retention.time=60d &

/** –web.enable-lifecycle 加上此参数可以远程热加载配置文件,无需重启prometheus,调用指令是curl -X POST http://ip:9090/-/reload – storage.tsdb.retention.time 数据默认保存时间为15天,启动时加上此参数可以控制数据保存时间 */ 其他的参数及配置可以在prometheus.yml中调整及配置。

3.在需要监控的机器上部署exporter 3.1 监控linux主机 下载监控linux主机的node_exporter,从官网下载

/** 下载 */

wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz

/** 解压 */

tar -zxvf node_exporter-0.18.1.linux-amd64.tar.gz

可以按照默认方式启动 /** 启动 node_exporter*/

cd node_exporter-0.18.1.linux-amd64 nohup ./node_exporter &

/** 默认端口9100 */

4.配置各监控仪表盘 4.1 下载各监控仪表盘 可以从grafana官网下载对应的模板,对应地址为:https://grafana.com/grafana/dashboards 找到对应的仪表盘模板后进入下载

4.2 配置数据源 使用均为prometheus数据源,因此配置一个prometheus数据源 如果之前没有在grafana上配置过数据源 登录后会提示创建

选择prometheus

配置prometheus地址

最终save&Test即可 4.3 导入仪表盘 将4.1中下载的模板导入

导入

修改名称及数据源

Import即可 5、在需要监控的机器上部署exporter 5.1 监控linux主机下载监控linux主机的node_exporter,从官网下载

/** 下载 */

wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz

/** 解压 */

tar -zxvf node_exporter-0.18.1.linux-amd64.tar.gz

按照默认方式启动 /** 启动 node_exporter*/

cd node_exporter-0.18.1.linux-amd64 nohup ./node_exporter &

/** 默认端口9100 */ 5.2 监控mysql 5.2.1 下载 下载监控mysql的mysql_exporter,从官网下载

/** 下载 */

wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz

/** 解压 */

tar -zxvf mysqld_exporter-0.12.1.linux-amd64.tar.gz

5.2.2 监控账号及修改文件配置 在mysql里配置mysql监控账号 /** 创建账号 */

mysql> CREATE USER 'mysql_monitor'@'localhost' identified by 'mysql_monitor';

/** 授权 */

mysql> GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'mysql_monitor'@'localhost'; mysql> GRANT SELECT ON performance_schema.* TO 'mysql_monitor'@'localhost';

/** 注意,不用版本对权限要求不一致,启动时注意查看日志,如权限不足则继续授权或创建对应的账号 */ 配置文件修改

cd mysqld_exporter-0.12.0.linux-amd64

vim .my.cnf /** 添加如下配置 */

[client] port=3306 user=mysql_monitor password=mysql_monitor

5.2.3 启动监控脚本

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

5.3 监控redis 5.3.1 下载redis_exporter 官网没有redis_exporter,可以从github上获取,另外redis插件无需放在dis机器上也可以。 /** 下载 */

wget https://github.com/oliver006/redis_exporter/releases/download/v0.30.0/redis_exporter-v0.30.0.linux-amd64.tar.gz

/** 解压 */

tar -zxvf redis_exporter-v0.30.0.linux-amd64.tar.gz

5.3.2 启动redis_exporter /** redis无密码 */

nohup ./redis_exporter -redis.addr=192.168.56.118:6379 -web.listen-address 0.0.0.0:9121 &

/** redis有密码 */

nohup ./redis_exporter -redis.addr=192.168.56.118:6479 -redis.password 123456 -web.listen-address 0.0.0.0:9122 &

/** -web.listen-address 可以自定义监控端口 */ 6.配置prometheus配置文件 6.1 添加各监控项 配置文件可以有多种配置方式,可以根据不同的分类和习惯配置。可参考如下 方式配置

# 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'] - job_name: 'OS' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['IP:9100'] labels: instance: 'IP' - targets: ['IP:9100'] labels: instance: 'IP6' - targets: ['IP:9100'] labels: instance: 'IP' ## 上述job单独做主机监控,每台主机的instance不同 - job_name: 'mysql' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['IP:9104'] labels: instance: 'IP' - targets: ['IP:9104'] labels: instance: 'IP' ## 以上是监控mysql的,instance和主机的instance的相同 - job_name: 'redis' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['IP:9121','IP:9122'] labels: instance: 'IP' - targets: ['IP:9100'] labels: instance: 'IP' # 可以类似上述这种,redis的主机及各redis监控项组合在一起,instance使用相同的

6.2 启动或热加载prometheus /** 启动 */

nohup ./prometheus --config.file=prometheus.yml --web.enable-lifecycle --storage.tsdb.retention.time=60d &

/** – storage.tsdb.retention.time 数据默认保存时间为15天,启动时加上此参数可以控制数据保存时间 */

/** 热加载 */

curl -X POST http://ip:9090/-/reload

/** 热加载的前提是启动时加了–web.enable-lifecycle */ 问题1: 修改添加节点后vi prometheus.yml,热加载集群 curl -X POST http://IP:9090/-/reload即可启动,热加载前提是启动时加了–web.enable-lifecycle 问题2: 9100端口被占用 启动/node_exporter时,加上修改端口号

nohup ./node_exporter --web.listen- address=":9101" &即可
最新回复(0)