部署Prometheus

一、部署

[root@server ~] at > /etc/yum.repos.d/prometheus.repo<< EOF
[prometheus] name=prometheus
baseurl=https://packagecloud.io/prometheus-rpm/release/el/$releasever/$basearchrepo_gpgcheck=1
enabled=1
gpgkey=https: //packagecloud.io/prometheus-rpm/release/gpgkey.
https://raw.githubusercontent.com/lest/prometheus-rpm/master/RPM-GPG-KEY-prometheus-rpm
gpgcheck=1 metadata_expire=300
EOF

1、下载、解压PRO

[root@server ~] tar zxvf prometheus-2.27.1.linux-amd64.tar.gz -C /usr/local/

#默认配置文件
cat prometheus. yml
# 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 对接alertmanger
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093
告警系统配置(如何加载/对接告警/记录)
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_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'.

    static_configs:
    - targets: ['localhost:9090']

2、设置时间同步

[root@server ~] ntpdate ntp1.aliyun.com

3、直接开启pro

[root@server ~] ./prometheus

另开一个端口查看

[root@server ~] # netstat -natp | grep 9090
tcp6       0      0 :::9090                 :::*                    LISTEN      3335/./prometheus   
tcp6       0      0 ::1:9090                ::1:33064               ESTABLISHED 3335/./prometheus   
tcp6       0      0 ::1:33064               ::1:9090                ESTABLISHED 3335/./prometheus  

4、访问方式

#访问1: ip : 9090查看prometheus(表达式浏览器)I

#访问2: ip : 9090 / metrics查看采集数据
prometheus 会进行周期性的采集数据(完整的),多次周期性(在一个时间区间内)采集的数据集合,形成了时间序列

#搜索prometheus http请求统计,
查询:prometheus_http_requests_total
可重复查询,观测到有增长

附加条件(指标过滤器:PromQL) :
prometheus_http_requests_total{ code="302"}

二、监控node节点

prometheus想要监控节点,则需要借助于node_exporter,下载地址https:/ /prometheus.io/download/

1、解压node exporter

tar zxvf node_exporter-1.1.2.linux-amd64.tar.gz

基于go语言开发,无需其他配置

cd node_exporter-1.1.2.linux-amd64/

2、复制命令,让系统可以识别

cp node_exporter /usr/local/bin/

3、开启

./node_exporter --help

#方式一,跑在后台(自定义开启模块)
exporter 的UNIT文件展示
[ Unit]
Description=node_exporter
Documentation=https : /prometheus.io/After=network.targets
[ serveice]
Type=simple
User=prometheus
Execstart=/usr/ local/bin/ node_exporter \
--collector.ntp \
--collector.mountstats \
--collector.systemd l--collector.tcpstat
ExecReload-/bin/ kill-HUP $MAINPIDTimeoutStopSec-20s
Restart=always
[ Install]
wantedBy=multi-user.targetI

#方式二:直接启动
./ node_exporter

4、直接访问ip: 9100/ metrics查看数据抓取的内容

三、修改prometheus 配置文件,关联/获取到node_exporter抓取的指标

1、静态发现

添加以下内容(静态PEI)
  - job_name: 'nodes'
    static_configs:
    - targets:
      - 192.168.52.30:9100
      - 192.168.52.40:9100
pro默认指定的是9100,所以我们默认指定9100即可,同时,在进行展示的时候,
我们默认会在URL中加入/metrics 路径〈做为指标采集的端点),若不是这个,则需药使用metrics path进行
指定同时在时间序列浏览器上会显示出为"up"状态的时序状态"1"为正常值

2、动态发现

①、基于文件服务发现
基于文件的服务发现是仅仅略优于静态配置的服务发现方式,它不依赖于任何平台或第三方服务,因而也是最为简单和通用的实现方式;
Prometheus Server定期从文件中加载Target信息
文件可使用JSON和YAML格式,它含有定义的Target列表,以及可选的标签信息;
以下第一个配置,能够将prometheus默认的静态配置转换为基于文件的服务发现时所需的配置:
(prom会周期性的读取、重载此文件中的配置,从而达到动态发现、更新的操作)

prometheus_server.yaml 文件
- targets:
  - 192.168.52.20:9090
  labels:
    app: prometheus
    job: prometheus

 nodes_centos.yaml 文件 
- targets:
  - 192.168.52.30:9100
  - 192.168.52.40:9100
  lables:
    app: node-exporter
    job: node

以上文件可由另一个系统生成,例如Puppet、Ansible或saltstack等配置管理系统,也可能是由脚本基于CMDB定期查询生成
vim file_sed/prometheus.yml
    file_sd_configs:
    - files :
      - targets/prometheus_*.yaml
      refresh_interval: 2m

  # All nodes
  - job_name: 'nodes'
    file_sd_configs :
    - files:
      - targets/nodes_*.yaml
      refresh interval: 2m

#进入targets ,创建prometheus_server.yaml和nodes_centos.yaml
cat prometheus_server.yaml
cat nodes_centos.yaml

#指定配置文件启动
./prometheus --config.file=./file_sd/prometheus.yml
 进入192.168.52.128: 9090 , targets,查看服务状态
 修改nodes_centos.yaml删除192.168.52.128:9100
 返回控制台,查看targets状态

3、基于DNS服务发现

基于DNS的服务发现针对一组DNS域名进行定期查询,以发现待监控的目标
查询时使用的DNS服务器由/etc/resolv.conf文件指定;
该发现机制依赖于A、AAAA和SRv资源记录,且仅支持该类方法,尚不支持REC6763中的高级DNs发现方式PS:
##SRV:SRV记录的作用是指明某域名下提供的服务。
实例:
_http._tcp.example.com. SRV 10 5 80. www.example.com

SRV后面项目的含义:
10 -优先级,类似MX记录
5-权重
80 -端口
www.example.com -实际提供服务的主机名。
同时sRV可以指定在端口上对应哪个service
#prometheus 基于Du的服务中的sR记录,iprometheus发现指定target上对应的端口对应的是exporter或instrumentation

示例:
names:
[ - <string> ] 										#定义SRV资源记录的名称,比如"prometheus._tcp.kgc.com"
[ type: <string> | defualt - 'SRV']					#基于哪种方式的DNS服务发现
[ port: <int> ]										#指定的端口是哪个
[ refresh_interval: <duration> | default = 30s ]	#多长时间刷新一次

4、基于consul服务发现

①、Consul简介

一款基于golang开发的开源工具,主要面向分布式,服务化的系统提供服务注册、服务发现和配置管理的功能
提供服务注册/发现、健康检查、Key/Value存储、多数据中心和分布式一致性保证等功能

#原理:通过定义json文件将可以进行数据采集的服务注册到consul中,用于自动发现
同时使用prometheus做为client端获取consul上注册的服务,从而进行获取数据

#启动开发者模式
#consul 开发者模式,可以快速开启单节点的 consul服务,具有完整功能,方便开发测试
mkdir -pv /consul/data
mkdir /etc/consul
consul agent -dev -ui -data-dir=/consul/data/ \
-config-dir=/etc/consul/ -client=0.0.0.0

agent -dev : 运行开发模式
agent -server:运行server模式-ui : ui界面
-data-dir :数据位置
-config-dir :配置文件位置
/etc/consul :可以以文件形式定义好的各个services的配置,也可以基于api接口直接配置-client:监听地址
运用的端口:8500

另启一个终端,进入/ect/consul目录定义prometheus-servers.json 
[root@server /etc/consul] # cat prometheus-servers.json 
{
  "services": [
    {
      "id": "prometheus-server-node01",
      "name": "prom-server-node01",
      "address": "192.168.221.20",
      "port": 9090,
      "tags": ["prometheus"],
      "checks": [{
        "http": "http://192.168.221.20:9090/metrics",
        "interval": "5s"
      }]
    }
  ]
}

#重载consul 配置文件
consul services register /etc/consul/prometheus-servers.json

#也可以使用consul reload 进行重载

#启动prometheus
cd /usr/local/prometheus-2.27.1.linux-amd64
mkdir consul-sd && cd consul-sd
cat prometheus.yml
   consul_sd_configs:
    - server: "192.168.221.20:8500"
      tags:
      - "prometheus"
      refresh_interval: 2m

  # All nodes
  - job_name: 'nodes'
    consul_sd_configs:
    - server: "192.168.221.20:8500"
      tags:
      - "nodes"
      refresh_interval: 2m

 ./prometheus --config.file=./consul_sd/prometheus.yml

##此时查看prometheus
targets指标,只能看到一个prometheus,因为配置文件中配置了"prometheus"的tag,所以只会抓取po的配置
##/etc/cosul目录下定义nodes.json

配置方式:
#在pro工作目录下:
mkdir files_sd& & cd files_sdmkdir targets
cat prometheus. yml
#my global config
#Author: MageEdu <mage@magedu.com>
# Repo: http://gitlab.magedu. com/MageEdu/prometheus-configs/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:
      # - alertmanager: 9093


停止前端启动的prometheus,重新启动
#表达式浏览器操作
查看targets值
nodes (JOB)中1个up 两个down
另外两个节点添加node_exporter即可

查询node指标

过滤标签
#CPU使用总量(所有节点)
node_cpu_seconds_total
#CPU空闲值(所有节点)
node_cpu_seconds_total { mode='idle'}
#指定查看节点
node_cpu_seconds_total{mode='idle', instance="192.168.221.30:9100"}

#进阶1:
计算过去5分钟内的CPU使用速率
PromQL: irate(node_cpu_seconds_total { mode="idle" } [ 5m] )
解析:
irate:速率计算函数(灵敏度非常高的>
node_cpu_seconds total: 
node节点cPu使用总量mode="idle"空闲指标
5m:过去的5分钟内,所有cPU空闲数的样本值,每个数值做速率运算
#进阶2:
每台主机cPU在5分组内的平均使用率
PromQL: (1- avg (irate(node_cpu_seconds_total[mode='idle' ) [5m]))by (instance)* 100
解析
avg: 平均值
avg (irate (node_cpu_seconds_total fmode='idle' } [5m]):可以理解为CPU空闲量的百分比
by (instance) :表示的是所有节点
(1i- avg (irate(node_cpu_seconds_total [mode='idle'][5m]))by (instance))× 100:CPU5分钟内的平均使用率

其他常用的指标:
1、查询分钟平均负载超过主机cPu数量两倍的时间序列
node_load1 > on (instance) 2 * count (node_cpu_seconds_total[mode=' idle')) by(instance)
2、内存使用率
node_memory_MemTotal_ bytes
node_memory_MemFree_bytes
node_inemory_Buffers_bytes
node_memory_Cached_bytes
3、计算使用率
可用空间:以上后三个指标之和
己用空间:总空间减去可用空间
使用率:已用空间除以总空间

四、服务发现service discovery

1、Prometheus指标抓取的生命周期

发现->配置-> relabel ->指标数据抓取 -> metrics relabel

2、Prometheus的服务发现

基于文件的服务发现;
基于DNS的服务发现;
基于API的服务发现:Kubernetes、Consul、Azure、…
重新标记
target重新打标
metric重新打标
Prometheus为何要进行服务发现?

Prometheus Server的数据抓取工作于Pull模型,因而,它必需要事先知道各Target的位置,然后才能
从相应的Exporter或Instrumentation中抓取数据
对于小型的系统环境来说,通过static_configs指定各Target便能解决问题,这也是最简单的配置方法;
每个Targets用一个网络端点(ip:port进行标识;
对于中大型的系统环境或具有较强动态性的云计算环境来说,静态配置显然难以适用;因此,Prometheus为此专门
设计了一组服务发现机制,以便于能够基于服务注册中心(服务总线)自动发现、检测、分类可被监控的各Target,以
及更新发生了变动的Target

3、指标抓取的生命周期

在每个scrape_interval期间,Prometheus都会检查执行的作业(Job) ;
这些作业首先会根据Job上指定的发现配置生成target列表,此即服务发现过程;
服务发现会返回一个Target列表,其中包含一组称为元数据的标签,这些标签都以" meta_"为前缀;
服务发现还会根据目标配置来设置其它标签,这些标签带有"__“前缀和后缀,包括

scheme”、 " address"和"metrics path",分别保存有target支持使用协议(http或https,默认为http) , target的地址及指标的URI路径(默认为/metrics) ;
若URI路径中存在任何参数,则它们的前缀会设置为" param "
这些目标列表和标签会返回给Prometheus,其中的一些标签也可以配置中被覆盖;
配置标签会在抓取的生命周期中被重复利用以生成其他标签,例如,指标上的instance标签的默认值就来自于address标签的值;

counter : 计数器单调递增
gauge:仪表盘:有起伏特征的
histogram:直方图:
在一段时间范围内对数据采样的相关结果,并记入配置的bucket中,他可以存储更多的数据,包括样本值分布在每个bucket中的数量,从而prometheus就可以使用内置函数进行计算:
计算样本平均值:以值得综合除以值的数量
计算样本分位值:分位数有助于了解符合特定标准的数据个数,例如评估响应时间超过1秒的请求比例,若超过20$则进行告警等summary,摘要,histogram的扩展类型,它是直接由监控端自行聚合计算出分位数,同时将计算结果响应给prometheus
server的样本采集请求,因而,其分位数计算是由监控端完成

####作业job和实例targets/instance
①job:能够接收prometheus server数据scrape
②targets 每一个可以被监控的系统,成为targets 多个相同的targets的集合(类)称为jobinstance:实例-targets(类似),
与target相比,instance
更趋近于一个具体可以提供监控数据的实例,而targets则更像一个对象、目标性质

PromQL(数据查询语言):时序数据库使用的语音
支持两种向量,同时内置提供了一组用于数据处理的函数
①即时向量:最近以此时间戳上跟踪的数据指标
即时向量选择器:返回0个1个或多个时间序列上在给定时间戳上的各自的个样本,该样本成为即时样本
②时间范围向量:指定时间范围内所有时间戳上的数据指标
范围向量选择器:返回0个、1个或多个时间序列上在给定时间范围内的各自的一组样本(范围向量选择器无法用于绘图)
series
|. . . . . . . … .
|. . . . . . . . … .
|. . . . . . . . … .
|. . . . . . . . . … .
|. . . . . . . … .
|. . . . . . . . . … .
|. . . . . . . … .
|. . . . . . . . . … .
↓←---------------------time
即时向量:当前()的时间区间中,去一个样本值,如下:
|.
|.
|.
|.
|.
|.
范围向量:在一个时间区间

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值