Prometheus+Grafana安装并监控Linux主机及微服务

(25) 2024-05-04 23:01:01

一、 简介

普罗米修斯是开源的系统监控/报警工具库,功能非常全,且拥有活跃的开发者和用户社区。Prometheus通过HTTP定期主动拉取(Pull)的方式获得指标(直接获取或通过gateway推送),在本地存储所有抓取的样本,并对这些数据运行规则,从现有数据聚合和记录新的时间序列,或生成警报。

Prometheus原生的可视化界面做得比较原始(主要用于调试),所以社区(官方推荐)使用Grafana来做数据展示。

Grafana专注于数据展示,有着丰富用成熟的展示方式和插件,数据源支持Elasticsearch, Prometheus, Graphite, InfluxDB等等。可以让你通过界面点击(无需写前端代码)快速搭建一个非常专业漂亮的展示界面。即便对于前端零基础的开发者也非常友好!

二、特点

1、具有由度量名称和键/值对标识的时间序列数据的多维数据模型
2、PromQL,一种灵活的查询语言, 可以利用这一维度
3、不依赖分布式存储; 单个服务器节点是自治的
4、时间序列集合通过HTTP上的拉模型发生
5、推送时间序列通过中间网关支持
6、通过服务发现或静态配置发现目标
7、多种图形和仪表板支持模式

三、prometheus的组件

Prometheus生态系统由多个组件组成,其中许多组件是可选的:
1、主要的Prometheus服务器,用于存储时间序列数据
2、用于检测应用程序代码的客户端库
3、用于支持短期工作的推送网关
4、针对HAProxy,StatsD,Graphite等服务的专用出口商 
5、一个alertmanager处理警报
6、各种支持工具
大多数Prometheus组件都是用Go编写的,因此很容易构建和部署为静态二进制文件。

Prometheus+Grafana安装并监控Linux主机及微服务 (https://mushiming.com/)  第1张

 四、Prometheus安装

1、下载prometheus Download | Prometheus (以版本2.36.2为例)
2、解压包到指定目录
tar -zxvf prometheus-2.36.2.linux-amd64.tar.gz -C /home/srv_cnexp_eshipv2dev/prometheus
3、修改配置文件
vim /home/srv_cnexp_eshipv2dev/prometheus/prometheus-2.36.2.linux-amd64/prometheus.yml
Prometheus+Grafana安装并监控Linux主机及微服务 (https://mushiming.com/)  第2张

4、prometheus配置语法校验
建议每次修改prometheus配置之后, 都进行语法校验, 以免导致 prometheus server无法启动 
./promtool check config prometheus.yml
5、启动prometheus
./prometheus --config.file=prometheus.yml
后台启动:
nohup ./prometheus --storage.tsdb.retention.time=7d --config.file=prometheus.yml --web.enable-admin-api > prometheus.log 2>&1 &
*启动参数说明:
--config.file -- 指明prometheus的配置文件路径
--web.enable-lifecycle -- 指明prometheus配置更改后可以进行热加载
--storage.tsdb.path -- 指明监控数据存储路径
--storage.tsdb.retention --指明数据保留时间;
在启动prometheus时加上参数 --web.enable-lifecycle 可启动配置热加载 修改配置后发送请求触发
 curl -X POST http//localhost:9090/-/reload

6、设置prometheus系统服务,并配置开机自启动(根据情况而定可不做
        to​​​​​​​uch /usr/lib/systemd/system/prometheus.service
        chown prometheus:prometheus /usr/lib/systemd/system/prometheus.service
        vim /usr/lib/systemd/system/prometheus.service

[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target

[Service]
Type=simple
User=srv_cnexp_eshipv2dev
# --storage.tsdb.path是可选项,默认数据目录在运行目录的./dada目录中
ExecStart=/home/srv_cnexp_eshipv2dev/prometheus/prometheus-2.36.2.linux-amd64/prometheus --storage.tsdb.path=/home/srv_cnexp_eshipv2dev/prometheus/prometheus-2.36.2.linux-amd64/data --storage.tsdb.retention.time=7d --config.file=/home/srv_cnexp_eshipv2dev/prometheus/prometheus-2.36.2.linux-amd64/prometheus.yml --web.enable-admin-api 
Restart=on-failure

[Install]
WantedBy=multi-user.target
​​​设置为开机自启动 执行一下命令
systemctl daemon-reload 
systemctl enable prometheus.service 
systemctl status prometheus.service 
​​​​​​​systemctl restart prometheus.service​​​​​​​

五、集成监控SpringBoot项目

1.spingboot项目引入依赖

<-- maven -->
<dependency>
 <groupId>io.micrometer</groupId>
 <artifactId>micrometer-registry-prometheus</artifactId>
 <version>1.7.3</vsersion>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<-- gradle-->
implementation "io.micrometer:micrometer-registry-prometheus:1.7.3"
implementation 'org.springframework.boot:spring-boot-starter-actuator'


2.修改配置 开启Actuator 暴露prometheus指标
   management.endpoints.web.exposure.include=*  #或者 prometheus
3. 访问URL查看暴露的监控(如果可以说明暴露成功)
   localhost:8080/actuator/prometheus
4.如果需要监控多个不同的服务需要分类 可增加配置区分

#增加配置   
management.metrics.tags.application:${spring.application.name}
#配置类方式
    @Bean
    MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
        return registry -> registry.config().commonTags("application", "服务名称");
    }

​​​​​​​5.自定义指标收集(在调用接口时候对要监控的指标项通过prometheus类进行计算)

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.PostConstruct;
@RestController
@RequestMapping("/api")
public class OperationController {
    @Autowired
    MeterRegistry registry;
    private Counter counter;
    private Counter failCounter;
    @PostConstruct
    private void init(){
        //初始化指标
        failCounter=  registry.counter("requests_add_fail_total","save","carson");
        counter = registry.counter("requests_add_total","save","carson");
    }
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(@Validated String firstName,@Validated String secondName) throws Exception {
        try{
            String name = firstName+secondName;
            //调用接口时对指标进行统计
            counter.increment();
            return name;
        }catch (Exception e){
            //调用接口时对指标进行统计
            failCounter.increment();
            throw new Exception("异常");
        }
    }
}

六、prometheus监控Linux主机

1.下载node_exporter (以1.3.1为例)
2.解压到指定目录
tar -zxvf node_exporter-1.3.1.linux-amd64.tar.gz -C /home/srv_cnexp_eshipv2dev/prometheus
3. 启动node_exporter
./node_exporter
后台启动 (更改默认端口为9100)
nohup ./node_exporter --web.listen-address=":9100" > /dev/null 2>&1 &
4.配置系统并设置开机自启动(是否需要根据情况而定)
​​​​​​​touch /usr/lib/systemd/system/node_exporter.service
chown prometheus:prometheus /usr/lib/systemd/system/node_exporter.service
chown -R prometheus:prometheus /usr/local/node_exporter*
vim /usr/lib/systemd/system/node_exporter.service

[Unit]
Description=node_exporter
After=network.target
[Service]
Type=simple
User=srv_cnexp_eshipv2dev
ExecStart=/home/srv_cnexp_eshipv2dev/prometheus/node_exporter-1.3.1.linux-amd64/node_exporter --web.listen-address=":9100"
Restart=on-failure
[Install]
WantedBy=multi-user.target
启动node_exporter服务并设置开机自启动
systemctl daemon-reload
systemctl enable node_exporter.service
systemctl start node_exporter.service
systemctl status node_exporter.service
systemctl start node_exporter.service
systemctl stop node_exporter.service
systemctl restart node_exporter.service 

5.验证
访问地址 http://node_exporter_server_ip:9100/metrics 可以查看到指标

七、验证prometheus

访问地址 http://prometheus_server_ip:9090/ 进入到prometheus页面​​​​​​​Prometheus+Grafana安装并监控Linux主机及微服务 (https://mushiming.com/)  第3张

 点击status -> Targets 会展示prometheus配置文件中的 Job 在最大分类的Job下会有多个微服务或者多台linux主机的具体类型信息

八、集成Grafana

 

1.下载grafana Download Grafana | Grafana Labs (以版本9.0.4为例)
2.解压到指定目录
tar -zxvf  grafana-enterprise-9.0.4.linux-amd64.tar.gz​​​​​​​ -C 指定目录
3.启动grafana
./grafana-server
后台启动:nohup ./grafana-server > grafana.log 2>&1 &
4.登录grafana
访问地址 http://grafana_ip:3000/   首次登录初始密码为 admin/admin 可以修改
5.配置数据源
Data sources->Add data source -> Prometheus,输入prometheus数据源的信息,主要是输入name和url 
Name:默认为Prometheus,当仅有一个Prometheus数据源时,默认即可(注意:首字母大写)
Url:默认http://localhost:9090,根据实际情况填写IP:端口。
Access:默认为Server,无需变更,默认即可。

Prometheus+Grafana安装并监控Linux主机及微服务 (https://mushiming.com/)  第4张
 

6.添加 Dashboard导入模板 Dashboards | Grafana Labs (仪表盘)​​​​​​​
点击+号->import->选择Upload json file或者是搜索输入仪表盘中的模板Id搜索模板------>load加载
Prometheus+Grafana安装并监控Linux主机及微服务 (https://mushiming.com/)  第5张

配置完成后可查看系统主机节点监控信息,包括系统运行时间, 内存和CPU的配置, CPU、内存、磁盘、网络流量等信息, 以及磁盘IO、CPU温度等信息。

Prometheus+Grafana安装并监控Linux主机及微服务 (https://mushiming.com/)  第6张

 Prometheus+Grafana安装并监控Linux主机及微服务 (https://mushiming.com/)  第7张

 参考资料:

  • 官网地址:https://prometheus.io/
  • GitHub: https://github.com/prometheus
  • 官方文档中文版: https://github.com/Alrights/prometheus
  • 官方监控agent列表:https://prometheus.io/docs/instrumenting/exporters/

        

THE END

发表回复