Linux 安裝 Prometheus

首先先到 Prometheus 官網的下載頁面,複製下載連結。本篇使用的是 3.0.1,2024-11-28 發布的版本

使用 wget 指令下載檔案,連結是剛剛在官網複製的下載連結:
    
wget https://github.com/prometheus/prometheus/releases/download/v3.0.1/prometheus-3.0.1.linux-amd64.tar.gz
    

將下載的檔案解壓縮:
註: 記得替換檔名
    
tar xvfz prometheus-3.0.1.linux-amd64.tar.gz
    

解壓縮後會產生 prometheus-3.0.1.linux-amd64 資料夾,我們把他移動到 linux 中專門存放第三方軟體的 /opt 路徑
    
sudo mv prometheus-3.0.1.linux-amd64 /opt/
    

進入到剛剛移動完畢的新路徑下:
    
cd /opt/prometheus-3.0.1.linux-amd64/
    

本次示範中不需要編輯設定檔,但是如果需要修改設定檔的話可以使用以下指令編輯設定檔:
    
sudo vi prometheus.yml
    

啟動 Prometheus
    
./prometheus --config.file=prometheus.yml
    

連線到 Prometheus,預設 PORT 是 9090 (可以在設定檔中修改):
註: 記得替換 IP
    
http://192.168.0.41:9090
    

上面啟動 Prometheus 的方式是在指令介面中呼叫執行檔,並不會自動在背景執行。我們可以使用 Linux 內建的服務來自動執行 Prometheus。
建立服務檔:
    
sudo vim /etc/systemd/system/prometheus.service
    

在服務檔中填入以下內容:
註: 記得替換 ExecStart 內的檔案路徑和設定檔路徑
    
[Unit]
Description=Prometheus Monitoring
Wants=network-online.target
After=network-online.target

[Service]
Restart=always
ExecStart=/opt/prometheus-3.0.1.linux-amd64/prometheus --config.file=/opt/prometheus-3.0.1.linux-amd64/prometheus.yml

[Install]
WantedBy=multi-user.target
    

啟用服務:
    
sudo systemctl enable prometheus
    

執行服務:
    
sudo systemctl start prometheus
    

這樣在開機後網路服務準備好時就會自動啟動 prometheus 了

參考資料:
Prometheus - First steps with Prometheus

留言