关于prometheus:PrometheusGrafana性能监控

9次阅读

共计 10705 个字符,预计需要花费 27 分钟才能阅读完成。

前言

  • 前几年做性能测试监控部署了 telegraf+infludb+grafana,不便查看和存档性能监控数据,随着 K8s 流行起来,Prometheus 计划开始失去各大厂青眼,最近尝试部署 Prometheus+Grafana,再配合各种 exporter,丰盛性能测试监控伎俩。

Prometheus 简介

概念

  • Prometheus 是由 SoundCloud 开发的开源监控报警零碎和时序列数据库(TSDB)。Prometheus 应用 Go 语言开发,是 Google BorgMon 监控零碎的开源版本。
  • 2016 年由 Google 发动 Linux 基金会旗下的原生云基金会(Cloud Native Computing Foundation), 将 Prometheus 纳入其下第二大开源我的项目。
  • Prometheus 目前在开源社区相当沉闷。
  • Prometheus 和 Heapster(Heapster 是 K8S 的一个子项目,用于获取集群的性能数据。)相比性能更欠缺、更全面。Prometheus 性能也足够反对上万台规模的集群。

官网地址:https://prometheus.io/

特点

  1. 多维度数据模型
  2. 高效灵便的查问语句
  3. 不依赖分布式存储,单个服务器节点是自主的
  4. 通过基于 HTTP 的 pull 形式采集时序数据
  5. 能够通过两头网关进行时序列数据推送
  6. 通过服务发现或者动态配置来发现指标服务对象
  7. 反对多种多样的图表和界面展现,比方 Grafana 等

组件

  1. promethues server:次要获取和存储工夫序列数据
  2. exporters:次要作为 agent 收集数据发送到 prometheus server,不同的数据收集由不同的 exporters 实现,如监控服务器有 node-exporters,redis 有 redis-exporter。

    • 更多 exporters 可参看 EXPORTERS AND INTEGRATIONS
    • 对应端口号 Default port allocations · prometheus/prometheus Wiki · GitHub
  3. pushgateway:容许短暂和批处理的 jobs 推送它们的数据到 prometheus;因为这类工作的存在工夫不长,须要他们被动将数据推送到 pushgateway,而后由 pushgateway 将数据发送给 prometheus。
  4. alertmanager:实现 prometheus 的告警性能。

架构

Grafana

  • Grafana 是一个跨平台的开源的度量剖析和可视化工具,能够通过将采集的数据查问而后可视化的展现,并及时告诉。

特点

  1. 展现形式:疾速灵便的客户端图表,面板插件有许多不同形式的可视化指标和日志,官网库中具备丰盛的仪表盘插件,比方热图、折线图、图表等多种展现形式;
  2. 数据源:Graphite,InfluxDB,OpenTSDB,Prometheus,Elasticsearch,CloudWatch 和 KairosDB 等;
  3. 告诉揭示:以可视形式定义最重要指标的警报规定,Grafana 将一直计算并发送告诉,在数据达到阈值时通过 Slack、PagerDuty 等取得告诉;
  4. 混合展现:在同一图表中混合应用不同的数据源,能够基于每个查问指定数据源,甚至自定义数据源;
  5. 正文:应用来自不同数据源的丰盛事件正文图表,将鼠标悬停在事件上会显示残缺的事件元数据和标记;
  6. 过滤器:Ad-hoc 过滤器容许动态创建新的键 / 值过滤器,这些过滤器会主动利用于应用该数据源的所有查问

装置部署

prometheus

  • 手动下载,并依据 yml 配置文件启动服务

download the latest release

wget https://github.com/prometheus/prometheus/releases/download/v*/prometheus-*.*-amd64.tar.gz
tar xvf prometheus-*.*-amd64.tar.gz
cd prometheus-*
nohup ./prometheus --config.file=./prometheus.yml &

grafana

  • 手动下载,并依据 yml 配置文件启动服务

download the latest release

wget https://dl.grafana.com/oss/release/grafana-7.5.6-1.x86_64.rpm
sudo yum install grafana-7.5.6-1.x86_64.rpm

docker 装置

  • docker-compose 对立装置 prometheus 及 grafana
cd /opt
mkdir -p prometheus/config/
mkdir -p grafana/data
chmod 777 grafana/data
mkdir -p /data/prometheus
chmod 777 /data/prometheus
  • 创立 prometheus.yml 配置文件
cd /opt/prometheus/config/
touch prometheus.yml
  • 编辑 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=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

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

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


  - job_name: "node"
    static_configs:
    - targets: ["192.168.9.140:9100"]

  - job_name: "qianmingyanqian"
    static_configs:
    - targets: ["11.12.108.226:9100","11.12.108.225:9100"]

  ## config for the multiple Redis targets that the exporter will scrape
  - job_name: "redis_exporter_targets"
    scrape_interval: 5s
    static_configs:
      - targets:
        - redis://192.168.9.140:6379
        - redis://192.168.9.140:7001
        - redis://192.168.9.140:7004
    metrics_path: /scrape
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 192.168.9.140:9121
  • 创立 docker-compose_prometheus_grafana.yml 配置文件
cd /opt
mkdir docker-compose
touch docker-compose_prometheus_grafana.yml
  • 编辑 docker-compose_prometheus_grafana.yml 文件并键入
version: '2'

networks:
  monitor:
    driver: bridge

services:
  prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    hostname: prometheus
    restart: always
    volumes:
      - /opt/prometheus/config:/etc/prometheus
      - /data/prometheus:/prometheus
    ports:
      - "9090:9090"
    expose:
      - "8086"
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--log.level=info'
      - '--web.listen-address=0.0.0.0:9090'
      - '--storage.tsdb.path=/prometheus'
      - '--storage.tsdb.retention=15d'
      - '--query.max-concurrency=50'
    networks:
      - monitor

  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    hostname: grafana
    restart: always
    volumes:
      - /opt/grafana/data:/var/lib/grafana
    ports:
      - "3000:3000"
      - "26:26"
    networks:
      - monitor
    depends_on:
      - prometheus
  • docker-compose 运行 docker 容器
docker-compose -p prometheus_grafana -f docker-compose_prometheus_grafana.yml up -d
  • 启动胜利通过浏览器 http://192.168.9.140:9090/ 拜访 prometheus 服务

  • 通过浏览器 http://192.168.9.140:9090/config 拜访 prometheus 配置信息

  • 通过浏览器 3000 端口拜访 grafana 服务

监控服务器 node

装置并运行 node_exporter

  • 在待被监控的服务器上下载并装置 node_exporter

download the latest release

wget https://github.com/prometheus/node_exporter/releases/download/v*/node_exporter-*.*-amd64.tar.gz
tar xvfz node_exporter-*.*-amd64.tar.gz
cd node_exporter-*.*-amd64
nohup ./node_exporter &
  • 或者应用 docker 装置,创立 docker-compose_node-exporter.yml 配置文件
cd /opt/docker-compose
touch docker-compose_node-exporter.yml
  • 编辑 docker-compose_node-exporter.yml 配置文件如下:
---
version: '3.8'

services:
  node_exporter:
    image: quay.io/prometheus/node-exporter:latest
    container_name: node_exporter
    command:
      - '--path.rootfs=/host'
    network_mode: host
    pid: host
    restart: unless-stopped
    volumes:
      - '/:/host:ro,rslave'
  • docker-compose 运行 docker 容器
docker-compose -p node_exporter -f docker-compose_node-exporter.yml up -d
  • 尝试拜访以下
curl http://192.168.9.140:9100/metrics
  • 返回以下数据证实服务失常
# HELP node_xfs_read_calls_total Number of read(2) system calls made to files in a filesystem.# TYPE node_xfs_read_calls_total counternode_xfs_read_calls_total{device="dm-1"} 10196node_xfs_read_calls_total{device="dm-2"} 17401node_xfs_read_calls_total{device="dm-3"} 970node_xfs_read_calls_total{device="dm-4"} 10node_xfs_read_calls_total{device="dm-5"} 19node_xfs_read_calls_total{device="dm-6"} 132node_xfs_read_calls_total{device="sda2"} 16378node_xfs_read_calls_total{device="sda3"} 2.67817784e+09node_xfs_read_calls_total{device="sda6"} 1.053587e+06

配置 Prometheus

  • prometheus.yml追加以下内容
  - job_name: "node"    static_configs:    - targets: ["192.168.9.140:9100"]
  • 批改配置文件后需重启 prometheus 容器
docker restart CONTAINER ID
  • 胜利后如下图

配置 Grafana

  • 登陆后增加数据源

  • 抉择 Prometheus

  • 在 URL 输入框键入http://192.168.9.140:9090,如图所示,并保留

  • 增加 Dashboards

  • 导入模板https://grafana.com/grafana/dashboards/1860,并 load 胜利

更多模板参考

  • 具体监控页面,可依据需要抉择监控时间段

监控 redis

装置并运行 redis_exporter

  • 在待被监控的 redis 服务器上下载并装置 redis_exporter

download the latest release

wget https://github.com/oliver006/redis_exporter/releases/download/v1.23.1/redis_exporter-v1.23.1.linux-386.tar.gztar zxvf redis_exporter-v1.23.1.linux-386.tar.gznohup ./redis_exporter -redis.addr 192.168.9.140:6379 -redis.password 111111 &
  • 或者应用 docker 装置
docker run -d --name redis_exporter -p 9121:9121 oliver006/redis_exporter --redis.addr=192.168.9.140:6379 --redis.password=111111

配置 Prometheus

  • redis 单实例,prometheus.yml追加以下内容
## config for scraping the exporter itself- job_name: 'redis_exporter'  scrape_interval: 5s  static_configs:    - targets:[192.168.9.140:9121]
  • redis 集群,prometheus.yml追加以下内容
  ## config for the multiple Redis targets that the exporter will scrape  - job_name: "redis_exporter_targets"    scrape_interval: 5s    static_configs:      - targets:        - redis://192.168.9.140:6379        - redis://192.168.9.140:7001        - redis://192.168.9.140:7004    metrics_path: /scrape    relabel_configs:      - source_labels: [__address__]        target_label: __param_target      - source_labels: [__param_target]        target_label: instance      - target_label: __address__        replacement: 192.168.9.140:9121
  • 批改配置文件后需重启 prometheus 容器
docker restart CONTAINER ID
  • 胜利后如下图

配置 Grafana

  • 增加 Dashboards,导入模板https://grafana.com/grafana/dashboards/11835,并 load 胜利

  • 具体监控页面,可依据需要抉择监控时间段

监控 mysql

装置并运行 mysqld_exporter

  • 首先受权用户
root@localhost 14:43:  [(none)]>CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'mysql_exporter';Query OK, 0 rows affected (0.04 sec)root@localhost 14:43:  [(none)]>GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';Query OK, 0 rows affected (0.03 sec)
  • 创立 my.cnf 配置文件
cd /opttouch .my.cnfvim .my.cnf
  • 配置文件键入以下内容
[client]user = exporterpassword = mysql_exporter
  • 在待被监控的 mysql 服务器上下载并装置 mysqld_exporter

download the latest release

wget https://github.com/prometheus/mysqld_exporter/releases/download/v*/mysqld_exporter-*.*-amd64.tar.gztar xvfz mysqld_exporter-*.*-amd64.tar.gzcd mysqld_exporter-*.*-amd64nohup ./mysqld_exporter --config.my-cnf=/opt/.my.cnf &
  • 或者应用 docker 装置,创立 docker-compose_mysqld-exporter.yml 配置文件
cd /opt/docker-composetouch docker-compose_mysqld-exporter.yml
  • 编辑 docker-compose_mysqld-exporter.yml 配置文件如下:
version: '2'networks:    monitor:        driver: bridgeservices:    mysql-exporter:        image: prom/mysqld-exporter        container_name: mysql-exporter        hostname: mysql-exporter        restart: always        ports:            - "9104:9104"        networks:            - my-mysql-network        environment:            DATA_SOURCE_NAME: "exporter:mysql_exporter@(192.168.9.140:3306)/"networks:    my-mysql-network:        driver: bridge
  • docker-compose 运行 docker 容器
docker-compose -p mysql_exporter -f docker-compose_mysqld-exporter.yml up -d

配置 Prometheus

  • prometheus.yml追加以下内容
- job_name: 'mysql'     static_configs:     - targets: ['192.168.9.140:9104']         labels:             instance: mysql
  • 批改配置文件后需重启 prometheus 容器
docker restart CONTAINER ID
  • 胜利后如下图

配置 Grafana

  • 增加 Dashboards,导入模板https://grafana.com/grafana/dashboards/11323,并 load 胜利

  • 具体监控页面,可依据需要抉择监控时间段

如果只想单纯监控 MySQL 或 MongoDB,能够思考 PMM(Percona Monitoring and Management)监控工具,增加了慢查问收集等额定性能

监控 docker 容器

  • 为了解决 docker stats 的问题(存储、展现),谷歌开源的 cadvisor 诞生了,cadvisor 不仅能够收集一台机器上所有运行的容器信息,还提供根底查问界面和 http 接口,不便其余组件如 Prometheus 进行数据抓取
  • cAdvisor 能够对节点机器上的资源及容器进行实时监控和性能数据采集,包含 CPU 应用状况、内存应用状况、网络吞吐量及文件系统应用状况.
  • Cadvisor 应用 Go 语言开发,利用 Linux 的 cgroups 获取容器的资源应用信息,在 K8S 中集成在 Kubelet 里作为默认启动项,官网标配。

装置并运行 cadvisor

  • 应用 docker 装置,创立 docker-compose_cadvisor.yml 配置文件
cd /opt/docker-composetouch docker-compose_cadvisor.yml
  • 编辑 docker-compose_cadvisor.yml 配置文件如下:
version: '3.2'services:  cadvisor:    image: google/cadvisor:latest    container_name: cadvisor    restart: unless-stopped    ports:      - '18080:8080'    volumes:      - /:/rootfs:ro      - /var/run:/var/run:rw      - /sys:/sys:ro      - /var/lib/docker/:/var/lib/docker:ro
  • docker-compose 运行 docker 容器
docker-compose -p cadvisor -f docker-compose_cadvisor.yml up -d
  • 胜利后浏览器拜访http://11.12.110.38:18080/containers/

配置 Prometheus

  • prometheus.yml追加以下内容
  - job_name: 'cadvisor'    scrape_interval: 5s    static_configs:      - targets: ['192.168.9.140:18080']
  • 批改配置文件后需重启 prometheus 容器
docker restart CONTAINER ID
  • 胜利后如下图

配置 Grafana

  • 增加 Dashboards,导入模板https://grafana.com/grafana/dashboards/8321,并 load 胜利

  • 具体监控页面,可依据需要抉择监控时间段

问题修复

Prometheus

  • Prometheus 服务启动后通过浏览器拜访 9090 端口提醒以下错误信息
Warning: Error fetching server time: Detected 785.6099998950958 seconds time difference between your browser and the server.
  • 到装 prometheus 的机器上,同步一下时钟
ntpdate time3.aliyun.com

redis_exporter

  • prometheus.yml 配置不正确可能会提醒 too many redis instances
  • redis_exporter 启动后通过 Prometheus 服务 web 页面 /targets 下 scrape 也可能会呈现以下谬误,此时个别为 redis_exporter 启动时明码配置不正确,如局部 redis 实例不须要明码强行配置明码,或者 redis 实例须要明码而没有配置明码
- redis_exporter_last_scrape_error{err="dial redis: unknown network redis"} 1

mysqld_exporter

  • 创立 mysql 用户时可能提醒以下谬误
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
  • 刷新权限后再次执行即可
flush privileges;

后记

  • 除了上述 Linux 服务器、Redis、Mysql 等,还能够通过其余 exporter 监控对应组件,工具不是万能的,适宜并且可能疾速定位性能瓶颈最重要。

参考文档

【监控】Prometheus+Grafana 监控简介

prometheus 简介 – 云 + 社区 – 腾讯云

Prometheus 监控 redis 并预警

redis 集群监控:prometheus+redis_exporter+grafana- 博客园

Prometheus 监控 Redis 集群的正确姿态

通过 prometheus 实现的 docker 部署 mysql 监控

prometheus 监控 mysql

正文完
 0