Prometheus安裝部署和node_exporter的使用

一、環境

服務器IP 系統 組件
192.168.0.181 CentOS7.6 Prometheus Server 2.18.1
192.168.0.182 CentOS7.6 node_exporter 1.0.0

下載地址爲:https://prometheus.io/download/,我這裏下載的都是最新版本的。

二、安裝Prometheus Server

以下操作在192.168.0.181上執行

1、下載安裝包

$ cd /usr/local/src/
$ wget https://github.com/prometheus/prometheus/releases/download/v2.18.1/prometheus-2.18.1.linux-amd64.tar.gz
$ wget https://github.com/prometheus/node_exporter/releases/download/v1.0.0/node_exporter-1.0.0.linux-amd64.tar.gz

2、解壓安裝包

$ tar -zxf prometheus-2.18.1.linux-amd64.tar.gz -C /usr/local/
$ cd /usr/local/
$ mv prometheus-2.18.1.linux-amd64 prometheus

3、安裝Prometheus

創建prometheus用戶,指定家目錄爲/var/lib/prometheus用來作爲prometheus的本地存儲。

$ groupadd prometheus
$ useradd -g prometheus -m -d /var/lib/prometheus -s /sbin/nologin prometheus

4、將prometheus交由systemd管理

cat > /usr/lib/systemd/system/prometheus.service << "EOF"
[Unit]
Description=prometheus
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus --storage.tsdb.retention=15d --log.level=info --web.enable-lifecycle
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF

來看一下prometheus啓動時的一些參數:

--config.file:指定配置文件的路徑
--storage.tsdb.path:指定Prometheus本地存儲的路徑。最好是指定較大分區下的目錄,因爲Prometheus收集的監控數據都會存儲在這個目錄下。
--log.level:指定日誌級別,爲[debug, info, warn, error]中的一種。
--web.enable-lifecycle:支持熱加載新配置。從2.0開始,hot reload功能是默認關閉的,如需開啓,需要在啓動Prometheus的時候,添加--web.enable-lifecycle參數,熱加載的方式爲curl -X POST localhost:9090/-/reload

三、安裝node_exporter

在兩個節點上都安裝上ndoe_exporter用來監控主機,以下操作需在兩臺上都執行。

注意:node_exporter也是用prometheus用戶啓動的,所以需要在每個安裝ndoe_exporter的節點上都創建prometheus用戶

$ groupadd prometheus
$ useradd -g prometheus -m -d /var/lib/prometheus -s /sbin/nologin prometheus

解壓

$ tar -zxf node_exporter-1.0.0.linux-amd64.tar.gz -C /usr/local/
$ cd /usr/local/
$ mv node_exporter-1.0.0.linux-amd64 node_exporter
$ chown -R prometheus:prometheus node_exporter

將node_exporter交由systemd管理

cat > /usr/lib/systemd/system/node_exporter.service << "EOF"
[Unit]
Description=node_export
Documentation=https://github.com/prometheus/node_exporter
After=network.target
 
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF

啓動node_exporter服務

$ systemctl daemon-reload
$ systemctl enable node_exporter.service
$ systemctl start node_exporter.service
$ systemctl status node_exporter.service

四、配置Prometheus添加監對象

$ cd /usr/local/prometheus/
$ vim prometheus.yml
# 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','localhost:9100'] # 對本機node_exporter進行監控

# 新增對其它node節點的監控
  - job_name: '192.168.0.182'
    #重寫了全局抓取間隔時間,由15秒重寫成5秒。
    scrape_interval: 5s
    static_configs:
    - targets: ['192.168.0.182:9100']

啓動Prometheus Server:

$ chown -R prometheus:prometheus /usr/local/prometheus
$ systemctl daemon-reload
$ systemctl enable prometheus.service
$ systemctl start prometheus.service
$ systemctl status prometheus.service

現在可以訪問Prometheus的web頁面來查看監控的主機
在這裏插入圖片描述

參考文章:
http://www.eryajf.net/2468.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章