如何在 CentOS 8 上安裝 Prometheus

在本教程中,我們將向您展示如何在 CentOS 8 上安裝 Prometheus。對於那些不知道的人,Prometheus 是一個很棒的開源監控系統,它允許我們從我們的應用程序收集指標並將它們存儲在數據庫中. ,尤其是基於時間序列的數據庫。 Prometheus最大的優勢在於它提供的用於數據處理的查詢語言。

本文假設您至少對 Linux 有基本的了解,您知道如何使用 shell,最重要的是,您在自己的 VPS 上託管您的網站。 安裝非常簡單,它假定您在 root 帳戶下運行,如果不是,您可能需要添加 'sudo' 獲取 root 權限的命令。 我將逐步向您展示如何在 CentOS 8 服務器上安裝 Prometheus。

以前的要求

  • 運行以下操作系統之一的服務器:CentOS 8。
  • 建議您使用全新的操作系統安裝以避免潛在問題。
  • non-root sudo user或訪問root user. 我們建議充當non-root sudo user但是,如果您在以 root 身份運行時不小心,可能會損壞您的系統。

在 CentOS 8 上安裝 Prometheus

第 1 步。首先,讓我們首先確保您的系統是最新的。

sudo dnf clean all
sudo dnf update

步驟 2. 為 Prometheus 添加系統用戶和組。

運行以下命令創建 Prometheus 系統用戶和組:

useradd -M -r -s /bin/false prometheus

步驟 3. 為 Prometheus 創建一個數據目錄。

創建一個用於存儲 Prometheus 數據的目錄:

mkdir /etc/prometheus
mkdir /var/lib/prometheus

步驟 3. 在 CentOS 8 上安裝 Prometheus。

我們需要下載最新版本的 Prometheus 歸檔文件並將其解壓縮以獲取二進製文件:

wget https://github.com/prometheus/prometheus/releases/download/v2.14.0/prometheus-2.14.0.linux-amd64.tar.gz -P /tmp
cd /tmp
tar -xzf prometheus-2.14.0.linux-amd64.tar.gz

接下來將Prometheus和promtool這兩個Prometheus文件複製到解壓出來的Prometheus文件目錄下/usr/local/bin目錄:

cp prometheus-2.14.0.linux-amd64/{prometheus,promtool} /usr/local/bin/
cp -r prometheus-2.14.0.linux-amd64/{consoles,console_libraries} /etc/prometheus/

配置普羅米修斯:

必須將設置添加到“/etc/prometheus/prometheus.yml”,打開配置文件進行修改,調整為如下所示:

nano /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).

# 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']

步驟 4. 創建 systemd 服務單元。

你需要創建一個systemd服務文件,/etc/systemd/system/prometheus.service配置如下:

nano /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

充值systemd服務:

systemctl daemon-reload
systemctl enable --now prometheus
systemctl status prometheus

步驟 5. 為 Prometheus 配置防火牆。

允許 Prometheus 通過防火牆:

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

步驟 6. 設置配置文件和目錄的所有權。

運行以下命令將 Prometheus 配置文件和目錄的所有權設置為 Prometheus:

chown -R prometheus:prometheus /etc/prometheus
chown -R prometheus:prometheus /var/lib/prometheus
chown prometheus.prometheus /usr/local/bin/{prometheus,promtool}

第 7 步。訪問普羅米修斯。

默認情況下,Prometheus 將在 HTTP 端口 9090 上可用。 打開您喜歡的瀏覽器並轉到https://your-domain.com:9090ohttps://server-ip-address:9090並完成必要的步驟以完成安裝。

恭喜! 您已成功安裝普羅米修斯。 感謝您使用本教程在 CentOS 8 系統上安裝 Prometheus,如需額外幫助或有用信息,建議您參考普羅米修斯官網.

相關文章