普罗米修斯是一个开源的监控、查询和警报工具。最初由Soundcloud于2012年构建,功能丰富的工具已被多家公司采用,用于监控其IT基础架构并确保所有系统平稳运行。Prometheus 允许您通过 HTTP 协议查询和提取时间序列指标,例如 CPU 和内存利用率,并在实时图表上可视化它们。您还可以将 Prometheus 配置为在节点或服务停机时推送警报,并将其与其他第三方监控工具(如 Grafana)集成,以增强数据可视化。在本指南中,我们将看看 Prometheus 在 CentOS 8 /RHEL 8 系统上的安装。
步骤1)创建普罗米修斯用户和组
首先,我们将为普罗米修斯创建一个系统用户。执行以下命令来实现此目的。
[root@prometheus ~]# useradd -m -s /bin/false prometheus
[root@prometheus ~]# id prometheus
uid=1002(prometheus) gid=1002(prometheus) groups=1002(prometheus)
[root@prometheus ~]#
您可能已经注意到,系统用户没有 /bin/false 选项中指定的登录权限
步骤2)为普罗米修斯创建配置目录
创建 Prometheus 的用户后,我们将在 /etc 和 /var 目录中创建配置目录,这些目录将存储 Prometheus 配置文件和数据。因此,请运行以下命令:
[root@prometheus ~]# mkdir /etc/prometheus
[root@prometheus ~]# mkdir /var/lib/prometheus
在 /var/lib/prometheus 上设置所有权
[root@prometheus ~]# chown prometheus /var/lib/prometheus/
步骤3)下载普罗米修斯焦油文件
目录到位后,我们现在可以下载普罗米修斯了。要获取最新版本,请转到下载页面以获取适用于您的环境的最新版本。在撰写本文时,最新版本是v 2.14.0。或者,只需运行以下命令
[root@prometheus ~]# dnf install wget -y
[root@prometheus ~]# wget https://github.com/prometheus/prometheus/releases/download/v2.14.0/prometheus-2.14.0.linux-amd64.tar.gz -P /tmp
下载完成后,解压缩压缩包文件,如下所示
[root@prometheus tmp]# tar -zxpvf prometheus-2.14.0.linux-amd64.tar.gz
这将给你留下一个名为prometheus-2.14.0.linux-amd64的目录。
使用树命令查看目录结构,
提取的目录包含 2 个二进制文件 prometheus & promtool,我们需要将其复制到 /usr/local/bin 路径。
因此,导航到提取的目录并使用以下命令复制它们:
[root@prometheus ~]# cd /tmp/prometheus-2.14.0.linux-amd64
[root@prometheus prometheus-2.14.0.linux-amd64]# cp prometheus /usr/local/bin
对另一个二进制文件执行类似操作
[root@prometheus prometheus-2.14.0.linux-amd64]# cp promtool /usr/local/bin
步骤4)为普罗米修斯创建配置文件
要从配置开始,请创建一个文件 /etc/prometheus/prometheus.yml 并将配置粘贴到文件中
[root@prometheus ~]# 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']
这只会监控您的本地系统(普罗米修斯服务器)。
接下来,按如下方式调整防火墙,以允许通过端口 9090 与服务器进行外部连接
[root@prometheus ~]# firewall-cmd --add-port=9090/tcp --permanent
success
[root@prometheus ~]# firewall-cmd --reload
success
[root@prometheus ~]#
步骤 5) 为 Prometheus Server 创建 Systemd 服务文件
为了使用 systemd 将普罗米修斯作为服务进行管理,我们需要为它创建一个系统文件。因此,如图所示创建文件并粘贴内容,
[root@prometheus ~]# 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,
[root@prometheus ~]# systemctl daemon-reload
现在启动并启用普罗米修斯在启动时运行
[root@prometheus ~]# systemctl start prometheus
[root@prometheus ~]# systemctl enable prometheus
为了确保普罗米修斯正在运行,请运行以下命令:
[root@prometheus ~]# systemctl status prometheus
从显示的输出中,我们可以清楚地看到普罗米修斯按预期运行,没有错误。此外,您可以使用 netstat 实用程序检查服务是否正在侦听端口 9090。
[root@prometheus ~]# netstat -tunlp
真棒!普罗米修斯按预期在端口 9090 上运行。现在前往您的浏览器浏览服务器的IP,如图所示
http://server-ip:9090
单击“状态”选项卡,然后单击“目标”
您的系统将显示如下
步骤6)安装和配置node_exporter
节点导出器是一个实用程序,用于收集和交付大量 Linux 系统指标,例如 CPU、内存使用情况、文件系统和网络统计信息。在本节中,我们将在 Prometheus 服务器和远程 CentOS 8 Linux 主机上安装 node_exporter,并监控两台主机上的系统指标。
在普罗米修斯节点上,我们将为node_exporter创建一个系统用户。
[root@prometheus ~]# useradd -m -s /bin/false node_exporter
接下来,前往普罗米修斯的下载页面并下载node_exporter压缩包或使用以下 wget 命令从命令行下载它,
[root@prometheus ~]# wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz
下载node_exporter文件后,继续并如图所示将其解压缩
[root@prometheus ~]# tar -zxpvf node_exporter-0.18.1.linux-amd64.tar.gz
您可以使用 tree 命令检查提取的文件夹的内容,如下所示
[root@prometheus ~]# tree node_exporter-0.18.1.linux-amd64
接下来,将名为 node_exporter 的二进制文件复制到 /usr/local/bin 路径
[root@prometheus ~]# cp node_exporter-0.18.1.linux-amd64/node_exporter /usr/local/bin
接下来,设置已复制node_exporter文件的文件权限,如图所示
[root@prometheus ~]# chown node_exporter:node_exporter /usr/local/bin/node_exporter
接下来,我们需要将node_exporter配置为作为服务运行。因此,继续并创建一个 systemd 服务文件,如下所示
[root@prometheus ~]# 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 管理器:
[root@prometheus ~]# systemctl daemon-reload
接下来,启动并启用node_exporter服务
[root@prometheus ~]# systemctl start node_exporter
[root@prometheus ~]# systemctl enable node_exporter
为了确保服务正在运行,请执行:
[root@prometheus ~]# systemctl status node_exporter
为了确保服务正在运行,请使用 netstat 实用程序检查它是否默认正在侦听端口 9100。
[root@prometheus ~]# netstat -pnltu | grep 9100
tcp6 0 0 :::9100 :::* LISTEN 3472/node_exporter
[root@prometheus ~]#
完善!Node_exporter服务按预期运行。
接下来,在防火墙中打开端口 9100,如下所示
[root@prometheus ~]# firewall-cmd --add-port=9100/tcp --permanent
success
[root@prometheus ~]# firewall-cmd --reload
success
[root@prometheus ~]#
同时重复为远程 CentOS 8 Linux 系统概述的步骤。
最后,您需要将node_exporter目标添加到 prometheus.yml 文件中。附加以下行以定义普罗米修斯服务器的node_exporter
[root@prometheus ~]# vi /etc/prometheus/prometheus.yml
---------
- job_name: 'node_exporter'
static_configs:
- targets: ['localhost:9100']
重新启动普罗米修斯服务
[root@prometheus ~]# systemctl restart prometheus
再次前往浏览器,单击“状态”选项卡,然后单击“目标”
请务必在浏览器上为普罗米修斯服务器观察一个名为 node_exporter 的新端点
要为远程 Linux 系统添加端点,请返回 prometheus.yml 文件并附加以下行
– 目标:['192.168.10.90:9100']
node_exporter部分现在应如下所示
- job_name: 'node_exporter'
static_configs:
- targets: ['localhost:9100']
- targets: ['192.168.10.90:9100']
保存更改并重新启动 Prometheus 服务
[root@prometheus ~]# systemctl restart prometheus
刷新浏览器,注意为远程 CentOS Linux 系统添加的第二个端点
要确保您从配置的节点接收指标。只需使用 curl 命令,如下所示:
# 卷曲 http://node-ip:9100/metrics
例如,要显示来自 Prometheus 服务器运行的指标,请执行以下操作:
[root@prometheus ~]# curl http://localhost:9100/metrics
对于远程 CentOS 8 主机,我执行了以下命令:
[root@prometheus ~]# curl http://192.168.10.90:9100/metrics
这也可以通过打开浏览器并浏览 URL 来实现
http://192.168.10.90:9100/metrics
您还可以选择绘制所需的指标图表。只需转到 Prometheus 服务器的主页,然后单击标有“在光标处插入指标”的下拉菜单。
选择要绘制图表的指标,
Click on the ‘Execute’ button and click on the ‘graph’ tab just below to reveal the graph
这就把我们带到了这个话题的结尾。您已成功安装并配置 Prometheus 以监控服务器和远程主机上的系统指标。在下一指南中,我们将 Prometheus 与 Grafana 集成,以便更好地可视化和分析指标。请随时与我们分享您的反馈,并与朋友分享文章。