Promethus(普罗米修斯)监控系统


前言

安装,配置和使用简单的Prometheus实例。您将在本地下载并运行Prometheus,对其进行配置以抓取自身和示例应用程序,然后使用查询,规则和图形使用收集的时间序列数据。


一、什么是Prometheus(普罗米修斯)?

Prometheus是最初在SoundCloud上构建的开源系统监视和警报工具包 。自2012年成立以来,许多公司和组织都采用了Prometheus,该项目拥有非常活跃的开发人员和用户社区。现在,它是一个独立的开源项目,并且独立于任何公司进行维护。为了强调这一点并阐明项目的治理结构,Prometheus在2016年加入了 Cloud Native Computing Foundation,这是继Kubernetes之后的第二个托管项目。

1.1特征

  • prometheus server:主要用于抓取数据和存储时序数据,另外还提供查询和 Alert Rule 配置管理
  • client libraries:用于对接 Prometheus Server,检测应用程序代码可以查询和上报数据
  • push gateway:用于批量,短期的监控数据的汇总节点,主要用于业务数据汇报等
  • exporters:各种汇报exporter,例如node_exporter,mysql_exporter,HAProxy,StatsD,Graphite
  • alertmanager:告警通知管理

1.2架构

下图说明了Prometheus的体系结构及其某些生态系统组件:

在这里插入图片描述
Prometheus直接或通过中间推送网关从已检测作业中删除指标,以用于短期作业。它在本地存储所有报废的样本,并对这些数据运行规则,以汇总和记录现有数据中的新时间序列,或生成警报。Grafana或其他API使用者可以用来可视化收集的数据。

二、下载Prometheus

个人喜好在根目录下创建一个文件夹把软件下载过去

创建文件夹
# mkdir -p /data/source
# cd /data/source/
官网下载链接
# wget https://github.com/prometheus/prometheus/releases/download/v2.23.0/prometheus-2.23.0.linux-amd64.tar.gz

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

三、安装

3.1下载最新版本的Prometheus后

解压缩到/usr/local/share/prometheus
#tar -zxvf prometheus-2.23.0.linux-amd64.tar.gz -C /usr/local/share/
进入文件夹
#cd /usr/local/share/
给他起个短一点的名字
#mv prometheus-2.23.0.linux-amd64 prometheus
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Prometheus服务器是一个称为prometheus的二进制文件。我们可以运行二进制文件,并通过传递–help标志来查看有关其选项的帮助。

查看帮助
#./prometheus --help
usage: prometheus [<flags>]

The Prometheus monitoring server

Flags:
  -h, --help                     Show context-sensitive help (also try --help-long and --help-man).
      --version                  Show application version.
      --config.file="prometheus.yml"  
                                 Prometheus configuration file path.
      --web.listen-address="0.0.0.0:9090"  

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

*在启动Prometheus之前,先对其进行配置。

3.2配置Prometheus

Prometheus配置为YAML。Prometheus下载在一个名为的文件中带有一个示例配置,prometheus.yml这是一个入门的好地方。

删除了示例文件中的大多数注释,以使其更加简洁(注释以开头的行#)。

配置如下:

global:		#global块控制Prometheus服务器的全局配置
  scrape_interval:     15s		#scrape_interval控制,Prometheus多久刮一次目标。
  evaluation_interval: 15s		#evaluation_interval选项控制Prometheus多久评估一次规则。

alerting:
  alertmanagers:
  - static_configs:
    - targets:

rule_files:		#rule_files块指定希望Prometheus服务器加载的任何规则的位置。
  # - "first_rules.yml"
  # - "second_rules.yml"

scrape_configs:		#scrape_configs控制Prometheus监视哪些资源。
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

3.3运行普罗米修斯

要使用我们新创建的配置文件启动Prometheus,转到包含Prometheus二进制文件的目录并运行:

./prometheus --config.file=prometheus.yml
  • 1

运行log
在这里插入图片描述

理论上来说这样就算是安装完成了,但这样太简陋了。因为每次启动 Prometheus server 都需要手动执行命令:

# /usr/local/share/prometheus/prometheus --config.file=/usr/local/share/prometheus/prometheus.yml
  • 1

这实在是太不方便了!应该把它配置成服务,用 systemd 来管理。

四、添加到systemd来管理

4.1先创建一个名为 prometheus 的用户:

# adduser prometheus
  • 1

把目录 /usr/local/share/prometheus/ 的所有者设置为 prometheus 用户:

# chown -R prometheus:prometheus /usr/local/share/prometheus/
  • 1

然后创建文件 /etc/systemd/system/prometheus.service,内容如下:

[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io/docs/introduction/overview/
After=network.target

[Service]
User=prometheus
Restart=on-failure
WorkingDirectory=/usr/local/share/prometheus/
ExecStart=/usr/local/share/prometheus/prometheus --config.file=/usr/local/share/prometheus/prometheus.yml

[Install]
WantedBy=multi-user.target
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

好了,现在可以通过 systemd 来控制 Prometheus 服务了,
重新载入systemctl服务:

# systemctl daemon-reload
  • 1

启动服务:

# systemctl start prometheus
  • 1

再把服务配置为开机时启动:

# systemctl enable prometheus
  • 1

检查一下服务的状态:

# systemctl status prometheus
  • 1

在这里插入图片描述

五、web端

现在可以在浏览器上浏览到有关其自身的状态页。
在这里插入图片描述
还可以通过导航到自己的指标终结点:http:// localhost:9090 / metrics来验证Prometheus是否正在提供有关自身的指标。

5.1用浏览器查看数据

让我们尝试查看Prometheus收集的有关自身的一些数据。要使用Prometheus的内置表达式浏览器,请导航至 http:// localhost:9090 / graph并在“图形”选项卡中选择“控制台”视图。

正如您可以从http:// localhost:9090 / metrics收集的那样,称为Prometheus导出的有关其自身的一个指标 promhttp_metric_handler_requests_total(/metricsPrometheus服务器已处理的请求总数)。继续并将其输入到表达式控制台中:

promhttp_metric_handler_requests_total
  • 1

在这里插入图片描述
Graphite InfluxDB Kapacitor OpenTSDB Nagios Sensu


六、使用NODE EXPORTER监视LINUX主机指标

Node Exporter公开了各种与硬件和内核相关的指标。

在localhost配置为从运行的Node Exporter刮取指标的Prometheus实例上启动

安装并运行节点导出器

Prometheus Node Exporter是一个单个静态二进制文件,可以通过tarball安装。从Prometheus下载页面下载后,将其解压缩并运行:

# 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
./node_exporter
  • 1
  • 2
  • 3
  • 4

您应该看到类似以下的输出,表明节点导出器正在运行,并且在端口9100上公开了指标:
在这里插入图片描述

节点导出器指标

安装并运行节点导出器后,可以通过对/metrics端点进行cURL验证来导出指标:

http://localhost:9100/metrics
  • 1

您应该看到如下输出:
在这里插入图片描述

配置您的Prometheus实例

需要正确配置本地运行的Prometheus实例,才能访问Node Exporter指标。以下prometheus.yml示例配置文件将通过以下命令告诉Prometheus实例从Node Exporter进行抓取以及抓取频率localhost:9100:
在这里插入图片描述

本节说明如何下载和安装Grafana,如何在基于RPM的Linux系统上启动并运行该服务,以及安装软件包的详细信息。

6.1下载并安装

# wget <rpm package url>
  • 1
# rpm -Uvh <local rpm package>
  • 1

6.2启动服务器

grafana-server将以grafana用户身份启动该过程,该过程是在软件包安装期间创建的。systemd命令在大多数情况下都可以使用,但是某些较旧的Linux系统可能需要init.d。安装程序应提示您输入正确的命令。

如果安装了.rpm软件包,则可以使用systemd或启动服务器init.d。如果安装了二进制.tar.gz文件,则需要执行二进制文件。

6.2.1用systemd启动服务器

要启动服务并验证服务已启动:

# systemctl daemon-reload
# systemctl start grafana-server
# systemctl status grafana-server
  • 1
  • 2
  • 3

在这里插入图片描述

配置Grafana服务器以在启动时启动:

# systemctl enable grafana-server
原文地址:https://www.cnblogs.com/netian/p/14183151.html