一、基礎環境
|
|
系統 |
IP |
|
監控主機 |
CentOS 7 |
192.168.56.200 |
|
被監控主機 |
CentOS 7 |
192.168.56.201 |
二、Prometheus服務端安裝
以下操作皆在監控主機(192.168.56.200)上執行,
2.0 關倍訓器防火墻
# systemctl stop firewalld
# systemctl disable firewalld

2.1 安裝 go 環境
由于Prometheus是由go語言開發的,所以在安裝Prometheus之前需要先在監控主機上安裝go環境,這里采用原始碼編譯的方式安裝,
由于國內網路環境的原因,如果能夠科學的上網,可從此地址下載最新版本的安裝包:https://golang.org/dl/ ,
未能科學的上網的,可從此鏈接下載:鏈接:https://pan.baidu.com/s/1gefGeXmoFmjGxSGxgCuQfw 提取碼:cz6l
安裝包下載以后,上傳至監控主機的 /usr/local 目錄下,

2.1.1 解壓安裝包
# tar -xvf go1.13.1.linux-amd64.tar.gz
2.1.2 配置環境變數
添加/usr/loacl/go/bin目錄到PATH變數中,添加到/etc/profile 或$HOME/.profile都可以
# vim /etc/profile
// 在最后一行添加
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
// wq保存退出后source一下
# source /etc/profile
執行go version,如果顯示版本號,則Go環境安裝成功,

2.2 安裝Prometheus
安裝包下載地址:https://prometheus.io/download/#prometheus
2.2.1 安裝
將下載后安裝包,上傳至 /usr/local 目錄下

解壓安裝包:
# tar -xvf prometheus-2.4.0.linux-amd64.tar.gz
# mv prometheus-2.4.0.linux-amd64/ prometheus
2.2.2 啟動
Prometheus的組態檔位于 /usr/local/Prometheus/prometheus.yml ,此處采用默認配置,
進入解壓后的檔案夾下,啟動Prometheus,
# cd prometheus
# ./prometheus --config.file=/usr/local/prometheus/prometheus.yml &
2.2.3 驗證
瀏覽器打開http://192.168.56.200:9090(IP:9090埠)即可打開普羅米修斯自帶的監控頁面

2.2.4 以服務的方式啟動
Ctrl+C 結束掉Prometheus行程,創建Prometheus服務,讓Prometheus以服務的方式,開機自啟,
添加系統服務
# vim /etc/systemd/system/prometheus.service
將以下內容寫入檔案中
[Unit]
Description=Prometheus Monitoring System
Documentation=Prometheus Monitoring System
[Service]
ExecStart=/usr/local/prometheus/prometheus \
--config.file=/usr/local/prometheus/prometheus.yml \
--web.listen-address=:9090
Restart=on-failure
[Install]
WantedBy=multi-user.target
啟動服務,設定開機自啟,并檢查服務開啟狀態
# systemctl daemon-reload
# systemctl enable prometheus
# systemctl start prometheus
# systemctl status prometheus

三、安裝Grafana
Prometheus自帶的監控頁面顯示的內容沒有那么直觀,我們安裝grafana來使監控資料看起來更加直觀
3.1、安裝grafana
此處安裝采用原始碼編譯的方式安裝,在監控主機(192.168.56.200)/usr/local 目錄下 下載安裝包,并安裝
# wget https://dl.grafana.com/oss/release/grafana-6.4.3-1.x86_64.rpm
# yum localinstall grafana-6.4.3-1.x86_64.rpm
沒有wget工具的,首先安裝wget工具:
# yum -y install wget
3.2、啟動grafana
設定grafana服務開機自啟,并啟動服務
# systemctl daemon-reload
# systemctl enable grafana-server.service
# systemctl start grafana-server.service
3.3、訪問grafana
瀏覽器訪問http://192.168.56.200:3000(IP:3000埠),即可打開grafana頁面,默認用戶名密碼都是admin,初次登錄會要求修改默認的登錄密碼


3.4、添加Prometheus資料源
(1)點擊主界面的“Add data source”

(2)選擇Prometheus

(3)填寫資料源設定項
URL處填寫Prometheus服務所在的IP地址,此處我們將Prometheus服務與Grafana安裝在同一臺機器上,直接填寫localhost即可

點擊下方 【Save & Test】按鈕,保存設定

(4)Dashboards頁面選擇“Prometheus 2.0 Stats”
點擊Dashboards選項卡,選擇Prometheus 2.0 Stats

(5)查看監控
點擊Grafana圖示,切換到Grafana主頁面,然后點擊Home,選擇我們剛才添加的Prometheus 2.0 Stats,即可看到監控資料




至此Prometheus服務端及Grafana配置完成,
四、安裝 node-exporter
以下操作皆在被監控主機(192.168.56.201)上操作,
4.0、關倍訓器防火墻
# systemctl stop firewalld
# systemctl disable firewalld
4.1、安裝node-exporter
首先下載node-exporter安裝包,下載地址:https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-arm64.tar.gz
將下載的安裝包上傳至被監控主機(192.168.56.201)的 /usr/local 目錄下

解壓安裝包
# tar -zvxf node_exporter-0.18.1.linux-amd64.tar.gz
# mv node_exporter-0.18.1.linux-amd64/ node_exporter
4.2、啟動node-exporter
進入解壓后的node_exporter檔案夾下,啟動node_exporter
# cd node_exporter
# ./node_exporter
4.3、驗證
在瀏覽器訪問 http://192.168.56.201:9100/metrics ,若出現資料則服務開啟成功

4.4、設定node_exporter 以服務的方式啟動并設定開機自啟
Ctrl+C 結束掉node_exporter行程,創建node_exporter服務,讓node_exporter以服務的方式,開機自啟,
添加系統服務
# vim /etc/systemd/system/node_exporter.service
將以下內容寫入檔案中
[Unit]
Description=node_exporter
After=network.target
[Service]
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
啟動服務,設定開機自啟,并檢查服務開啟狀態
# systemctl daemon-reload
# systemctl enable node_exporter
# systemctl start node_exporter
# systemctl status node_exporter

至此node_exporter配置完成,
五、修改Prometheus 配置,監控Linux機器
以下操作皆在監控主機(192.168.56.200)上進行,
5.1、修改Prometheus配置
進入Prometheus的安裝檔案夾,打開Prometheus組態檔
# cd /usr/local/prometheus
# vim prometheus.yml
在scrape_configs標簽下,添加以下內容,配置監控
- job_name: 'Linux'
static_configs:
- targets: ['192.168.56.201:9100']
labels:
instance: Linux
以下是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).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# 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']
- job_name: 'Linux'
static_configs:
- targets: ['192.168.56.201:9100']
labels:
instance: Linux
保存退出,重啟Prometheus服務
# systemctl restart prometheus
瀏覽器訪問 http://192.168.56.200:9090/targets 查看監控資訊

可以看到,Linux機器已經加入進來,
5.2、配置Grafana
添加dashboard
Grafana官方為我們提供了很多dashboard頁面,可直接下載使用,瀏覽器訪問 https://grafana.com/grafana/dashboards 下載所需要的dashboard頁面

選擇資料源為Prometheus,然后我們選擇第一個dashboard
復制dashboard Id

然后打開我們的Grafana監控頁面,打開dashboard的管理頁面

點擊【import】按鈕

然后將我們剛才的復制的dashboard Id 復制進去

Grafana會自動識別dashboard Id ,
然后點擊【change】按鈕,生成一個隨機的UID,然后點擊下方輸入框,選擇我們之前創建的資料源Prometheus,最后點擊【Import】按鈕,即可完成匯入,

匯入成功后,會自動打開該Dashboard,即可看到我們剛才設定好的node監控

至此Prometheus+Grafana 安裝配置,并監控Linux機器,配置完成,
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/158858.html
標籤:Linux
