prometheus安装和使用过程

2023-12-07 0 420
目录
  • 一、Prometheus介绍
  • 二、Prometheus特点
    • 2.1、prometheus特点
    • 2.2、什么是样本
  • 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:

    prometheus安装和使用过程

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

    prometheus安装和使用过程

    prometheus安装和使用过程

    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

    prometheus安装和使用过程

    添加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

    prometheus安装和使用过程

    dashboard:

    prometheus安装和使用过程

    注意点:

    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安装内容请搜索悠久资源网以前的文章或继续浏览下面的相关文章希望大家以后多多支持悠久资源网!

    您可能感兴趣的文章:

    • Prometheus插件mysql_exporter安装过程
    • docker安装prometheus和grafana的详细过程
    • Prometheus和NodeExporter安装监控数据说明
    • prometheus监控节点程序的安装及卸载命令
    • Docker 部署 Prometheus的安装详细教程
    • Prometheus的安装和配置教程详解

    收藏 (0) 打赏

    感谢您的支持,我会继续努力的!

    打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
    点赞 (0)

    悠久资源 编程综合 prometheus安装和使用过程 https://www.u-9.cn/biancheng/bczonghe/118297.html

    常见问题

    相关文章

    发表评论
    暂无评论
    官方客服团队

    为您解决烦忧 - 24小时在线 专业服务