如何在 CentOS 8 / RHEL 8 上安装 Prometheus

[ad_1]

普罗米修斯 是一个开源的监控、查询和警报工具。 最初由 Soundcloud 于 2012 年构建,该功能丰富的工具已被多家公司用于监控其 IT 基础架构并确保所有系统顺利运行。 Prometheus 允许您查询和提取时间序列指标,例如 CPU 和内存利用率 HTTP协议 并在实时图表上可视化它们。 您还可以将 Prometheus 配置为在节点或服务停机时推送警报,并将其与其他第三方监控工具(例如 格拉法纳 用于增强数据可视化。 在本指南中,我们将介绍在 CentOS 8 /RHEL 8 系统上安装 Prometheus。

Step1) 创建 Prometheus 用户和组

首先,我们将为 Prometheus 创建一个系统用户。 执行以下命令以实现此目的。

[[email protected] ~]# useradd -m -s /bin/false prometheus
[[email protected] ~]# id prometheus
uid=1002(prometheus) gid=1002(prometheus) groups=1002(prometheus)
[[email protected] ~]#

您可能已经注意到,系统用户没有指定的登录权限 /斌/假 选项

步骤 2) 为 Prometheus 创建配置目录

创建 Prometheus 用户后,我们将在 /etc 和 /var 目录中创建配置目录,这些目录将存储 Prometheus 配置文件和数据。 所以运行下面的命令:

[[email protected] ~]# mkdir /etc/prometheus
[[email protected] ~]# mkdir /var/lib/prometheus

在 /var/lib/prometheus 上设置所有权

[[email protected] ~]# chown prometheus /var/lib/prometheus/

步骤 3) 下载 Prometheus tar 文件

目录到位后,我们现在可以下载 Prometheus。 要获取最新版本,请前往 下载页面 获取适合您环境的最新版本。 在撰写本文时,最新版本是 v 2.14.0. 或者,只需运行以下命令

[[email protected] ~]# dnf install wget -y
[[email protected] ~]# wget https://github.com/prometheus/prometheus/releases/download/v2.14.0/prometheus-2.14.0.linux-amd64.tar.gz -P /tmp

下载完成后,解压缩 tarball 文件,如图所示

[[email protected] tmp]# tar -zxpvf prometheus-2.14.0.linux-amd64.tar.gz

这将为您留下一个名为 prometheus-2.14.0.linux-amd64 的目录

使用tree命令查看目录结构,

解压后的目录包含 2 个二进制文件 普罗米修斯 & 促销工具 并且我们需要复制到 /usr/本地/bin 小路。

因此,导航到提取的目录并使用以下命令复制它们:

[[email protected] ~]# cd /tmp/prometheus-2.14.0.linux-amd64
[[email protected] prometheus-2.14.0.linux-amd64]# cp prometheus  /usr/local/bin

对另一个二进制文件执行同样的操作

[[email protected] prometheus-2.14.0.linux-amd64]# cp promtool  /usr/local/bin

步骤 4)为 Prometheus 创建配置文件

要从配置开始,请创建一个文件 /etc/prometheus/prometheus.yml 并将配置粘贴到文件中

[[email protected] ~]# vi /etc/prometheus/prometheus.yml
# 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: 15s  # scrape_timeout is set to the global default (10s).
# 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']

这只会监视您的本地系统(Prometheus Server)。

接下来调整防火墙如下,允许外部通过9090端口连接服务器

[[email protected] ~]# firewall-cmd --add-port=9090/tcp --permanent
success
[[email protected] ~]# firewall-cmd --reload
success
[[email protected] ~]#

步骤 5) 为 Prometheus Server 创建 Systemd 服务文件

为了使用 systemd 将 Prometheus 作为服务进行管理,我们需要为其创建一个系统文件。 因此,创建如图所示的文件并粘贴内容,

[[email protected] ~]# vi /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus Time Series Collection and Processing Server
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=/etc/prometheus/consoles 
    --web.console.libraries=/etc/prometheus/console_libraries

[Install]
WantedBy=multi-user.target

要使更改生效,请重新加载 systemctl,

[[email protected] ~]# systemctl daemon-reload

现在启动并启用 Prometheus 以在启动时运行

[[email protected] ~]# systemctl start prometheus
[[email protected] ~]# systemctl enable prometheus

为了确定 Prometheus 正在运行,请运行以下命令:

[[email protected] ~]# systemctl status prometheus

从显示的输出中,我们可以清楚地看到 Prometheus 正在按预期运行,没有错误。 此外,您可以使用 netstat 实用程序检查服务是否正在侦听端口 9090。

[[email protected] ~]# netstat -tunlp

那太棒了! Prometheus 正按预期在端口 9090 上运行。 现在前往您的浏览器,浏览您服务器的 IP,如图所示

https://server-ip:9090

单击“地位‘ 选项卡,然后单击 ‘目标

您的系统将如图所示

步骤 6) 安装和配置 node_exporter

节点导出器 是一个收集和发送大量 Linux 系统指标的实用程序,例如 CPU、内存使用、文件系统和网络统计信息。 在本节中,我们将在 Prometheus 服务器和远程 CentOS 8 Linux 主机上安装 node_exporter,并在两台主机上监控系统指标。

在 Prometheus 节点上,我们将为 node_exporter 创建一个系统用户。

[[email protected] ~]# useradd -m -s /bin/false node_exporter

接下来,前往 Prometheus 的下载页面并下载 node_exporter tarball 或使用下面的 wget 命令从命令行下载它,

[[email protected] ~]# wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz

下载 node_exporter 文件后,继续并解压缩,如图所示

[[email protected] ~]# tar -zxpvf node_exporter-0.18.1.linux-amd64.tar.gz

您可以使用 tree 命令检查提取文件夹的内容,如图所示

[[email protected] ~]# tree node_exporter-0.18.1.linux-amd64

接下来,复制名为的二进制文件 node_exporter/usr/本地/bin 小路

[[email protected] ~]# cp node_exporter-0.18.1.linux-amd64/node_exporter /usr/local/bin

接下来,设置已经复制的node_exporter文件的文件权限,如图

[[email protected] ~]# chown node_exporter:node_exporter /usr/local/bin/node_exporter

接下来,我们需要配置 node_exporter 作为服务运行。 因此,继续并创建一个 systemd 服务文件,如图所示

[[email protected] ~]# vi /etc/systemd/system/node_exporter.service

然后粘贴如下所示的配置并保存文件

[Unit]
Description=Prometheus Node Exporter
Wants=network-online.target
After=network-online.target

[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter

[Install]
WantedBy=multi-user.target

要使更改生效,请使用以下命令重新加载 systemd 管理器:

[[email protected] ~]# systemctl daemon-reload

接下来,启动并启用 node_exporter 服务

[[email protected] ~]# systemctl start node_exporter
[[email protected] ~]# systemctl enable node_exporter

为了确保服务正在运行,请执行:

[[email protected] ~]# systemctl status node_exporter

为了确保服务正在运行,请使用 netstat 实用程序检查它是否在默认情况下侦听端口 9100。

[[email protected] ~]# netstat -pnltu | grep 9100
tcp6       0      0 :::9100       :::*          LISTEN      3472/node_exporter
[[email protected] ~]#

完美的! Node_exporter 服务按预期运行。

接下来,如图所示在防火墙中打开9100端口

[[email protected] ~]# firewall-cmd --add-port=9100/tcp  --permanent
success
[[email protected] ~]# firewall-cmd --reload
success
[[email protected] ~]#

重复列出的步骤 远程 CentOS 8 Linux 系统 以及。

最后,您需要将 node_exporter 目标添加到 prometheus.yml 文件中。 附加以下几行以定义 Prometheus 服务器的 node_exporter

[[email protected] ~]# vi /etc/prometheus/prometheus.yml
---------
 - job_name: 'node_exporter'
   static_configs:
   - targets: ['localhost:9100']

重启 Prometheus 服务

[[email protected] ~]# systemctl restart prometheus

再次前往您的浏览器,单击“地位‘ 标签并点击 ‘目标

一定要在浏览器上观察一个名为的新端点 node_exporter 用于普罗米修斯服务器

添加端点 远程Linux系统 回到 普罗米修斯.yml 文件并附加下面的行

– 目标: [‘192.168.10.90:9100’]

node_exporter 部分现在应如下所示

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

Save 更改并重新启动 Prometheus 服务

[[email protected] ~]# systemctl restart prometheus

刷新浏览器并注意已为远程添加的第二个端点 CentOS Linux 系统

确保您从配置的节点接收指标。 只需使用 curl 命令如下:

# curl https://node-ip:9100/metrics

例如,要显示来自 Prometheus 服务器的指标,请运行:

[[email protected] ~]# curl https://localhost:9100/metrics

对于远程 CentOS 8 主机,我执行了命令:

[[email protected] ~]# curl https://192.168.10.90:9100/metrics

这也可以通过打开浏览器浏览网址来实现

https://192.168.10.90:9100/metrics

您还可以选择绘制所需的指标图表。 只需转到 Prometheus 服务器的主页,然后单击标有“在光标处插入指标‘。

选择要绘制图表的指标,

单击“执行‘ 按钮并点击 ‘图形‘ 标签就在下方以显示图表

这使我们结束了这个话题。 您已成功安装并配置 Prometheus 以监控服务器和远程主机上的系统指标。 在我们的下一个指南中,我们将把 Prometheus 与 Grafana 集成,以更好地可视化和分析指标。 随时与我们分享您的反馈,并与朋友分享文章。

还阅读 如何将 Grafana 与 Prometheus 集成以进行监控

[ad_2]

Related Posts