本文是一個(gè)“Hello World”風(fēng)格的教程柠并,演示了如何在簡(jiǎn)單的示例設(shè)置中安裝岭接,配置和使用Prometheus。您將在本地下載并運(yùn)行Prometheus臼予,然后將其自己看做一個(gè)應(yīng)用程序來(lái)進(jìn)行監(jiān)控鸣戴,同時(shí)使用Node Exporter采集主機(jī)數(shù)據(jù)。最后通過(guò)儀表盤(pán)來(lái)使用收集的時(shí)間序列數(shù)據(jù)粘拾。
安裝
為您的平臺(tái)下載最新版本的Prometheus窄锅,然后解壓縮并運(yùn)行它:
$ wget https://github.com/prometheus/prometheus/releases/download/v2.9.1/prometheus-2.9.1.linux-amd64.tar.gz
$ tar xvfz prometheus-2.9.1.linux-amd64.tar.gz
采集應(yīng)用數(shù)據(jù)
Prometheus通過(guò)在目標(biāo)應(yīng)用上的HTTP端點(diǎn)/metrics來(lái)收集受監(jiān)控目標(biāo)的指標(biāo)。由于Prometheus也以同樣的方式公開(kāi)數(shù)據(jù)缰雇,因此也可以抓取它的指標(biāo)和監(jiān)控自身的健康狀況入偷。
雖然Prometheus服務(wù)器收集有關(guān)自身的數(shù)據(jù)在實(shí)踐中并不是很有用,但它是一個(gè)很好的示例械哟。
配置
可以看到Prometheus的配置文件prometheus.yml:
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']
這里設(shè)置了Prometheus采集本地9090端口服務(wù)疏之。
啟動(dòng)
./prometheus --config.file=prometheus.yml
正常的情況下,你可以看到以下輸出內(nèi)容:
level=info ts=2019-04-18T03:24:43.863Z caller=main.go:285 msg="no time or size retention was set so using the default time retention" duration=15d
level=info ts=2019-04-18T03:24:43.863Z caller=main.go:321 msg="Starting Prometheus" version="(version=2.9.1, branch=HEAD, revision=ad71f2785fc321092948e33706b04f3150eee44f)"
level=info ts=2019-04-18T03:24:43.863Z caller=main.go:322 build_context="(go=go1.12.4, user=root@09f919068df4, date=20190416-17:50:04)"
level=info ts=2019-04-18T03:24:43.863Z caller=main.go:323 host_details="(Linux 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 localhost.localdomain (none))"
level=info ts=2019-04-18T03:24:43.863Z caller=main.go:324 fd_limits="(soft=1024, hard=4096)"
level=info ts=2019-04-18T03:24:43.863Z caller=main.go:325 vm_limits="(soft=unlimited, hard=unlimited)"
level=info ts=2019-04-18T03:24:43.864Z caller=main.go:640 msg="Starting TSDB ..."
level=info ts=2019-04-18T03:24:43.874Z caller=web.go:416 component=web msg="Start listening for connections" address=0.0.0.0:9090
level=info ts=2019-04-18T03:24:43.876Z caller=main.go:655 msg="TSDB started"
level=info ts=2019-04-18T03:24:43.876Z caller=main.go:724 msg="Loading configuration file" filename=prometheus.yml
level=info ts=2019-04-18T03:24:43.877Z caller=main.go:751 msg="Completed loading of configuration file" filename=prometheus.yml
level=info ts=2019-04-18T03:24:43.877Z caller=main.go:609 msg="Server is ready to receive web requests."
這時(shí)Prometheus應(yīng)該已經(jīng)啟動(dòng)暇咆。您還應(yīng)該通過(guò)localhost:9090瀏覽到自己的狀態(tài)頁(yè)面锋爪。過(guò)幾秒鐘后就會(huì)從自己的HTTP指標(biāo)端點(diǎn)收集到有關(guān)數(shù)據(jù)丙曙。
您還可以通過(guò)瀏覽其指標(biāo)端點(diǎn)來(lái)驗(yàn)證Prometheus是否正在提供有關(guān)自身的指標(biāo): localhost:9090/metrics
使用Node Exporter采集主機(jī)數(shù)據(jù)
Exporter的作用是暴露已有的第三方服務(wù)的 metrics 給 Prometheus。Exporter將監(jiān)控?cái)?shù)據(jù)采集的端點(diǎn)通過(guò)HTTP服務(wù)的形式暴露給Prometheus Server其骄,Prometheus Server通過(guò)訪問(wèn)該Exporter提供的Endpoint端點(diǎn)亏镰,即可獲取到需要采集的監(jiān)控?cái)?shù)據(jù)。
從上面的描述中可以看出Exporter可以是一個(gè)相對(duì)開(kāi)放的概念拯爽,其可以是一個(gè)獨(dú)立運(yùn)行的程序獨(dú)立于監(jiān)控目標(biāo)以外索抓,也可以是直接內(nèi)置在監(jiān)控目標(biāo)中。只要能夠向Prometheus提供標(biāo)準(zhǔn)格式的監(jiān)控樣本數(shù)據(jù)即可毯炮。
這里為了能夠采集到主機(jī)的運(yùn)行指標(biāo)如CPU, 內(nèi)存逼肯,磁盤(pán)等信息。我們可以使用Node Exporter否副。
安裝
$ wget https://github.com/prometheus/node_exporter/releases/download/v0.17.0/node_exporter-0.17.0.linux-amd64.tar.gz
$ tar xvfz node_exporter-0.17.0.linux-amd64.tar.gz
運(yùn)行
$ cd node_exporter-0.17.0.linux-amd64
$ ./node_exporter
看到如下輸出:
INFO[0000] Starting node_exporter (version=0.17.0, branch=HEAD, revision=f6f6194a436b9a63d0439abc585c76b19a206b21) source="node_exporter.go:82"
INFO[0000] Build context (go=go1.11.2, user=root@322511e06ced, date=20181130-15:51:33) source="node_exporter.go:83"
INFO[0000] Enabled collectors: source="node_exporter.go:90"
INFO[0000] - arp source="node_exporter.go:97"
INFO[0000] - bcache source="node_exporter.go:97"
INFO[0000] - bonding source="node_exporter.go:97"
INFO[0000] - conntrack source="node_exporter.go:97"
INFO[0000] - cpu source="node_exporter.go:97"
INFO[0000] - diskstats source="node_exporter.go:97"
INFO[0000] - edac source="node_exporter.go:97"
INFO[0000] - entropy source="node_exporter.go:97"
INFO[0000] - filefd source="node_exporter.go:97"
INFO[0000] - filesystem source="node_exporter.go:97"
INFO[0000] - hwmon source="node_exporter.go:97"
INFO[0000] - infiniband source="node_exporter.go:97"
INFO[0000] - ipvs source="node_exporter.go:97"
INFO[0000] - loadavg source="node_exporter.go:97"
INFO[0000] - mdadm source="node_exporter.go:97"
INFO[0000] - meminfo source="node_exporter.go:97"
INFO[0000] - netclass source="node_exporter.go:97"
INFO[0000] - netdev source="node_exporter.go:97"
INFO[0000] - netstat source="node_exporter.go:97"
INFO[0000] - nfs source="node_exporter.go:97"
INFO[0000] - nfsd source="node_exporter.go:97"
INFO[0000] - sockstat source="node_exporter.go:97"
INFO[0000] - stat source="node_exporter.go:97"
INFO[0000] - textfile source="node_exporter.go:97"
INFO[0000] - time source="node_exporter.go:97"
INFO[0000] - timex source="node_exporter.go:97"
INFO[0000] - uname source="node_exporter.go:97"
INFO[0000] - vmstat source="node_exporter.go:97"
INFO[0000] - xfs source="node_exporter.go:97"
INFO[0000] - zfs source="node_exporter.go:97"
INFO[0000] Listening on :9100 source="node_exporter.go:111"
代表已啟動(dòng)成功汉矿,可通過(guò)9100端口訪問(wèn)。
從Node Exporter收集監(jiān)控?cái)?shù)據(jù)
為了能夠讓Prometheus Server能夠從當(dāng)前node exporter獲取到監(jiān)控?cái)?shù)據(jù)备禀,這里需要修改Prometheus配置文件。編輯prometheus.yml并在scrape_configs節(jié)點(diǎn)下添加以下內(nèi)容:
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: 'node'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9100']
重新啟動(dòng)Prometheus Server
訪問(wèn)http://localhost:9090奈揍,進(jìn)入到Prometheus Server曲尸。輸入“up”并且點(diǎn)擊執(zhí)行按鈕以后,可以看到如下結(jié)果:
其中“1”表示正常男翰,反之“0”則為異常另患。
集成Grafana
第三方的可視化工具Grafana是一個(gè)開(kāi)源的可視化平臺(tái),并且提供了對(duì)Prometheus的完整支持蛾绎。
下載安裝
官網(wǎng)下載地址:Grafana
根據(jù)自己的系統(tǒng)版本和配置昆箕,下載對(duì)應(yīng)的包。3000為Grafana的默認(rèn)偵聽(tīng)端口租冠,啟動(dòng)后打開(kāi)瀏覽器鹏倘,輸入IP+端口進(jìn)行訪問(wèn)。
系統(tǒng)默認(rèn)用戶(hù)名和密碼為admin/admin顽爹,第一次登陸系統(tǒng)會(huì)要求修改密碼纤泵。
添加數(shù)據(jù)源
首先是添加數(shù)據(jù)源,這里選擇Prometheus作為默認(rèn)的數(shù)據(jù)源镜粤,如下圖所示捏题,指定數(shù)據(jù)源類(lèi)型為Prometheus并且設(shè)置Prometheus的訪問(wèn)地址即可,在配置正確的情況下點(diǎn)擊“Save & Test”按鈕肉渴,會(huì)提示連接成功的信息:
創(chuàng)建DashBoard
創(chuàng)建好數(shù)據(jù)源之后公荧,就需要?jiǎng)?chuàng)建DashBoard(儀表盤(pán)),可以自定義同规,也可以導(dǎo)入你需要的儀表盤(pán)循狰,官方提供了很多的可選儀表盤(pán)庸诱。
這里我們選擇官方提供的Prometheus 2.0 Stats。
展示儀表盤(pán)
Grafana中所有的Dashboard通過(guò)JSON進(jìn)行共享晤揣,下載并且導(dǎo)入這些JSON文件桥爽,就可以直接使用這些已經(jīng)定義好的Dashboard:
可以在Explore的“Metrics”選項(xiàng)下通過(guò)PromQL查詢(xún)需要可視化的數(shù)據(jù),比如輸入指標(biāo)node_memory_MemFree_bytes查看系統(tǒng)可用內(nèi)存: