prometheus安装部署
一、安装prometheus服务端
1、下载prometheus服务端
https://prometheus.io/download/
2、上传服务器、解压至/usr/local/prometheus
#解压
mkdir -p /usr/local/prometheus
tar -zxvf prometheus-2.54.0.linux-amd64.tar.gz -C /usr/local/prometheus
#重命名
cd /usr/local/prometheus
mv prometheus-2.54.0.linux-amd64 prometheus-2.54.0
3、启动
#直接使用默认配置文件,后台启动
/usr/local/prometheus/prometheus-2.54.0/prometheus --config.file="/usr/local/prometheus/prometheus-2.54.0/prometheus.yml" &
#确认端口(9090)
lsof -i:9090
4、访问
http://ip:9090/
看到这个界面,服务端部署完成。
数据查看
http://ip:9090/metrics
二、监控节点node_exporter部署
1、下载node_exporter
https://prometheus.io/download/
2、上传服务器、解压至/usr/local/prometheus
#解压
tar -xzvf node_exporter-1.8.2.linux-amd64.tar.gz -C /usr/local/prometheus
#重命名
cd /usr/local/prometheus
mv node_exporter-1.8.2.linux-amd64 node_exporter-1.8.2
3、启动
#后台启动
nohup /usr/local/prometheus/node_exporter-1.8.2/node_exporter &
#确认端口(9100)
lsof -i:9100
4、访问验证
http://10.23.190.34:9100/metrics
5、添加客户端到服务端
此时启动的客户端节点跟服务端没有关联,不支持自动注册,需要在服务端修改配置,添加客户端节点
添加之前,服务端看不到要监控的别的节点:
添加如下代码:
cd /usr/local/prometheus/prometheus-2.54.0
vim prometheus.yml
填入内容:
- job_name: “agent-34” # 取一个job名称来代表被监控的机器
static_configs:- targets: [“10.23.190.34:9100”] # 这里改成被监控机器的IP,后面端口接9100
重启Prometheus服务,回到web管理界面 --》点Status --》点Targets --》可以看到多了一台监控目标:
三、常用指标
1、内存使用率
100 - (node_memory_MemFree_bytes+node_memory_Cached_bytes+node_memory_Buffers_bytes) / node_memory_MemTotal_bytes * 100
2、内存剩余
100 - (node_memory_MemFree_bytes+node_memory_Cached_bytes+node_memory_Buffers_bytes) / node_memory_MemTotal_bytes * 100
3、磁盘使用率
100-node_filesystem_free_bytes{fstype=~"ext4|xfs",mountpoint="/"} / node_filesystem_size_bytes{fstype=~"ext4|xfs",mountpoint="/"} * 100
4、5分钟平均负载
avg(irate(node_cpu_seconds_total{mode='idle'}[5m])) by (instance)
四、设置系统服务,简化启动命令
1、Prometheus系统服务
cd /usr/local/prometheus/prometheus-2.54.0
#创建prometheus.service系统文件
vim prometheus.service
文件内容
[Unit]
Description=prometheus Server
After=network.target
[Service]
Type=simple
User=prometheus
Group=prometheus
ExecStart=/usr/local/prometheus/prometheus-2.54.0/prometheus --config.file=/usr/local/prometheus/prometheus-2.54.0/prometheus.yml --storage.tsdb.path=/usr/local/prometheus/prometheus-2.54.0/data --storage.tsdb.retention.time=60d --web.enable-lifecycle
Restart=on-failure
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
通过软链挂载到系统服务路径下
ln -s /usr/local/prometheus/prometheus-2.54.0/prometheus.service /etc/systemd/system/prometheus.service
创建启动用户和赋权
useradd prometheus -M -s /sbin/nologin
chown -R prometheus.prometheus /usr/local/prometheus/prometheus-2.54.0
系统服务的启动停止命令
systemctl daemon-reload
systemctl start prometheus
systemctl stop prometheus
systemctl status prometheus
2、node_exporter系统服务
cd /usr/local/prometheus/node_exporter-1.8.2
#创建系统服务
vim node_exporter.service
文件内容如下:
[Unit]
Description=node_exporter
After=network.target
[Service]
Type=simple
User=prometheus
Group=prometheus
ExecStart=/usr/local/prometheus/node_exporter-1.8.2/node_exporter
Restart=on-failure
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
通过软链挂载到系统服务下
ln -s /usr/local/prometheus/node_exporter-1.8.2/node_exporter.service /etc/systemd/system/node_exporter.service
创建启动用户和赋权
useradd prometheus -M -s /sbin/nologin
chown -R prometheus.prometheus /usr/local/prometheus/node_exporter-1.8.2
启动命令:
systemctl daemon-reload
systemctl start node_exporter
systemctl stop node_exporter
systemctl status node_exporter
五、springboot集成Prometheus
添加依赖(pom.xml):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
创建服务类(PrometheusService.java):
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class PrometheusService {
@Autowired
private RestTemplate restTemplate;
private static final String PROMETHEUS_URL = "http://localhost:9090/api/v1/query?query=";
public String getMetric(String metricName) {
String url = PROMETHEUS_URL + metricName;
return restTemplate.getForObject(url, String.class);
}
// 示例方法,具体metric查询根据实际PromQL编写
public String getCpuUsage() {
// 假设的PromQL查询
String query = "node_cpu_seconds_total";
return getMetric(query);
}
// 类似地,添加内存和硬盘使用率的查询方法
}
创建控制器(MetricsController.java):
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MetricsController {
@Autowired
private PrometheusService prometheusService;
@GetMapping("/metrics/cpu")
public String getCpuUsage() {
return prometheusService.getCpuUsage();
}
// 添加内存和硬盘使用率的接口
}