prometheus安装和使用过程

 更新时间:2023年06月20日 09:01:35   作者:辉子t1  
Prometheus是一个开源的系统监控和报警系统,通常会搭配prometheus进行监控,同时也支持多种exporter采集数据,还支持pushgateway进行数据上报,Prometheus性能足够支撑上万台规模的集群,这篇文章主要介绍了prometheus安装和使用记录,需要的朋友可以参考下

一、Prometheus介绍

Prometheus是一个开源的系统监控和报警系统,现在已经加入到CNCF基金会,成为继k8s之后第二个在CNCF托管的项目,在kubernetes容器管理系统中,通常会搭配prometheus进行监控,同时也支持多种exporter采集数据,还支持pushgateway进行数据上报,Prometheus性能足够支撑上万台规模的集群。

二、Prometheus特点

2.1、prometheus特点

1)多维度数据模型

每一个时间序列数据都由metric度量指标名称和它的标签labels键值对集合唯一确定:这个metric度量指标名称指定监控目标系统的测量特征(如:http_requests_total- 接收http请求的总计数)。labels开启了Prometheus的多维数据模型:对于相同的度量名称,通过不同标签列表的结合, 会形成特定的度量维度实例。(例如:所有包含度量名称为/api/tracks的http请求,打上method=POST的标签,则形成了具体的http请求)。这个查询语言在这些度量和标签列表的基础上进行过滤和聚合。改变任何度量上的任何标签值,则会形成新的时间序列图。

2)灵活的查询语言(PromQL):可以对采集的metrics指标进行加法,乘法,连接等操作;

3)可以直接在本地部署,不依赖其他分布式存储;

4)通过基于HTTP的pull方式采集时序数据;

5)可以通过中间网关pushgateway的方式把时间序列数据推送到prometheus server端;

6)可通过服务发现或者静态配置来发现目标服务对象(targets)。

7)有多种可视化图像界面,如Grafana等。

8)高效的存储,每个采样数据占3.5 bytes左右,300万的时间序列,30s间隔,保留60天,消耗磁盘大概200G

9)做高可用,可以对数据做异地备份,联邦集群,部署多套prometheus,pushgateway上报数据

2.2、什么是样本

样本:在时间序列中的每一个点称为一个样本(sample),样本由以下三部分组成:

  • 指标(metric):指标名称和描述当前样本特征的 labelsets;
  • 时间戳(timestamp):一个精确到毫秒的时间戳;
  • 样本值(value): 一个 folat64 的浮点型数据表示当前样本的值。

表示方式:通过如下表达方式表示指定指标名称和指定标签集合的时间序列:<metric name>{<label name>=<label value>, ...}

例如,指标名称为 api_http_requests_total,标签为 method="POST" 和 handler="/messages" 的时间序列可以表示为:api_http_requests_total{method="POST", handler="/messages"}

prometheus安装和使用记录

Getting started | Prometheus

Configuration | Prometheus

Download | Prometheus

Download Grafana | Grafana Labs

# prometheus
mkdir -m=777 -p /data/{download,app_logs,app/prometheus}
cd /data/download
wget https://github.com/prometheus/prometheus/releases/download/v2.45.0-rc.0/prometheus-2.45.0-rc.0.linux-amd64.tar.gz
tar xvfz prometheus-*.tar.gz
ln -s /data/download/prometheus-2.45.0-rc.0.linux-amd64/prometheus /usr/bin/prometheus
cp /data/download/prometheus-2.45.0-rc.0.linux-amd64/prometheus.yml /data/app/prometheus/prometheus.yml 
prometheus --config.file=/data/app/prometheus/prometheus.yml --web.listen-address=:9090 --web.enable-lifecycle --storage.tsdb.path=/data/app/prometheus/data >>/data/app_logs/prometheus.log 2>&1 &
# node_exporter 在需要监控的服务器里安装
mkdir -m=777 -p /data/{download,app_logs,app/prometheus}
cd /data/download
wget https://github.com/prometheus/node_exporter/releases/download/v1.6.0/node_exporter-1.6.0.linux-amd64.tar.gz
tar xvfz node_exporter*
ln -s /data/download/node_exporter-1.6.0.linux-amd64/node_exporter /usr/bin/node_exporter
# 启动node_exporter,服务器暴露的端口是8080,同时服务器里有其他服务占用了8080端口,可以使用nginx将node_exporter获取指标的api暴露出去
# location /metrics {
#     proxy_pass http://127.0.0.1:9000/metrics;
# }
node_exporter --web.listen-address 127.0.0.1:9000 >>/data/app_logs/node_exporter.log 2>&1 &
# 添加node_exporter之后,需要更新prometheus.xml添加targets,然后运行:curl -X PUT http://server_address:port/-/reload重新加载配置文件
# alert_manager可以和prometheus安装到同一台服务器
cd /data/download
wget https://github.com/prometheus/alertmanager/releases/download/v0.25.0/alertmanager-0.25.0.linux-amd64.tar.gz
tar xvfz alertmanager*
ln -s /data/download/alertmanager-0.25.0.linux-amd64/alertmanager /usr/bin/alertmanager
cp /data/download/alertmanager-0.25.0.linux-amd64/alertmanager.yml /data/app/prometheus/alertmanager.yml
alertmanager --config.file=/data/app/prometheus/alertmanager.yml --web.listen-address 127.0.0.1:9001 >>/data/app_logs/node_exporter.log 2>&1 &
# 将alert_manager的地址添加到prometheus.yml里的alertmanagers的targets里,然后运行:curl -X PUT http://server_address:port/-/reload重新加载配置文件

测试报警邮件功能:设置如果安装exporter的服务器内存占用率超过50%或者tcp timewait超过10的时候就发邮件(在实际工作中需要设置一个合适的条件):

prometheus.yml里添加rule_files的路径:

# 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:
          - 127.0.0.1:9001
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"
  - "/data/app/prometheus/alert.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'.
    scrape_interval: 5s
    static_configs:
            - targets: ["node1_ip:8080"]
            - targets: ["node2_ip:8080"]
              labels:
                groups: 'container'

alert.rules.yml里添加具体的rule,node_socket_TCP_tw这些具体的指标通过http://node_exporter_ip:port/metrics可以获取到

groups:
- name: tcp-alert-group
  rules:
  - alert: TcpTimeWait
    expr: node_sockstat_TCP_tw > 10
    for: 10m
    labels:
      severity: warning
    annotations:
      summary: tcp time wait more than 10
      description: please check node_sockstat_TCP_tw metric
  - alert: MemoryUse
    expr: (node_memory_MemTotal_bytes-node_memory_MemFree_bytes-node_memory_Buffers_bytes-node_memory_Cached_bytes)/node_memory_MemTotal_bytes > 0.5
    for: 10m
    labels:
      severity: warning
    annotations:
      summary: memory use more than 50% for 10 min
      description: please check memory use

alertmanager.yml里配置告警邮件的信息:

global:
  resolve_timeout: 5m
  smtp_smarthost: your_smpt_host:port
  smtp_from: alertmanager@your_email_domain
  smtp_require_tls: false
route:
  group_by: ['alertname']
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 10m
  receiver: 'email'
receivers:
  - name: 'email'
    email_configs:
    - to: 'receiver_email'
      send_resolved: true

yml文件一旦更新,需要重新加载配置:curl -X PUT http://server_address:port/-/reload

在Prometheus的界面可以看到添加的alert:

 当alert的条件满足后,alertmanager就会发邮件

grafana的安装和启动:

# grafana可以和prometheus里安装到同一台服务器
yum install -y https://dl.grafana.com/enterprise/release/grafana-enterprise-10.0.0-1.x86_64.rpm
# grafana默认启动的端口号是3000,如果服务器没有暴露3000端口的话,需要修改grafana的配置文件
sed -i 's/3000/8080/g' /usr/share/grafana/conf/defaults.ini
grafana server >> /data/app_logs/grafana.log 2>&1 &
# grafana数据保存地址:/var/lib/grafana.db

grafana启动之后就可以在浏览器上打开对应的地址,初次登录用户名和密码:admin/admin

Data sources里添加prometheus,grafana和prometheus启动在同一台服务器里的话,地址就可以用localhost

 添加dashboard,在Explore里可以查询指标并且添加到dashboard

cpu使用率:avg(1-irate(node_cpu_seconds_total{mode="idle"}[1m])) by(instance)

内存使用率:(node_memory_MemTotal_bytes-node_memory_MemFree_bytes-node_memory_Buffers_bytes-node_memory_Cached_bytes)/node_memory_MemTotal_bytes

tcp连接数:node_sockstat_TCP_alloc

 dashboard:

注意点:

1.prometheus启动的时候添加--web.enable-lifecycle才允许通过调用/-/reload接口重新加载配置文件
2.prometheus启动的时候指定一个固定的数据存放位置--storage.tsdb.path=/data/app/prometheus/data,如果数据存放位置不一致,启动后查不到历史数据,历史数据做备份的话,prometheus启动的服务器还可以变更
3.grafana的数据保存地址:/var/lib/grafana.db,定期做备份,服务器发生系统错误无法使用的时候,在新的服务器里同步/var/lib/grafana.db文件之后,启动grafana之前的配置不会丢失

到此这篇关于prometheus安装和使用过程的文章就介绍到这了,更多相关prometheus安装内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • CommonLisp中解析命令行参数示例

    CommonLisp中解析命令行参数示例

    这篇文章主要为大家介绍了CommonLisp中解析命令行参数示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • 新手程序员编程必不可少的工具

    新手程序员编程必不可少的工具

    这篇文章主要为大家详细介绍了新手程序员编程必不可少的工具,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • aop的实现原理_动力节点Java学院整理

    aop的实现原理_动力节点Java学院整理

    这篇文章主要介绍了aop的实现原理,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • jenkins通过sshPut传输文件时报错Permission denied的SftpException的问题及解决方案

    jenkins通过sshPut传输文件时报错Permission denied的SftpException的问题及解决

    使用jenkins的ssh插件传输文件至远程机器的指定目录,php程序打包后,经过zip压缩为oms.zip,这篇文章主要介绍了jenkins通过sshPut传输文件的时候,报错Permission denied的SftpException,需要的朋友可以参考下
    2023-08-08
  • 字符编码笔记 ASCII,Unicode和UTF-8

    字符编码笔记 ASCII,Unicode和UTF-8

    下面就是我的笔记,主要用来整理自己的思路。但是,我尽量试图写得通俗易懂,希望能对其他朋友有用。毕竟,字符编码是计算机技术的基石,想要熟练使用计算机,就必须懂得一点字符编码的知识
    2012-08-08
  • 人工智能学习路线分享

    人工智能学习路线分享

    这篇文章主要为大家分享了人工智能"六步走"学习路线,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • 混淆矩阵Confusion Matrix概念分析翻译

    混淆矩阵Confusion Matrix概念分析翻译

    这篇文章主要为大家介绍了混淆矩阵Confusion Matrix分析的翻译,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • VS2019提示scanf不安全问题的解决

    VS2019提示scanf不安全问题的解决

    这篇文章主要介绍了VS2019提示scanf不安全问题的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • 微信支付、支付宝支付等常用第三方支付通道接口手续费对比

    微信支付、支付宝支付等常用第三方支付通道接口手续费对比

    微信支付、支付宝等第三方支付,需要和银联、网联对接,有清算机构和银行的交易处理通道成本。费率指支付手续费的费率,不同行业、不同的支付平台、不同的支付额度或次数所对应的通道费率是不一样的。
    2023-01-01
  • 关于Centos7中Vscode无响应的问题及解决方法

    关于Centos7中Vscode无响应的问题及解决方法

    在CentOS7中使用命令行直接安装Vscode时,打开Vscode出现界面卡死、无响应情况,如何处理这个问题呢,今天小编给大家带来了Centos7中Vscode无响应的问题及解决方法,感兴趣的朋友一起看看吧
    2021-07-07

最新评论