Prometheus + Grafana + Node_exporter部署使用
部署环境说明
IP | 组件 |
---|---|
http://192.168.146.18/ | Prometheus & grafana & node_exporter |
http://192.168.146.19/ | node_exporter |
http://192.168.146.17/ | node_exporter |
部署Prometheus
1、下载
https://prometheus.io/download/
2、安装
上传prometheus-2.33.1.linux-amd64.tar.gz 到 /home/software
tar -xvf prometheus-2.33.1.linux-amd64.tar.gz
mv xxx prometheus 修改文件名
查看是否安装成功
./prometheus --version
添加组
groupadd prometheus
创建用户
useradd -g prometheus prometheus
更改用户权限
chown -R prometheus:prometheus /home/software/prometheus/
添加服务
vim /etc/systemd/system/prometheus.service(添加服务)
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
# Type设置为notify时,服务会不断重启
Type=simple
User=prometheus
# --storage.tsdb.path是可选项,默认数据目录在运行目录的./dada目录中
ExecStart=/home/software/prometheus/prometheus --config.file=/home/software/prometheus/prometheus.yml --storage.tsdb.path=/home/software/prometheus
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl enable prometheus.service (开机自启)
部署Node_exporter
1、下载node_exporter
在prometheus官网
tar -vzxf node_exporter-1.3.1.linux-amd64
mv node_exporter-1.3.1.linux-amd64 node_exporter
2、安装node_exporter
(如果node_exporter与Prometheus不在同一台机器,请创建Prometheus用户,否则无法启动)
添加组
groupadd prometheus
创建用户
useradd -g prometheus prometheus
更改用户权限
chown -R prometheus:prometheus /home/software/node_exporter/
注意:更改用户权限时主要查看当前文件夹的父文件夹权限是否是其他非root用户,是的话,需要将文件夹移至父文件夹为root用户的文件夹下
添加服务
vim /etc/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target
[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/home/software/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable node_exporter
3、Prometheus配置
vim /home/software/prometheus/prometheus.yml
# 全局配置
global:
scrape_interval: 15s # 设置抓取(pull)时间间隔,默认是1m
evaluation_interval: 15s # 设置rules评估时间间隔,默认是1m
# scrape_timeout is set to the global default (10s).
# 告警管理配置,暂未使用,默认配置
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# 加载rules,并根据设置的时间间隔定期评估,暂未使用,默认配置
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# 抓取(pull),即监控目标配置
# 默认只有主机本身的监控配置
scrape_configs:
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
#新增
- job_name: 'linux'
static_configs:
- targets: ['10.200.114.162:9100'] #被监控端服务器ip
重启
systemctl restart prometheus.service
Grafana部署以及使用
1、下载和安装
#进入下载目录下载 cd /home/software
wget https://dl.grafana.com/oss/release/grafana-6.6.0-1.x86_64.rpm
#yum安装
yum localinstall grafana-6.6.0-1.x86_64.rpm
#启动 & 开机自启
systemctl start grafana-server
systemctl enable grafana-server
访问http://192.168.146.18:3000/
2、配置数据源和dashboard
配置数据源
点击后点击add data source,选择prometheus
选择好url后直接save & test
配置dashboard
输入pid后load后选择数据源为prometheus直接import
推荐服务器面板id为8919,微服务面板id10280
微服务监控
1、获取注册中心所有服务
依赖
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-prometheus</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
使用:https://gitee.com/596392912/mica/tree/master/mica-prometheus
发现服务存放list
import com.talkweb.twiot.device.pojo.grafana.TargetGroup;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;
/**
* @program: tw-iot
* @description: prometheus
* @author: LiuZhuzheng
* @create: 2022-02-11 16:47
**/
@RestController
@RequestMapping