如何在CentOS 8上安装Prometheus
Prometheus是一个开源的监视,查询和警报工具。该功能丰富的工具最初由Soundcloud于2012年构建,已被多家公司采用,以监视其IT基础架构并确保所有系统运行顺畅。Prometheus允许您查询和提取时间序列指标,例如通过HTTP协议的 CPU和内存利用率,并在实时图形上将其可视化。您还可以将Prometheus配置为在节点或服务停机时发出警报,并将其与其他第三方监视工具(例如Grafana)集成以增强数据可视化。在本指南中,我们将介绍在CentOS 8系统上Prometheus的安装。
步骤1)创建一个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] ~]#
您可能已经注意到,系统用户没有/ bin / false选项中指定的登录权限。
步骤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.19.2。或者,只需运行以下命令
[[email protected] ~]# dnf install wget -y
[[email protected] ~]# wget https://github.com/prometheus/prometheus/releases/download/v2.19.2/prometheus-2.19.2.linux-amd64.tar.gz -P /tmp
下载完成后,解压缩tarball文件,如下所示
[[email protected] tmp]# tar -zxpvf prometheus-2.19.2.linux-amd64.tar.gz
这将为您提供一个名为prometheus-2.19.2.linux-amd64的目录
使用tree命令查看目录结构,
提取的目录包含2个二进制文件 prometheus和promtool,我们需要将其复制到/ usr / local / bin路径。
因此,导航到提取的目录并使用以下命令将其复制:
[[email protected] ~]# cd /tmp/prometheus-2.19.2.linux-amd64
[[email protected] prometheus-2.19.2.linux-amd64]# cp prometheus /usr/local/bin
对其他二进制文件也一样
[[email protected] prometheus-2.19.2.linux-amd64]# cp promtool /usr/local/bin
我们需要将prometheus.yml复制到/etc/prometheus/路径。
[[email protected] prometheus-2.19.2.linux-amd64]# cp prometheus.yml /etc/prometheus/
步骤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,如图所示
http://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/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz
下载node_exporter文件后,继续进行提取,如下所示
[[email protected] ~]# tar -zxpvf node_exporter-1.0.1.linux-amd64.tar.gz
您可以使用tree命令检查提取的文件夹的内容,如下所示
[[email protected] ~]# tree node_exporter-1.0.1.linux-amd64
接下来,将名为node_exporter的二进制文件复制到/ usr / local / bin路径
[[email protected] ~]# cp node_exporter-1.0.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 Manager:
[[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 3406/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
再次转到浏览器,单击“ 状态 ”标签,然后单击“ 目标 ”
确保 在Prometheus服务器的浏览器上观察到名为node_exporter的新端点
要为远程Linux系统添加端点,请回到prometheus.yml文件并在下面添加以下行
–目标:['192.168.174.195:9100']
现在,node_exporter部分应如下所示
- job_name: 'node_exporter'
static_configs:
- targets: ['localhost:9100']
- targets: ['192.168.174.195:9100']
保存更改并重新启动Prometheus服务
[[email protected] ~]# systemctl restart prometheus
刷新浏览器并注意为远程CentOS Linux系统添加的第二个端点
为确保您从配置的节点接收指标。只需使用curl命令,如下所示:
# curl http://node-ip:9100/metrics
例如,要显示来自Prometheus服务器的指标,请运行:
[[email protected] ~]# curl http://localhost:9100/metrics
对于远程CentOS 8主机,我执行了以下命令:
[[email protected] ~]# curl http://192.168.174.195:9100/metrics
也可以通过打开浏览器并浏览URL来实现
http://192.168.174.195:9100/metrics
您还可以选择以图形方式绘制所需的指标。只需转到Prometheus服务器的主页,然后单击标记为“ 在光标处插入度量 ”的下拉菜单。
选择要绘制图形的指标,
单击“ 执行 ”按钮,然后单击下面的“ 图形 ”选项卡以显示图形
这使我们到了本主题的结尾。您已成功安装并配置了Prometheus,以监视服务器和远程主机上的系统指标。在下一个指南中,我们将Prometheus与Grafana集成在一起,以更好地可视化和分析指标。