zoukankan      html  css  js  c++  java
  • [转]Prometheus 与 Grafana 实现服务器运行状态监控

    http://flintx.me/2017/12/12/Prometheus%20+%20Grafana%20%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E5%99%A8%E8%BF%90%E8%A1%8C%E7%8A%B6%E6%80%81%E7%9B%91%E6%8E%A7/

    什么是Prometheus?

    Prometheus 是由 SoundCloud 开发的开源监控报警系统和时序列数据库(TSDB)。

    Prometheus 使用 Go 语言开发,是 Google BorgMon 监控系统的开源版本。

    其架构如下:

    Prometheus 的基本原理是:

    通过HTTP协议周期性抓取被监控组件的状态,任意组件只要提供对应的 HTTP 接口就可以接入监控。不需要任何SDK或者其他的集成过程。

    Prometheus服务过程大概是这样:

    • Prometheus Daemon负责定时去目标上抓取metrics(指标)数据,每个抓取目标需要暴露一个 http 服务的接口给它定时抓取。Prometheus 采用 pull 的方式进行监控,即服务器可以直接通过目标 pull 数据.
    • Prometheus 在本地存储抓取的所有数据,并通过一定规则进行清理和整理数据,并把得到的结果存储到新的时间序列中。
    • Prometheus 通过 PromQL和其他 API 可视化地展示收集的数据。Prometheus 支持很多方式的图表可视化,例如 Grafana、自带的Promdash以及自身提供的模版引擎等等。Prometheus 还提供 HTTP API 的查询方式,自定义所需要的输出。
    • PushGateway支持 client 主动推送 metrics 到 PushGateway,而 Prometheus 只是定时去 Gateway 上抓取数据。
    • Alertmanager是独立于Prometheus的一个组件,可以支持 Prometheus 的查询语句,提供十分灵活的报警方式。

    而我们的使用场景如下:

    • 4 台需要被监控的服务器;
    • 监控项主要有 CPU、内存的使用情况,可以使用 node_exporter 插件实现;
    • 1 台服务器既被监控同时也作为 server 来接收其余 3 台服务器的运行状况;
    • 可视化部分用 Grafana 来完成。

    Prometheus 以及 node_exporter 的安装

    1. 先在 server 机(用了4号机作为 server)上下载最新的 release 并解压 : https://prometheus.io/download/

    • $ wget https://github.com/prometheus/prometheus/releases/download/v2.0.0/prometheus-2.0.0.linux-amd64.tar.gz
      $ tar xvfz prometheus-*.tar.gz
      $ mv prometheus-2.0.0.linux-amd64 prometheus # 为文件夹改名
    • 在 prometheus 目录下有一个名为 prometheus.yml 的主配置文件。查看其内容:

    $ cd prometheus
    $ vim 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: ['localhost:9090']

      其中 scrape_configs 项目中的内容是我们主要关注的,该配置项决定了我们的 Prometheus 需要去抓取哪些数据,默认配置中只有一个 prometheusjob ,这个 job 是 Prometheus 自带的,它的功能是提供 Prometheus 进程本身的运行数据以供我们抓取。

      除了 prometheus 这个 job 以外,我们还没任何为 Prometheus 提供数据的 HTTP API 程序。于是我们先将 Prometheus 的配置任务放去后面,下一步是在四台服务器上安装 node_exporter

    • 在 4 台需要监控的服务器上下载解压 node_exporterhttps://prometheus.io/download/#node_exporter

    • $ wget https://github.com/prometheus/node_exporter/releases/download/v0.15.1/node_exporter-0.15.1.linux-amd64.tar.gz
      $ tar -zxvf node_exporter-0.15.1.linux-amd64.tar.gz
      $ mv node_exporter-0.15.1.linux-amd64 prometheus/node_exporter # 将文件夹重命名后移入prometheus 文件夹
      $ ls prometheus
      console_libraries consoles data LICENSE node_exporter NOTICE prometheus prometheus.yml promtool

      由于 node_exporter 是编译好的程序,我们可以将其配为服务直接运行。

    • 在 4 台需要监控的服务器上配置 node_exporter 服务:

    $ sudo vim /etc/systemd/system/node_exporter.service

    内容如下:

    [Unit]
    Description=Node Exporter
     
    [Service]
    User=admin
    ExecStart=/home/admin/prometheus/node_exporter/node_exporter
     
    [Install]
    WantedBy=default.target

    运行服务装载命令:

    $ sudo systemctl daemon-reload
    $ sudo systemctl enable node_exporter.service
    $ sudo systemctl start node_exporter.service
    $ systemctl status node_exporter
    ● node_exporter.service - Node Exporter
    Loaded: loaded (/etc/systemd/system/node_exporter.service; enabled; vendor preset: disabled)
    Active: active (running) since 二 2017-12-05 17:39:32 CST; 1 day 2h ago
    Main PID: 4752 (node_exporter)
    CGroup: /system.slice/node_exporter.service
    └─4752 /home/admin/prometheus/node_exporter/node_exporter

    node_exporter 的 HTTP API 默认端口是 9100 ,为其开放防火墙:

    • $ sudo firewall-cmd --zone=public --add-port=9100/tcp --permanent ; sudo firewall-cmd --reload
      # 测试
      $ curl http://localhost:9100
      <html>
      <head><title>Node Exporter</title></head>
      <body>
      <h1>Node Exporter</h1>
      <p><a href="/metrics">Metrics</a></p>
      </body>
      </html>

    • 重新编辑 server 机上的 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: ['localhost:9090']
       
      # 添加的部分
       
      - job_name: "node1"
      static_configs:
      - targets: ['111.x.xxx.20:9100']
       
      - job_name: "node2"
      static_configs:
      - targets: ['111.x.xxx.20:9100']
      - job_name: "node3"
      static_configs:
      - targets: ['111.x.xxx.20:9100']
      - job_name: "node4"
      static_configs:
      - targets: ['111.x.xxx.21:9100']

    • 配置 Prometheus 服务并运行:

    1. $ sudo vim /etc/systemd/system/prometheus.service
      $ cat vim /etc/systemd/system/prometheus.service
      [Unit]
      Description=Node Exporter
      [Service]
      User=root
      ExecStart=/home/admin/prometheus/prometheus --config.file=/home/admin/prometheus/prometheus.yml
      [Install]
      WantedBy=default.target
      $ sudo systemctl daemon-reload
      $ sudo systemctl enable prometheus.service
      $ sudo systemctl start prometheus.service
      $ systemctl status prometheus
      ● prometheus.service - Node Exporter
      Loaded: loaded (/etc/systemd/system/prometheus.service; enabled; vendor preset: disabled)
      Active: active (running) since 三 2017-12-06 20:43:35 CST; 3s ago
      Main PID: 5918 (prometheus)
      CGroup: /system.slice/prometheus.service
      └─5918 /home/admin/prometheus/prometheus --config.file=/home/admin/prometheus/prometheus.yml
       
      # 为 Prometheus 开启防火墙
      $ sudo firewall-cmd --zone=public --add-port=9090/tcp --permanent ; sudo firewall-cmd --reload

    2. 测试,访问 http://yourserverip:9090/graph ,执行 node_load1 可绘制 CPU 瞬时运行图,也可以通过 http://yourserverip:9090/targets 来查看 job 的部署情况,通过 http://yourserverip:9090/metrics。

    Grafana 的安装

    用 Prometheus 自带的控制面板略显简陋,一时要手动刷新数据,二是可视化功能不够强大、不支持自定义数据显示,三是执行命令太过繁琐。

    于是我们选择 Grafana 来作为可视化工具。

    其安装过程(可在任意主机安装)如下:

    $ sudo yum install https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-4.6.2-1.x86_64.rpm
    # 为 Grafana 开启防火墙
    $ sudo firewall-cmd --zone=public --add-port=9090/tcp --permanent ; sudo firewall-cmd --reload

    启动服务:

    $ sudo service grafana-server start

    启动后可使用 http://yourip:3000 访问 Grafana 页面,配置好用户名和密码后,先设置好数据源(data source),也就是从哪里获取数据。

    参考:

    可视化的具体配置可参考官方文档,推荐使用 Node Exporter Server Metrics 模板,访问 https://grafana.com/dashboards/405 ,在 Dashboard 中导入该模板即可

  • 相关阅读:
    .NET Core使用SignalR做登录、推送
    记录node-sass安装失败的解决方法
    .NET Core微服务二:Ocelot API网关
    .NET Core微服务一:Consul服务中心
    ASP.NET Core下Ocelot的简单使用
    ASP.Net Core 发布到IIS Http Error 502.5 官方解决办法
    Windows版Redis主从配置
    IIS 32位/64位 全局模式切换
    NOIP 2007 普及组 初赛 试卷+答案
    P1066 2^k进制数 NOIP 2006 提高组 第四题
  • 原文地址:https://www.cnblogs.com/wincai/p/9224295.html
Copyright © 2011-2022 走看看