作為監(jiān)控系統(tǒng)的后起之秀,prometheus的安裝可謂非常簡單,不需要第三方的依賴(數(shù)據(jù)庫、緩存、PHP之類的)。下面演示如何二進(jìn)制安裝prometheus、使用 Node Exporter 采集主機(jī)信息并使用Grafana來進(jìn)行圖形化的展示。
1. 安裝Prometheus Server
Prometheus基于Golang編寫,編譯后的軟件包,不依賴于任何的第三方依賴。用戶只需要下載對應(yīng)平臺(tái)的二進(jìn)制包,解壓并且添加基本的配置即可正常啟Prometheus Server。
1.1 下載并解壓二進(jìn)制安裝包
通過prometheus的官網(wǎng),我們下載最新版本的prometheus,目前看到的最新版本是 2.13.0,這是在2019-10-04日的版本。
#下載、解壓、創(chuàng)建軟鏈接
wget https://github.com/prometheus/prometheus/releases/download/v2.13.0/prometheus-2.13.0.linux-amd64.tar.gz
tar -xf prometheus-2.13.0.linux-amd64.tar.gz
mv prometheus-2.13.0.linux-amd64 /usr/local/
ln -s /usr/local/prometheus-2.13.0.linux-amd64/ /usr/local/prometheus
1.2 配置說明
解壓后當(dāng)前目錄會(huì)包含默認(rèn)的Prometheus配置文件promethes.yml,下面配置文件做下簡略的解析:
# 全局配置
global:
scrape_interval: 15s # 設(shè)置抓取間隔,默認(rèn)為1分鐘
evaluation_interval: 15s #估算規(guī)則的默認(rèn)周期,每15秒計(jì)算一次規(guī)則。默認(rèn)1分鐘
# scrape_timeout #默認(rèn)抓取超時(shí),默認(rèn)為10s
# Alertmanager相關(guān)配置
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# 規(guī)則文件列表,使用'evaluation_interval' 參數(shù)去抓取
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# 抓取配置列表
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
1.3 創(chuàng)建prometheus的用戶及數(shù)據(jù)存儲(chǔ)目錄
為了安全,我們使用普通用戶來啟動(dòng)prometheus服務(wù)。作為一個(gè)時(shí)序型的數(shù)據(jù)庫產(chǎn)品,prometheus的數(shù)據(jù)默認(rèn)會(huì)存放在應(yīng)用所在目錄下,我們需要修改為 /data/prometheus下。
useradd -s /sbin/nologin -M prometheus
mkdir /data/prometheus -p
#修改目錄屬主
chown -R prometheus:prometheus /usr/local/prometheus/
chown -R prometheus:prometheus /data/prometheus/
1.4 創(chuàng)建Systemd服務(wù)啟動(dòng)prometheus
prometheus的啟動(dòng)很簡單,只需要直接啟動(dòng)解壓目錄的二進(jìn)制文件prometheus即可,但是為了更加方便對prometheus進(jìn)行管理,這里使用systemd來啟停prometheus。
vim /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/data/prometheus
Restart=on-failure
[Install]
WantedBy=multi-user.target
備注:在service文件里面,我們定義了啟動(dòng)的命令,定義了數(shù)據(jù)存儲(chǔ)在/data/prometheus路徑下,否則默認(rèn)會(huì)在prometheus二進(jìn)制的目錄的data下。
systemctl start prometheus
systemctl status prometheus
systemctl enable prometheus
1.5 打開prometheus的web頁面
到這里,prometheus就已經(jīng)安裝好了,是不是比zabbix要簡單多了呢。prometheus啟動(dòng)后默認(rèn)會(huì)啟動(dòng)9090端口,我們通過瀏覽器打開該端口頁面看下吧,后續(xù)的學(xué)習(xí)會(huì)在這個(gè)頁面上進(jìn)行的。

2. Grafana的安裝
雖然說prometheus能展示一些圖表,但對比Grafana,那只是個(gè)過家家。接下來我們需要在同一個(gè)服務(wù)器上安裝Grafana服務(wù),用來展示prometheus收集到的數(shù)據(jù)。
2.1 下載并解壓二進(jìn)制包
這里演示的Grafana的版本為6.4.2,要下載其他的版本可以到Grafana的官網(wǎng)進(jìn)行下載。
wget https://dl.grafana.com/oss/release/grafana-6.4.2.linux-amd64.tar.gz
tar -zxvf grafana-6.4.2.linux-amd64.tar.gz
mv grafana-6.4.2 /usr/local/
ln -s /usr/local/grafana-6.4.2/ /usr/local/grafana
2.2 創(chuàng)建grafana用戶及數(shù)據(jù)存放目錄
useradd -s /sbin/nologin -M grafana
mkdir /data/grafana
chown -R grafana:grafana /usr/local/grafana/
chown -R grafana:grafana /data/grafana/
2.3 修改配置文件
修改 /usr/local/grafana/conf/defaults.ini 文件,配置為上面新建的數(shù)據(jù)目錄。
data = /data/grafana/data
logs = /data/grafana/log
plugins = /data/grafana/plugins
provisioning = /data/grafana/conf/provisioning
2.4 把grafana-server添加到systemd中
新增 grafana-server.service 文件,使用systemd來管理grafana服務(wù)
vim /etc/systemd/system/grafana-server.service
[Unit]
Description=Grafana
After=network.target
[Service]
User=grafana
Group=grafana
Type=notify
ExecStart=/usr/local/grafana/bin/grafana-server -homepath /usr/local/grafana
Restart=on-failure
[Install]
WantedBy=multi-user.target
啟停并設(shè)置開機(jī)啟動(dòng)
systemctl start grafana-server
systemctl status grafana-server
systemctl enable grafana-server
2.5 打開grafana的web頁面
到這里,grafana已經(jīng)安裝完畢。默認(rèn)情況下,grafana-server會(huì)啟動(dòng)3000端口,我們使用瀏覽器打開grafana頁面,然后輸入默認(rèn)的賬號(hào)密碼 admin/admin登錄。
2.6 添加數(shù)據(jù)源
grafana雖然已經(jīng)安裝好了,但是這個(gè)時(shí)候還沒有數(shù)據(jù),沒辦法作圖。下面我們把grafana和prometheus關(guān)聯(lián)起來,也就是在grafana中添加添加數(shù)據(jù)源。
在配置頁面點(diǎn)擊添加數(shù)據(jù)源,然后選擇prometheus,輸入prometheus服務(wù)的參數(shù)即可。

按照上圖示例添加數(shù)據(jù)源之后,點(diǎn)擊save & test就可以了。
2.7 添加自帶的示例圖表
按照上面的指導(dǎo)添加數(shù)據(jù)源之后,我們就可以針對這些數(shù)據(jù)來繪制圖表了。grafana最人性化的一點(diǎn)就是擁有大量的圖表模板,我們只需要導(dǎo)入模板即可,從而省去了大量的制作圖表的時(shí)間。
目前我們的prometheus還沒有什么監(jiān)控?cái)?shù)據(jù),只有prometheus本身的數(shù)據(jù),我們看下這些prometheus本身數(shù)據(jù)圖表是怎樣的。
在添加數(shù)據(jù)源的位置上,右邊的選項(xiàng)有個(gè)Dashboards的菜單選項(xiàng),我們點(diǎn)擊進(jìn)去,然后導(dǎo)入prometheus2.0.


最后我們在左上角的位置上選擇這個(gè)圖表查看下,是不是很酷炫呢。這是個(gè)簡單的示例,后續(xù)我們使用node-exporter來收集主機(jī)的性能信息,然后在grafana中展示。

3. 使用Node Exporter采集主機(jī)運(yùn)行數(shù)據(jù)
與傳統(tǒng)的監(jiān)控zabbix來對比的話,prometheus-server就像是mysql,負(fù)責(zé)存儲(chǔ)數(shù)據(jù)。只不過這是時(shí)序數(shù)據(jù)庫而不是關(guān)系型的數(shù)據(jù)庫。數(shù)據(jù)的收集還需要其他的客戶端,在prometheus中叫做exporter。針對不同的服務(wù),有各種各樣的exporter,就好比zabbix的zabbix-agent一樣。
這里為了能夠采集到主機(jī)的運(yùn)行指標(biāo)如CPU, 內(nèi)存,磁盤等信息。我們可以使用Node Exporter。Node Exporter同樣采用Golang編寫,并且不存在任何的第三方依賴,只需要下載,解壓即可運(yùn)行??梢詮?a target="_blank">https://prometheus.io/download/獲取最新的node exporter版本的二進(jìn)制包。
3.1 下載node exporter
wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz
tar -xf node_exporter-0.18.1.linux-amd64.tar.gz
#新建一個(gè)目錄專門安裝各種exporter
mkdir -p /usr/local/prometheus_exporter
mv node_exporter-0.18.1.linux-amd64 /usr/local/prometheus_exporter/
cd /usr/local/prometheus_exporter/
ln -s node_exporter-0.18.1.linux-amd64/ node_exporter
3.2 啟動(dòng)node exporter
直接打開node_exporter的可執(zhí)行文件即可啟動(dòng) node export,默認(rèn)會(huì)啟動(dòng)9100端口。建議使用nohup來啟動(dòng)
/usr/local/prometheus_exporter/node_exporter/node_exporter
#建議使用nohup
nohup /usr/local/prometheus_exporter/node_exporter/node_exporter >/dev/null 2>&1 &
3.3 加入開機(jī)啟動(dòng)
在 /etc/rc.local 加入上面的啟動(dòng)命令即可
##node exporter
nohup /usr/local/prometheus_exporter/node_exporter/node_exporter >/dev/null 2>&1 &
3.4 配置Prometheus,收集node exporter的數(shù)據(jù)
可以看到node exporter啟動(dòng)后也就是暴露了9100端口,并沒有把數(shù)據(jù)傳到prometheus,我們還需要在prometheus中配置,讓prometheus去pull這個(gè)接口的數(shù)據(jù)。
編輯prometheus.yml文件,增加后面4行.
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
#采集node exporter監(jiān)控?cái)?shù)據(jù)
- job_name: 'node'
static_configs:
- targets: ['localhost:9100']
然后重啟prometheus,打開prometheus頁面查看是不是有對應(yīng)的數(shù)據(jù)了。

在prometheus的web界面看到這個(gè)節(jié)點(diǎn)是up的狀態(tài)了,接下來我們在grafana中添加對應(yīng)的模板。
3.5 導(dǎo)入grafana模板,數(shù)據(jù)展示
在導(dǎo)入界面,我們輸入模板的編號(hào),這里我使用的是9276號(hào)模板,如要使用其他的模板,請到grafana的官網(wǎng)去查找 https://grafana.com/dashboards

選擇數(shù)據(jù)源,然后點(diǎn)擊導(dǎo)入

然后你就可以看到下面一個(gè)這么形象具體好看的界面了。

到這里,Prometheus+Grafana的安裝就完畢了。后面深入的學(xué)習(xí)可以看我的讀書筆記。