栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Prometheus+Grafana学习(五) 安装Prometheus

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Prometheus+Grafana学习(五) 安装Prometheus

文章目录

前言1、Prometheus介绍

1.1、Prometheus组件说明1.2、Prometheus的特点1.3、Prometheus的架构1.4、Prometheus的使用场景 2、安装Prometheus

1.下载2.安装Prometheus3.注册系统服务4.启动服务 3、配置Prometheus

3.1配置总体说明3.2global配置3.3 alerting告警配置3.4 rule_files规则文件配置3.5 scrape_configs拉取配置3.6 remote_read/remote_write远程读写配置3.7 修改prometheus.yml

前言

前面章节的Prometheus是用Docker安装的,需要依赖Docker且不易于持久化。下面介绍手动安装。

1、Prometheus介绍

Prometheus是最初在SoundCloud上构建的开源系统监视和警报工具包。自2012年成立以来,许多公司和组织都采用了Prometheus,该项目拥有非常活跃的开发人员和用户社区。Prometheus 于2016年加入了 Cloud Native Computing Foundation,这是继Kubernetes之后的第二个托管项目。
官网:https://prometheus.io
最新版本:2.32.1

1.1、Prometheus组件说明
组件组件说明
Prometheus开源的系统监控和报警框架,灵感源自Google的Borgmon监控系统
alertManager处理由客户端应用程序(如Prometheus server)发送的警报。它负责将重复数据删除,分组和路由到正确的接收者集成,还负责沉默和抑制警报
Node_Exporter用来监控各节点的资源信息的exporter,应部署到prometheus监控的所有节点
PushGateway推送网关,用于接收各节点推送的数据并暴露给Prometheus server

Exporter是一个采集监控数据并通过Prometheus监控规范对外提供数据的组件,能为Prometheus提供监控的接口。
Exporter将监控数据采集的端点通过HTTP服务的形式暴露给Prometheus Server,Prometheus Server通过访问该Exporter提供的Endpoint端点,即可获取到需要采集的监控数据。不同的Exporter负责不同的业务。

1.2、Prometheus的特点
    多维的数据模型(基于时间序列的Key、Value键值对)灵活的查询和聚合语言PromQL提供本地存储和分布式存储通过基于HTTP的Pull模型采集时间序列数据可利用Pushgateway(Prometheus的可选中间件)实现Push模式可通过动态服务发现或静态配置发现目标机器支持多种图表和数据大盘
1.3、Prometheus的架构

1.4、Prometheus的使用场景

prometheus非常适合记录任何纯数字时间序列。它既适合以机器为中心的监视,也适合监视高度动态的面向服务的体系结构。在微服务世界中,它对多维数据收集和查询的支持是一种特别的优势。
prometheus的设计旨在提高可靠性,使其成为中断期间要使用的系统,从而使您能够快速诊断问题。每个prometheus服务器都是独立的,而不依赖于网络存储或其他远程服务,当基础设施部分出现问题时仍然可以使用它。

2、安装Prometheus

当前安装版本为2.32.1。

1.下载

下载地址:https://prometheus.io/download/

wget https://github.com/prometheus/prometheus/releases/download/v2.32.1/prometheus-2.32.1.linux-amd64.tar.gz
2.安装Prometheus
tar zxvf prometheus-2.32.1.linux-amd64.tar.gz
mv prometheus-2.32.1.linux-amd64 /usr/local/Prometheus
mkdir -p /data/Prometheus
3.注册系统服务
vi /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
After=network.target

[Service]
Type=simple
Environment="GOMAXPROCS=4"
#User=prometheus
#Group=prometheus
ExecReload=/bin/kill -HUP $MAINPID
ExecStart=/usr/local/prometheus/prometheus 
  --config.file=/usr/local/prometheus/prometheus.yml 
  --storage.tsdb.path=/data/prometheus 
  --storage.tsdb.retention=30d 
  --web.console.libraries=/usr/local/prometheus/console_libraries 
  --web.console.templates=/usr/local/prometheus/consoles 
  --web.listen-address=0.0.0.0:9090 
  --web.read-timeout=5m 
  --web.max-connections=10 
  --query.max-concurrency=20 
  --query.timeout=2m 
  --web.enable-lifecycle
PrivateTmp=true
PrivateDevices=true
ProtectHome=true
NoNewPrivileges=true
LimitNOFILE=infinity
ReadWriteDirectories=/data/prometheus
ProtectSystem=full

SyslogIdentifier=prometheus
Restart=always

[Install]
WantedBy=multi-user.target
4.启动服务
systemctl daemon-reload
systemctl enable prometheus
systemctl start prometheus

访问http://172.16.10.171:9090

这样就启动成功了

3、配置Prometheus 3.1配置总体说明

配置文件在/usr/local/prometheus/prometheus.yml,主要分以下几个配置模块:

global全局配置
alerting告警配置
rule_files规则文件配置
scrape_configs拉取配置
remote_read、remote_write远程读写配置
3.2global配置

global指定在所有其他配置上下文中有效的参数。还可用作其他配置部分的默认设置。

global:
  # 默认拉取频率
  [ scrape_interval:  | default = 1m ]

  # 拉取超时时间
  [ scrape_timeout:  | default = 10s ]

  # 执行规则频率
  [ evaluation_interval:  | default = 1m ]

  # 通信时添加到任何时间序列或告警的标签
  # external systems (federation, remote storage, alertmanager).
  external_labels:
    [ :  ... ]

  # 记录PromQL查询的日志文件
  [ query_log_file:  ]
3.3 alerting告警配置

alerting指定与alertmanager相关的设置。

alerting:
  alert_relabel_configs:
    [ -  ... ]
  alertmanagers:
    [ -  ... ]
3.4 rule_files规则文件配置

rule_files指定prometheus加载的任何规则的位置,从所有匹配的文件中读取规则和告警。

rule_files:
  [ -  ... ]
3.5 scrape_configs拉取配置

scrape_configs指定prometheus监控哪些资源。默认会拉取prometheus本身的时间序列数据,通过http://ip:9090/metrics进行拉取。
一个scrape_config指定一组目标和参数,描述如何拉取它们。在一般情况下,一个拉取配置指定一个作业。在高级配置中,这可能会改变。
可以通过static_configs参数静态配置目标,也可以使用支持的服务发现机制之一动态发现目标。
此外,relabel_configs在拉取之前,可以对任何目标及其标签进行修改。

scrape_configs:
job_name: 

# 拉取频率
[ scrape_interval:  | default =  ]

# 拉取超时时间
[ scrape_timeout:  | default =  ]

# 拉取的http路径
[ metrics_path:  | default = /metrics ]

# honor_labels 控制prometheus处理已存在于收集数据中的标签与prometheus将附加在服务器端的标签("作业"和"实例"标签、手动配置的目标标签和由服务发现实现生成的标签)之间的冲突
# 如果 honor_labels 设置为 "true",则通过保持从拉取数据获得的标签值并忽略冲突的服务器端标签来解决标签冲突
# 如果 honor_labels 设置为 "false",则通过将拉取数据中冲突的标签重命名为"exported_"来解决标签冲突(例如"exported_instance"、"exported_job"),然后附加服务器端标签
# 注意,任何全局配置的 "external_labels"都不受此设置的影响。在与外部系统的通信中,只有当时间序列还没有给定的标签时,它们才被应用,否则就会被忽略
[ honor_labels:  | default = false ]

# honor_timestamps 控制prometheus是否遵守拉取数据中的时间戳
# 如果 honor_timestamps 设置为 "true",将使用目标公开的metrics的时间戳
# 如果 honor_timestamps 设置为 "false",目标公开的metrics的时间戳将被忽略
[ honor_timestamps:  | default = true ]

# 配置用于请求的协议
[ scheme:  | default = http ]

# 可选的http url参数
params:
  [ : [, ...] ]

# 在每个拉取请求上配置 username 和 password 来设置 Authorization 头部,password 和 password_file 二选一
basic_auth:
  [ username:  ]
  [ password:  ]
  [ password_file:  ]

# 在每个拉取请求上配置 bearer token 来设置 Authorization 头部,bearer_token 和 bearer_token_file 二选一
[ bearer_token:  ]

# 在每个拉取请求上配置 bearer_token_file 来设置 Authorization 头部,bearer_token_file 和 bearer_token 二选一
[ bearer_token_file: /path/to/bearer/token/file ]

# 配置拉取请求的TLS设置
tls_config:
  [  ]

# 可选的代理URL
[ proxy_url:  ]

# Azure服务发现配置列表
azure_sd_configs:
  [ -  ... ]

# Consul服务发现配置列表
consul_sd_configs:
  [ -  ... ]

# DNS服务发现配置列表
dns_sd_configs:
  [ -  ... ]

# EC2服务发现配置列表
ec2_sd_configs:
  [ -  ... ]

# OpenStack服务发现配置列表
openstack_sd_configs:
  [ -  ... ]

# file服务发现配置列表
file_sd_configs:
  [ -  ... ]

# GCE服务发现配置列表
gce_sd_configs:
  [ -  ... ]

# Kubernetes服务发现配置列表
kubernetes_sd_configs:
  [ -  ... ]

# Marathon服务发现配置列表
marathon_sd_configs:
  [ -  ... ]

# AirBnB's Nerve服务发现配置列表
nerve_sd_configs:
  [ -  ... ]

# Zookeeper Serverset服务发现配置列表
serverset_sd_configs:
  [ -  ... ]

# Triton服务发现配置列表
triton_sd_configs:
  [ -  ... ]

# 静态配置目标列表
static_configs:
  [ -  ... ]

# 目标relabel配置列表
relabel_configs:
  [ -  ... ]

# metric relabel配置列表
metric_relabel_configs:
  [ -  ... ]

# 每次拉取样品的数量限制
# metric relabelling之后,如果有超过这个数量的样品,整个拉取将被视为失效。0表示没有限制
[ sample_limit:  | default = 0 ]
3.6 remote_read/remote_write远程读写配置

remote_read/remote_write将数据源与prometheus分离。

# 与远程写功能相关的设置
remote_write:
  [ -  ... ]

# 与远程读功能相关的设置
remote_read:
  [ -  ... ]
3.7 修改prometheus.yml

将前几章部署的springboot、mysql、nginx的exporter配置信息,配置到prometheus.yml中。

vi /usr/local/prometheus/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
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=` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]
  - job_name: "spring"
    scrape_interval: 15s
    scrape_timeout: 10s
    metrics_path: "/demo/actuator/prometheus"
    static_configs:
    - targets: ["192.168.100.88:7901"]
  - job_name: "mysql8.x"
    static_configs:
    - targets: ["172.16.10.169:9104", "192.168.100.88:9104"]
  - job_name: "nginx"
    static_configs:
    - targets: ["172.16.10.171:9913"]

修改完后重启下prometheus

systemctl restart Prometheus

访问prometheus的targets: http://172.16.10.171:9090/targets

发现除了自身的prometheus,一个springboot项目,两个mysql,一个nginx,都接入进来了。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/705483.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号