在 CentOS 7 上安装 Node_Exporter

Prometheus 从许多来源接收指标,如机器、数据库、应用程序等。

为此,Prometheus 需要在每个节点上安装一个代理(称为导出器),我们要为其收集指标。

Prometheus 网站上提供了不同类型的导出器。
我们可以根据需要下载并使用一个。
但是我们更喜欢在每个节点上安装以收集机器指标的最常见导出器是 node_exporter 。

在本节中,我们将在同一个基于 CentOS 7 的 Prometheus 网络监控服务器上安装 node_exporter。

从它的官方下载页面下载 Node_Exporter。

[root@prometheus-01 ~]# cd /tmp
[root@prometheus-01 tmp]# wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz

提取下载的文件如下。

[root@prometheus-01 tmp]# tar xvf node_exporter-0.18.1.linux-amd64.tar.gz
node_exporter-0.18.1.linux-amd64/
node_exporter-0.18.1.linux-amd64/node_exporter
node_exporter-0.18.1.linux-amd64/NOTICE
node_exporter-0.18.1.linux-amd64/LICENSE

为 node_exporter 软件创建一个目录。

[root@prometheus-01 tmp]# mkdir -p /var/lib/prometheus/node_exporter

将提取的文件移动到 /var/lib/prometheus/node_exporter 目录。

[root@prometheus-01 tmp]# mv node_exporter-0.18.1.linux-amd64/* /var/lib/prometheus/node_exporter
[root@prometheus-01 tmp]# cd

更改 node_exporter 目录的所有权。

[root@prometheus-01 ~]# chown -R prometheus:prometheus /var/lib/prometheus/node_exporter/

为 node_exporter 创建 systemd 服务,以便在 Linux 服务器启动时自动启动。

[root@prometheus-01 ~]# vi /usr/lib/systemd/system/node_exporter.service

在此文件中添加以下代码行。

[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
ExecStart=/var/lib/prometheus/node_exporter/node_exporter
[Install]
WantedBy=default.target

启用并启动 node_exporter 服务。

[root@prometheus-01 ~]# systemctl enable --now node_exporter.service
Created symlink from /etc/systemd/system/default.target.wants/node_exporter.service to /usr/lib/systemd/system/node_exporter.service.

节点导出器使用默认服务端口 9100/tcp 。
因此,我们需要在 Linux 防火墙中允许该服务端口。

[root@prometheus-01 ~]# firewall-cmd --permanent --add-port=9100/tcp
success
[root@prometheus-01 ~]# firewall-cmd --reload
success

CentOS 7 上 Prometheus 的前置配置

作为最佳实践,我们正在创建一个 Linux 用户来拥有 Prometheus 软件和进程。

[root@prometheus-01 ~]# useradd --no-create-home -s /bin/false prometheus

为 Prometheus 软件创建所需的目录。

[root@prometheus-01 ~]# mkdir /etc/prometheus
[root@prometheus-01 ~]# mkdir /var/lib/prometheus

将这些目录的所有者和组更改为 Prometheus。

[root@prometheus-01 ~]# chown prometheus:prometheus /etc/prometheus
[root@prometheus-01 ~]# chown prometheus:prometheus /var/lib/prometheus

什么是Prometheus?

Prometheus 是一个免费的开源软件应用程序,用于事件监控和警报。

它在时间序列数据库中收集和记录实时指标,并根据自定义阈值提醒用户。
Prometheus 是用 Go 编程语言编写的,并在 Apache License 2.0 下分发。

Prometheus 需要在目标节点上安装多个导出器。
导出器在目标节点的相关端口上生成和发布指标。
Prometheus 监控服务器将提取这些指标并存储在时间序列数据库中。

在本教程中,我们将在 CentOS 7 上安装 Prometheus 网络监控服务器以及 node_exporter 。

在 CentOS 7 上安装 Prometheus 网络监控服务器

Prometheus 是一个免费的开源网络监控工具。
在本文中,我们将学习如何在 CentOS 7 上安装 Prometheus 网络监控服务器。

之路 on it Road.com

在 CentOS 7 上安装 Prometheus 网络监控服务器

我们可以从他们的官方网站或者它的 GitHub 存储库下载 Prometheus 网络监控软件。

在这里,我们在 wget Linux 命令的帮助下下载最新版本的 Prometheus。

[root@prometheus-01 ~]# cd /tmp
[root@prometheus-01 tmp]# wget https://github.com/prometheus/prometheus/releases/download/v2.13.1/prometheus-2.13.1.linux-amd64.tar.gz

使用 tar Linux 命令解压缩下载的 zip 文件。

[root@prometheus-01 tmp]# tar xvzf prometheus-2.13.1.linux-amd64.tar.gz

将 Prometheus 文件移动到 /var/lib/prometheus 目录。

[root@prometheus-01 tmp]# mv prometheus-2.13.1.linux-amd64/* /var/lib/prometheus/

调整 /var/lib/prometheus 目录的所有者和组。

[root@prometheus-01 tmp]# chown -R prometheus:prometheus /var/lib/prometheus

Prometheus zip 文件还包含一个具有默认设置的配置文件。
我们可以使用这个文件来配置我们的 Prometheus 服务器。

借助 mv Linux 命令,将 Prometheus 配置文件移动到 /etc/prometheus 目录。

[root@prometheus-01 tmp]# cd
[root@prometheus-01 ~]# mv /var/lib/prometheus/prometheus.yml /etc/prometheus/

创建 prometheus 可执行文件的软链接,以便我们可以从 CLI 执行 prometheus 命令。

[root@prometheus-01 ~]# ln -s /var/lib/prometheus/prometheus /usr/local/bin/prometheus

为了在 Linux 服务器启动时设置 Prometheus 的自动启动,我们需要为 Prometheus 网络监控工具创建一个 Systemd 服务,如下所示。

[root@prometheus-01 ~]# vi /usr/lib/systemd/system/prometheus.service

并在该文件中添加以下指令。

[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
--config.file /etc/prometheus/prometheus.yml \
--storage.tsdb.path /var/lib/prometheus/ \
--web.console.templates=/var/lib/prometheus/consoles \
--web.console.libraries=/var/lib/prometheus/console_libraries
[Install]
WantedBy=multi-user.target

启用并启动 Prometheus 网络服务。

[root@prometheus-01 ~]# systemctl enable --now prometheus.service
Created symlink from /etc/systemd/system/multi-user.target.wants/prometheus.service to /usr/lib/systemd/system/prometheus.service.

Prometheus 使用默认服务端口 9090/tcp 。
我们需要在 Linux 防火墙中允许这个端口,这样网络流量才能到达 Prometheus 服务。

[root@prometheus-01 ~]# firewall-cmd --permanent --add-port=9090/tcp
success
[root@prometheus-01 ~]# firewall-cmd --reload
success

使用客户端浏览器打开 URL http://prometheus-01.onitroad.com。

我们现在在表达式浏览器网页。
在这里,我们可以搜索 Prometheus 正在收集数据的指标。

要查看数据图表,请单击 Graph 。

在 Prometheus 服务器中添加一个 Node_Exporter 目标

我们已经成功安装了 node_exporter,现在我们必须将其添加为 Prometheus 服务器中的目标。

编辑 Prometheus 配置文件。

[root@prometheus-01 ~]# vi /etc/prometheus/prometheus.yml

并在文件末尾添加以下指令。

- job_name: 'node_exporter'
    static_configs:
    - targets: ['localhost:9100']

重新启动 Prometheus 服务以使更改生效。

[root@prometheus-01 ~]# systemctl restart prometheus.service

浏览 URL http://prometheus-01.onitroad.com:9090/ 并在那里搜索一些节点指标。

我们在这里搜索 node_memory_MemAvailable_bytes 指标。

要查看该指标的图表,请单击图表按钮。

由于,我们最近将此节点添加到 Prometheus Server,因此,目前可用的时间序列数据并不多。
因此,我们将时间调整为 1m 以完全填充图形。

日期:2020-09-17 00:16:45 来源:oir作者:oir