prometheus基本使用

一、介绍

1.基本概念

prometheus是一种时间序列的数据库,适合应用于监控以及告警,但是不适合100%的准确计费,因为采集的数据不一定很准确,主要是作为监控以及收集内存、CPU、硬盘的数据。

前身是SoundCloud警告工具包,在2016年继Kubernetes之后,加入了 Cloud Native Computing Foundation。

1.1 主要特征
  • 多维数据模型(时序数据由 metric名和一组 key/value组成)
  • 在多维度上灵活的查询语言(PromQI)
  • 不依赖分布式存储,单主节点工作
  • 通过基于HTTP的pull方式采集时序数据
  • 也通过中间网关进行时序数据推送(pushing)
  • 通过服务发现或静态配置,来发现目标服务对象
  • 多种可视化和仪表盘支持,如grafana
1.2 相关组件

Prometheus生态系统由多个组件组成,其中许多是可选的:

  • Prometheus Server::负责对监控数据的获取,存储以及查询

  • Client Library: 客户端库,负责检测应用程序代码

  • Push Gateway:正常情况下Prometheus Server能够直接与Exporter进行通信,然后pull数据;当网络需求无法满足时就可以使用PushGateway作为中转站了

  • Exporter:监控数据采集器,将数据通过Http的方式暴露给Prometheus Server;

  • AlertManager::Prometheus支持通过PromQL来创建告警规则,满足规则时创建一条告警,后续的告警流程就交给AlertManager,其提供了多种告警方式包括email,webhook等方式;

  • Web UI:简单的web控制台

1.3 整体架构

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-reh69ymx-1652268438509)(https://prometheus.io/assets/architecture.svg)]

1.4 四种指标
  • Counter-计数器:
    递增的计数器,这类指标只增不减,常用来统计http请求数、下单数等指标。

  • Gauge-测量仪:
    可以任意变化的数值,这类指标可增可减,常用来统计比如CPU、内存、在线用户等标。

  • Histogram-直方图:
    对一对时间范围内数据进行采样,并对所有数值求和与统计数量。这类指标使用分桶方式来统计样本分布,比如请求的延迟是落在哪个区间范围内的。

  • Summary-汇总:
    这类指标是根据样本计算出百分位的,是在客户端计算好的然后被抓取到promethues中的。比如99%、90%、85%、70%/60%的响应时间在哪个区间。

1.5 数据模型
  • Prometheus将所有数据存储为时间序列,具有相同度量名称以及标签属于同一指标
  • 每个时间序列都由度量标准名称和一组键值对(也称为标签)的唯一标识

时间序列格式:

<metric name> {<label name>=<label value>,...}

示例: api_http_requests_total{method=“POST”, handler=“/messages”}

1.5 作业和实例

实例: 可以抓取的目标成为实例(instances)

工作: 具有相同目标的实例集合成为作业(job)

示例:

scape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']
    
  - job_name: 'node'
    static_configs:
    - targets: ['10.10.10.10:9090']

2.与zabbix对比

Prometheus vs Zabbix

  • Zabbix 使用的是 C 和 PHP, Prometheus 使用 Golang, 整体而言 Prometheus 运行速度更快一点。
  • Zabbix 属于传统主机监控,主要用于物理主机,交换机,网络等监控,Prometheus 不仅适用主机监控,还适用于 Cloud, SaaS, Openstack,Container 监控。
  • Zabbix 在传统主机监控方面,有更丰富的插件
  • Zabbix 可以在 WebGui 中操作配置,Prometheus 需要手动修改文件配置。

总结


  • Prometheus 属于一站式监控告警平台,依赖少,功能齐全

  • Prometheus 支持对云或容器的监控,其他系统主要对主机监控

  • Prometheus 数据查询语句表现力更强大,内置更强大的统计函数

  • Prometheus 在数据存储扩展性以及持久性上没有 InfluxDB,OpenTSDB,Sensu 好

二.Prometheus部署

  • 二进制部署:https://prometheus.io/docs/prometheus/latest/getting_started/

  • Docker部署:https://prometheus.io/docs/prometheus/latest/installation/

1. Server端配置

docker部署

vi docker-compose.yaml


version: '3'
services:

  prometheus:
    image: prom/prometheus
    container_name: prometheus
    volumes:
      - "/home/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml"
      - "/etc/localtime:/etc/localtime"

# 自动发现配置文件目录
#      - "/home/prometheus/sd_config/:/etc/prometheus/sd_config/"

    ports:
      - "9090:9090"

prometheus.yml 配置

Configuration | Prometheus

vi prometheus.yml

# 全局配置
global:
  # 设置间隔时间为每15秒,默认为每1分钟
  scrape_interval: 15s

  # 计算采集数据时间间隔,并对变更进行更新。
  evaluation_interval: 15s


# 监控指标配置
scrape_configs:
  # 作业名称,可以有多个作业
  - job_name: 'prometheus'

    # 设置采集周期,默认使用全局得
    scrape_interval: 5s

  # 静态配置监控实例
    static_configs:
      # 监控目标主机地址
      - targets: ['localhost:9090']
        labels:
          idc: local

基于文件服务发现

vi perometheus.yml


# 在scrape_configs得 job_name下面添加如下配置
file_sd_configs:
  - files: ['/etc/prometheus/sd_config/*.yml']
    refresh_interval: 5s

# 创建新配置文件
mkdir /etc/prometheus/sd_config/
cd /etc/prometheus/sd_config/
vi test.yml

- targets: [10.10.10.10:9090]

## 或者 写成

- targets:
  - 10.10.10.10:9090
  - 10.10.10.11:9090
2. node_exporter配置

Monitoring Linux host metrics with the Node Exporter | Prometheus

GitHub - prometheus/node_exporter: Exporter for machine metrics

Download | Prometheus

2.1 二进制程序 部署
  • 下载程序包

    wget https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz
    
  • 解压并创建 软连接

    tar xvf  node_exporter-1.3.1.linux-amd64.tar.gz -C /usr/local/
    cd /usr/local/
    ln -s node_exporter-1.3.1.linux-amd64/ node_exporter
    
  • 创建service脚本,由systemd管理

    vi /usr/lib/systemd/system/node_exporter.service

    [Unit]
    Description=node_exporter.service
    
    [Service]
    Restart=no-failure
    # --web.listen-address=:9100 指定端口
    ExecStart=/usr/local/node_exporter/node_exporter --web.listen-address=:9100
    
    [Install]
    WantedBy=multi-user.target
    
  • 启动服务

    systemctl daemon-reload
    
    systemctl start node_exporter
    
    systemctl enable node_exporter
    
    
  • 重载配置文件

    docker exec -it prometheus kill -HUP 1
    
2.2 docker 部署

vi docker-compose.yaml

version: '3'
services:
  node_exporter:
    image: prom/node-exporter 
    container_name: node_exporter
    command:
      - '--path.rootfs=/host'
    network_mode: host
    pid: host
    restart: unless-stopped
    volumes:
      - '/:/host:ro,rslave'

perometheus.yml 中添加配置

vi perometheus.yml

  - job_name: node
    static_configs:
      - targets: ['localhost:9100']
      
# 或者配置自动发现
  - job_name: node
	file_sd_configs:
  	- files: ['/etc/prometheus/sd_config/*.yml']
      refresh_interval: 5s

2.3 启用密码认证
  • 生成加密密文

htpasswd 创建密码

# 安装 htpasswd命令软件
yum install httpd-tools -y

# 创建密文
htpasswd -nBC 12 '' | tr -d ':\n'
 
 
$2y$12$LM2y0E3sXT4LSdJFPI4j6euPSqVnou7Nl39nP9b1txcwgI5dTTgru
  • 创建密码配置文件
cd /usr/local/node_exporter

vi config.yml

basic_auth_users:
	# 当前设置的用户名为admin,可以设置多个
  admin: $2y$12$LM2y0E3sXT4LSdJFPI4j6euPSqVnou7Nl39nP9b1txcwgI5dTTgru

  • node_exporter启动时引用该配置
# 修改 service中 Execstart

vi /usr/lib/systemd/system/node_exporter.service

/usr/local/node_exporter/node_exporter --web.config=/usr/local/node_exporter/config.yml
  • 重启服务
# 重启加载,并重启
systemctl daemon-reload 
systemctl restart node_exporter.service
  • 配置Prometheus启用用户名密码访问
  - job_name: node
  	basic_auth:
  		username: admin
  		password: 123456
    static_configs:
      - targets: ['localhost:9100']

三.grafana展示

1.docker安装grafana

vi docker-compose.yml

version: '3'
services:
  grafana:
    image: grafana/grafana
    container_name: grafana
    volumes:
      - "/home/grafana/data:/var/lib/grafana"
    ports:
      - "3000:3000"
    environment:
      GF_SECURITY_ADMIN_PASSWORD: password

设置权限

chmod 777 /home/grafana/data

2.配置可视化显示
2.1 登录 granfana

登录 http://10.10.10.200:3000

账号:admin 密码: password

2.2. 配置数据源
  • 点击 “设置” 图标
  • 选择 “Data Sources”
  • 选择 “Prometheus”
  • 填写 URL 为 node_exporter地址,例如:http://10.10.10.200:9090
  • 拉到页面最后,点击 Save
2.3 创建可视化dashboard
  • 使用 grafana 官网现成面板:
    地址:https://grafana.com/grafana/dashboards/
  • 搜索 “Node Exporter Full”
    https://grafana.com/grafana/dashboards/1860
  • 可以看到 面板对应ID,1860
  • 在granfana页面,点击 "加号"图标,选择 “Import”
    输入 1860 ,点击 Load ,即加载面板
  • 常用好看得 8919
2.4.修改默认 Dashboards

修改Grafana 默认的 home Dashboards_LQ_2021的博客-CSDN博客_grafana设置默认主页

  • Dashboards首页 将要设置得仪表盘 加星
  • 左侧菜单上,将光标停留 Configuration (齿轮)图标上,然后单击 Preferences
  • 在 “Home Dashboard” 中选择要设置得默认 仪表盘
  • 点击 save 保存

参考:

【云原生学习】史上最全Prometheus学习笔记_九离⠂的博客-CSDN博客_prometheus 云原生
Introduction · Prometheus中文技术文档

猜你喜欢

转载自blog.csdn.net/wq1205750492/article/details/124716954