在Rocky Linux 9/AlmaLinux 9上安装Prometheus

本文介绍在Rocky Linux 9/AlmaLinux 9系统上安装带有Node Exporter的Prometheus。为了让Prometheus收集度量,您需要安装“Exporter”应用程序来公开数据和度量。Node Exporter用于公开Linux系统上的硬件和内核相关度量。

具体操作方法

1、添加Prometheus存储库

默认的Rocky Linux 9/AlmaLinux 9存储库中不提供Prometheus。需要将存储库添加到系统中才能安装Prometheus。

首先启用EPEL存储库:

sudo dnf -y install epel-release vim

创建存储库:

sudo vim /etc/yum.repos.d/prometheus.repo

将以下行添加到文件中:

[prometheus]

name=prometheus

baseurl=https://packagecloud.io/prometheus-rpm/release/el/8/$basearch

repo_gpgcheck=1

gpgcheck=0

enabled=1

gpgkey=https://packagecloud.io/prometheus-rpm/release/gpgkey

sslverify=1

sslcacert=/etc/pki/tls/certs/ca-bundle.crt

metadata_expire=300

[prometheus-source]

name=prometheus-rpm_release-source

baseurl=https://packagecloud.io/prometheus-rpm/release/el/8/SRPMS

repo_gpgcheck=1

gpgcheck=0

enabled=1

gpgkey=https://packagecloud.io/prometheus-rpm/release/gpgkey

sslverify=1

sslcacert=/etc/pki/tls/certs/ca-bundle.crt

metadata_expire=300

2、安装和配置Prometheus服务器

添加存储库后,可以使用以下命令安装Prometheus:

sudo dnf install prometheus -y

会返回安装的相关信息,如prometheus2 x86_64 2.37.0-1.el8 prometheus-rpm_release 35M等,到时按y回车。

安装后,Prometheus将其数据存储在/var/lib/prometheus,配置文件存储在/etc/prometheus/。

默认配置文件位于/etc/prometheus/prometheus.yml:

sudo vim /etc/prometheus/prometheus.yml

它可以根据需要进行定制:

# my 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 is set to the global default (10s).

.........

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: ["192.168.205.13:9090"]

确保文件符合普罗米修斯的初始设置。使用以下命令保存、启动并启用服务:

sudo systemctl start prometheus

sudo systemctl enable prometheus

检查服务状态:

$ systemctl status prometheus.service

允许端口通过防火墙:

sudo firewall-cmd --add-port=9090/tcp --permanent

sudo firewall-cmd --reload

此时,您可以使用URL访问Prometheus,地址格式为http://IP_Address:9090,如下:

在Rocky Linux 9/AlmaLinux 9上安装Prometheus

3、安装和配置Node Exporter

您有两个安装Node Exporter的选项。

选项1:从RPM存储库安装(推荐)

如步骤1所述添加Prometheus存储库,然后继续从中安装软件包:

sudo dnf -y install node_exporter

安装后启动并启用服务:

sudo systemctl enable --now node_exporter

选项2:手动下载并安装

安装curl和wget包:

sudo dnf -y install wget tar curl vim

可以从Github发布页面下载Node Exporter,地址在https://github.com/prometheus/node_exporter/releases。您还可以使用wget提取归档文件,如下所示:

##For AMD64

VERSION=$(curl -s https://api.github.com/repos/prometheus/node_exporter/releases/latest|grep tag_name|cut -d '"' -f 4|sed 's/v//')

wget https://github.com/prometheus/node_exporter/releases/download/v$VERSION/node_exporter-$VERSION.linux-amd64.tar.gz

##For ARM64

VERSION=$(curl -s https://api.github.com/repos/prometheus/node_exporter/releases/latest|grep tag_name|cut -d '"' -f 4|sed 's/v//')

wget https://github.com/prometheus/node_exporter/releases/download/v$VERSION/node_exporter-$VERSION.linux-arm64.tar.gz

##For i386

VERSION=$(curl -s https://api.github.com/repos/prometheus/node_exporter/releases/latest|grep tag_name|cut -d '"' -f 4|sed 's/v//')

wget https://github.com/prometheus/node_exporter/releases/download/v$VERSION/node_exporter-$VERSION.linux-386.tar.gz

下载后,提取存档:

tar -xf node_exporter-$VERSION.linux-*.tar.gz

将二进制文件移动到PATH:

sudo mv node_exporter-*/node_exporter /usr/local/bin

要将Node Exporter作为服务运行,请创建一个专用用户:

sudo adduser -M -r -s /sbin/nologin prometheus

创建systemd服务文件:

sudo vim /etc/systemd/system/node_exporter.service

将以下行添加到文件中:

[Unit]

Description=Node Exporter

After=network.target

[Service]

User=prometheus

Group=prometheus

Type=simple

ExecStart=/usr/local/bin/node_exporter

ExecReload=/bin/kill -HUP $MAINPID

Restart=on-failure

RestartSec=5s

[Install]

WantedBy=multi-user.target

保存文件并配置SELinux以访问路径:

sudo /sbin/restorecon -v /usr/local/bin/node_exporter

并重新加载系统守护程序:

sudo systemctl daemon-reload

启动并启用Node Exporter:

sudo systemctl enable --now node_exporter

验证服务是否正在运行:

$ systemctl status node_exporter

检查服务正在侦听的端口:

$ sudo ss -aplnt | grep node

LISTEN 0      4096               *:9100            *:*    users:(("node_exporter",pid=2401,fd=3))

允许此端口通过防火墙:

sudo firewall-cmd --add-port=9100/tcp --permanent

sudo firewall-cmd --reload

现在将Prometheus配置为使用Node Exporter:

sudo vim /etc/prometheus/prometheus.yml

在scrape_config部分下,为Node Exporter添加新作业:

  - job_name: 'node_exporter_metrics'

    scrape_interval: 5s

    static_configs:

      - targets: ['SERVER-IP:9100']

请记住适当更换服务器IP。保存文件并重新启动Prometheus服务:

sudo systemctl restart prometheus

4、安装和配置Blackbox导出器

Blackbox导出器支持通过HTTP、HTTPS、DNS、TCP和ICMP探测端点。您可以从YUM存储库或手动安装。

选项1:从Prometheus存储库安装Blackbox导出器

如步骤1所示添加存储库并安装软件包:

sudo dnf -y install blackbox_exporter

Blackbox导出器的设置位于/etc/prometheus/blackbox.yml文件中。您可以修改或仅启动并启用该服务:

sudo systemctl enable --now blackbox_exporter

在防火墙中启用所需端口:

sudo firewall-cmd --add-port=9115/tcp --permanent

sudo firewall-cmd --reload

选项2:手动安装Blackbox导出器

可以从Github发布页面下载导出程序,地址在https://github.com/prometheus/blackbox_exporter/releases。但首先安装curl和wget包:

sudo dnf -y install wget tar curl vim

您还可以按如下所示拉动文件:

##For AMD64

VERSION=$(curl -s https://api.github.com/repos/prometheus/blackbox_exporter/releases/latest|grep tag_name|cut -d '"' -f 4|sed 's/v//')

wget https://github.com/prometheus/blackbox_exporter/releases/download/v$VERSION/blackbox_exporter-$VERSION.linux-amd64.tar.gz

##For ARM64

VERSION=$(curl -s https://api.github.com/repos/prometheus/blackbox_exporter/releases/latest|grep tag_name|cut -d '"' -f 4|sed 's/v//')

wget https://github.com/prometheus/blackbox_exporter/releases/download/v$VERSION/blackbox_exporter-$VERSION.linux-arm64.tar.gz

##For i386

VERSION=$(curl -s https://api.github.com/repos/prometheus/blackbox_exporter/releases/latest|grep tag_name|cut -d '"' -f 4|sed 's/v//')

wget https://github.com/prometheus/blackbox_exporter/releases/download/v$VERSION/blackbox_exporter-$VERSION.linux-386.tar.gz

下载后,提取存档:

tar -xf blackbox_exporter-$VERSION.linux-*.tar.gz

现在将二进制文件移动到PATH:

sudo mv blackbox_exporter-*/blackbox_exporter /usr/local/bin

为Blackbox导出器创建系统用户:

sudo adduser -M -r -s /sbin/nologin blackbox_exporter

创建具有正确所有权的配置文件:

sudo mkdir /etc/blackbox_exporter

sudo mv  blackbox_exporter-*/blackbox.yml /etc/blackbox_exporter

sudo chown blackbox_exporter:blackbox_exporter /etc/blackbox_exporter/blackbox.yml

为Blackbox导出器创建systemd服务文件:

sudo vim /etc/systemd/system/blackbox_exporter.service

添加以下行:

[Unit]

Description=Blackbox Exporter

After=network.target

[Service]

User=blackbox_exporter

Group=blackbox_exporter

Type=simple

ExecStart=/usr/local/bin/blackbox_exporter --config.file /etc/blackbox_exporter/blackbox.yml

[Install]

WantedBy=multi-user.target

保存文件并配置SELinux以访问路径:

sudo /sbin/restorecon -v /usr/local/bin/blackbox_exporter

重新加载系统守护程序:

sudo systemctl daemon-reload

启动并启用服务:

sudo systemctl enable --now blackbox_exporter

验证服务是否按要求运行:

$ systemctl status blackbox_exporter

验证Blackbox导出器端口:

$ sudo ss -aplnt | grep black

LISTEN 0      4096               *:9115            *:*    users:(("blackbox_export",pid=2752,fd=3))

允许此端口通过防火墙:

sudo firewall-cmd --add-port=9115/tcp --permanent

sudo firewall-cmd --reload

下面为Blackbox导出器配置Prometheus:

sudo vim /etc/prometheus/prometheus.yml

Blackbox导出器支持通过HTTP、HTTPS、DNS、TCP和ICMP进行端点探测。我们将为Blackbox创建一个简单的HTTP作业,如下所示:

scrape_configs:

.......

  - job_name: 'blackbox_http'

    metrics_path: /probe

    params:

      module: [http_2xx]  # Look for a HTTP 200 response.

    static_configs:

      - targets:

        - http://192.168.205.13:9090    # Target to probe with http.

        - https://prometheus.example.com   # Target to probe with https.

        - http://example.com:8080 # Target to probe with http on port 8080.

    relabel_configs:

      - source_labels: [__address__]

        target_label: __param_target

      - source_labels: [__param_target]

        target_label: instance

      - target_label: __address__

        replacement: ServerIP:9115

在上面的文件中,我们将Prometheus网络作为我们的网络端点进行监控。根据需要更换服务器IP。

参见以下其他示例:

scrape_configs:

......

  # Using [icmp] module

  # job_name can be anything

  - job_name: 'Blackbox_icmp'

    metrics_path: /probe

    params:

      module: [icmp]

    static_configs:

      - targets:

        #hostname or IP address of target Host

        - ServerIP

    relabel_configs:

      - source_labels: [__address__]

        target_label: __param_target

      - source_labels: [__param_target]

        target_label: instance

      - target_label: __address__

        # Blackbox exporter Host:Port

        replacement: exporterserverip:9115

  # Using [ssh_banner] module

  - job_name: 'Blackbox_ssh'

    metrics_path: /probe

    params:

      module: [ssh_banner]

    static_configs:

      - targets:

        - ServerIP:22

    relabel_configs:

      - source_labels: [__address__]

        target_label: __param_target

      - source_labels: [__param_target]

        target_label: instance

      - target_label: __address__

        replacement: exporterserverip:9115

  # Using [tcp_connect] module

  - job_name: 'Blackbox_tcp'

    metrics_path: /probe

    params:

      module: [tcp_connect]

    static_configs:

      - targets:

        # Target example is MariaDB/MySQL

        - ServerIP:3306

    relabel_configs:

      - source_labels: [__address__]

        target_label: __param_target

      - source_labels: [__param_target]

        target_label: instance

      - target_label: __address__

        replacement: exporterserverip:9115

保存文件并重新启动Prometheus:

sudo systemctl restart prometheus

5、Prometheus上的仪表板访问和指标查看

一旦安装并配置了导出器,您就可以在Prometheus上查看仪表板。导航到状态-目标(Status-Targets):

在Rocky Linux 9/AlmaLinux 9上安装Prometheus

有3个不同的端点,Prometheus、Node Exporter和Blackbox导出器:

在Rocky Linux 9/AlmaLinux 9上安装Prometheus

可显示更多以查看作业详细信息:

在Rocky Linux 9/AlmaLinux 9上安装Prometheus

现在点击图形并在Prometheus上设置所需的图形。要获取图形,请键入PromQL查询(如node_os_info)并执行它:

在Rocky Linux 9/AlmaLinux 9上安装Prometheus

您还可以使用查询node_network_speed_bytes查看网络速度,并导航到图形选项卡:

在Rocky Linux 9/AlmaLinux 9上安装Prometheus

到这里,在Rocky Linux 9/AlmaLinux 9上安装带有Node Exporter的Prometheus完成了。

栏目相关文章