文章目的:
1则剃、向沒(méi)聽(tīng)過(guò)或者剛聽(tīng)過(guò)但是還對(duì)這個(gè)監(jiān)控系統(tǒng)沒(méi)有任何概念的開(kāi)發(fā)者介紹Prometheus的應(yīng)用場(chǎng)景。
2如捅、向有需要深入的了解的人推薦一些博客棍现、書籍。
3镜遣、內(nèi)心真實(shí)想法:本想寫個(gè)系統(tǒng)一點(diǎn)的己肮,但是列出那些博文書籍之后,感覺(jué)已經(jīng)沒(méi)有必要了悲关。谎僻。。
什么是Prometheus寓辱?
prometheus是一個(gè)開(kāi)源的監(jiān)控系統(tǒng)艘绍。。秫筏。算了官方的話一大把看得太累
那么到底什么是Prometheus
呢诱鞠,他確實(shí)是用來(lái)做監(jiān)控系統(tǒng)的挎挖,但是這里我們先不討論它,我們來(lái)看個(gè)小工具node_exporter
什么是 node_exporter
舉個(gè)例子航夺,如果你有一臺(tái)服務(wù)器蕉朵,你想要獲取它運(yùn)行時(shí)候的參數(shù),比如當(dāng)前的CPU負(fù)載阳掐、系統(tǒng)負(fù)載始衅、內(nèi)存消耗、硬盤使用量锚烦、網(wǎng)絡(luò)IO等等快耿,那么你就可以在服務(wù)器上運(yùn)行一個(gè)node_exporter
刚陡,它能幫你把這些參數(shù)收集好,并且暴露出一個(gè)HTTP接口以便你訪問(wèn)查詢。廢話不多說(shuō)我們直接試一試
準(zhǔn)備一臺(tái)Linux服務(wù)器岛请,外網(wǎng)互通:
IP:10.19.70.139
系統(tǒng):ubuntu 16.04
登錄系統(tǒng)進(jìn)入/opt
目錄(目錄隨便指定烤蜕,空間大于待會(huì)兒下載的文件大小即可)
下載并運(yùn)行最新版的node_exporter 【如果你想看源碼堕担,可以訪問(wèn)gihub開(kāi)源地址,它是Go語(yǔ)言編寫的翻斟,部署很方便】
cd /opt sudo wget https://github.com/prometheus/node_exporter/releases/download/v0.17.0/node_exporter-0.17.0.linux-amd64.tar.gz sudo tar -zxvf node_exporter-0.17.0.linux-amd64.tar.gz cd node_exporter-0.17.0.linux-amd64 sudo ./node_exporter
可以看到如下的輸出
訪問(wèn)方式:IP+Port
我們?cè)L問(wèn)http://10.19.70.139:9100
點(diǎn)擊Metrics
(如果你使用老版本的node_exporter那么可能沒(méi)有這個(gè)Metric標(biāo)題頁(yè))就可以看到暴露出來(lái)的系統(tǒng)參數(shù)了
指標(biāo)舉例
#HELP node_cpu_seconds_total Seconds the cpus spent in each mode.
#TYPE node_cpu_seconds_total counter
node_cpu_seconds_total{cpu="0",mode="idle"} 1.40181324e+06
這是其中一條指標(biāo),由指標(biāo)名+{標(biāo)簽鍵值對(duì)}+值
組成苞尝。
#HELP
開(kāi)頭的行用來(lái)說(shuō)明下面指標(biāo)node_cpu_seconds_total
的意義:
指標(biāo)node_cpu_seconds_total
用來(lái)表示CPU在每種模式下工作的秒數(shù)畸肆,而大括號(hào)里的稱之為標(biāo)簽,以鍵值對(duì)的形式出現(xiàn)宙址,可以用來(lái)細(xì)分同一個(gè)指標(biāo)下的不同部分轴脐,比如這里的cpu="0"
表示CPU的第0個(gè)核心,mode="idle"
表示工作模式為idle抡砂,也就是沒(méi)被任何程序使用的空閑模式大咱。
#TYPE
開(kāi)始的第二行表示指標(biāo)的數(shù)值類型為counter
類型。在Prometheus的規(guī)范中注益,counter
類型表示只增長(zhǎng)的類型碴巾,也就是只會(huì)增加不會(huì)減少的值,且數(shù)值只能是正整數(shù)丑搔。大數(shù)值會(huì)啟用科學(xué)計(jì)數(shù)法厦瓢。
此時(shí)可以看出指標(biāo)node_cpu_seconds_total{cpu="0",mode="idle"} 1.40181324e+06
表示節(jié)點(diǎn)的CPU0的空閑時(shí)間共計(jì)1.40181324e+06秒。數(shù)值從系統(tǒng)開(kāi)機(jī)時(shí)算起啤月,重啟歸零煮仇。
問(wèn)題與答案:
此時(shí)我們可以從頁(yè)面中看到很多類似的指標(biāo)以及值,如果只打算監(jiān)控這一臺(tái)谎仲,又或者只需要這一個(gè)值欺抗,我們可以自行編寫代碼訪問(wèn)這個(gè)接口,匹配出想要的指標(biāo)和值即可强重。但是如果有很多臺(tái)服務(wù)器绞呈,而且想要綜合多臺(tái)進(jìn)行對(duì)比分析,更有甚者希望將指標(biāo)繪制成圖表间景,或者說(shuō)保留歷史數(shù)據(jù)以便查詢過(guò)去的某個(gè)時(shí)間點(diǎn)其服務(wù)器的運(yùn)行狀態(tài)佃声,那么這將會(huì)變得很麻煩。而
Prometheus
就是干這個(gè)事情的倘要。
什么是Prometheus圾亏?
現(xiàn)在當(dāng)我們?cè)僖淮斡懻撌裁词荘rometheus的時(shí)候,我想你心里一定有個(gè)大概的猜測(cè)了封拧,如果你認(rèn)真思考了上面的問(wèn)題與答案
的話志鹃。我們想要獲取所有的服務(wù)器上node_exporter暴露出來(lái)的數(shù)據(jù),就必須有個(gè)程序去定時(shí)訪問(wèn)這些接口泽西,如果想要增加或者修改這些接口曹铃,那么就需要有個(gè)配置文件來(lái)記錄這些服務(wù)器的地址,如果想要訪問(wèn)歷史的某個(gè)時(shí)間點(diǎn)的數(shù)據(jù)捧杉,那么就必須按照時(shí)間順序存儲(chǔ)獲取到的指標(biāo)和值陕见。而如果想要將值繪制成圖,也需要有代碼去查詢味抖、計(jì)算和渲染评甜。最后你可能還希望當(dāng)服務(wù)器的某個(gè)指標(biāo)超過(guò)一定的閾值時(shí),向指定的接口發(fā)出告警信息仔涩。一切的一切其實(shí)都可以使用Prometheus來(lái)解決忍坷,現(xiàn)在我們可以去了解什么是Prometheus了。
先上一張官方的架構(gòu)圖
從架構(gòu)圖中可以看到熔脂,我們先前驗(yàn)證的node_exporter屬于jobs/exporter部分佩研,稱之為exporter
導(dǎo)出器,是Prometheus主要的指標(biāo)來(lái)源锤悄。
Prometheus Server
是服務(wù)核心組件韧骗,存儲(chǔ)使用時(shí)序數(shù)據(jù)庫(kù)TSDB
將數(shù)據(jù)保存在硬盤上,由于官方對(duì)SSD做了專門的優(yōu)化零聚,所以使用SSD性能會(huì)更優(yōu)袍暴。
配置方式包含多種,可以直接在寫在yaml
文件中隶症,但如果配置較長(zhǎng)也可以寫入其他文件并啟用文件發(fā)現(xiàn)
(file_sd)功能讓其自行偵聽(tīng)配置文件變化政模,甚至可以使用consul或者kubernetes這樣的服務(wù)發(fā)現(xiàn)
來(lái)動(dòng)態(tài)更新配置以適應(yīng)頻繁的節(jié)點(diǎn)變更。
Prometheus使用pull
模型從節(jié)點(diǎn)暴露出來(lái)的端口拉取配置蚂会,這相比push方式更容易避免節(jié)點(diǎn)異常帶來(lái)的干擾和繁瑣的工作淋样。
pushgateway
類似于一個(gè)中轉(zhuǎn)站,Prometheus的server端只會(huì)使用pull方式拉取數(shù)據(jù)胁住,但是某些節(jié)點(diǎn)因?yàn)槟承┰蛑荒苁褂胮ush方式推送數(shù)據(jù)趁猴,那么它就是用來(lái)接收push而來(lái)的數(shù)據(jù)并暴露給Prometheus的server拉取的中轉(zhuǎn)站刊咳。
Prometheus支持告警系統(tǒng),自帶的alertmanager
可以通過(guò)在配置文件中添加規(guī)則的方式儡司,計(jì)算并發(fā)出警報(bào)娱挨,它支持多種發(fā)送方式比如pagerduty、Email等等捕犬。
對(duì)于已經(jīng)存儲(chǔ)的歷史數(shù)據(jù)跷坝,Prometheus提供了PromQL
語(yǔ)言進(jìn)行查詢,并自帶了一個(gè)簡(jiǎn)易的UI界面碉碉,可以在界面上進(jìn)行查詢柴钻、繪圖、查看配置垢粮、告警等等贴届。
架構(gòu)圖中,Grafana等其他API客戶端可以通過(guò)PromQL查詢語(yǔ)言查詢Prometheus中存儲(chǔ)的數(shù)據(jù)足丢。在這里粱腻,大部分用戶喜歡將Grafana與Prometheus配合使用,因?yàn)镻rometheus自帶的UI界面太過(guò)簡(jiǎn)陋斩跌,而Grafana提供的界面非常的美觀绍些,且可以長(zhǎng)期保留。
話休繁緒耀鸦,跑一個(gè)測(cè)試看看
Prometheus的官方下載地址為 https://prometheus.io/download/
我們另起一個(gè)ssh連接到服務(wù)器柬批,選擇一個(gè)版本下載并啟動(dòng)
cd /opt
sudo wget https://github.com/prometheus/prometheus/releases/download/v2.6.0/prometheus-2.6.0.linux-amd64.tar.gz
sudo tar -zxvf prometheus-2.6.0.linux-amd64.tar.gz
cd prometheus-2.6.0.linux-amd64/
編輯配置文件sudo vim prometheus.yml
在scrape_configs欄添加如下配置
- job_name: "node"
static_configs:
- targets: ["localhost:9100"]
此時(shí)的配置文件加上原來(lái)已經(jīng)默認(rèn)存在的,如下(可能因?yàn)榘姹静煌煌?/p>
#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: "node"
static_configs:
- targets: ["localhost:9100"]
yaml文件要尤其注意縮頸袖订,這里我默認(rèn)你已經(jīng)懂得了yaml文件的規(guī)則氮帐。
啟動(dòng)Prometheus
sudo ./prometheus
此時(shí)Prometheus的輸出如下
level=info ts=2018-12-29T08:51:26.126230419Z caller=main.go:243 msg="Starting Prometheus" version="(version=2.6.0, branch=HEAD, revision=dbd1d58c894775c0788470944b818cc724f550fb)"
level=info ts=2018-12-29T08:51:26.126302838Z caller=main.go:244 build_context="(go=go1.11.3, user=root@bf5760470f13, date=20181217-15:14:46)"
level=info ts=2018-12-29T08:51:26.126419034Z caller=main.go:245 host_details="(Linux 4.4.0-132-generic #158-Ubuntu SMP Thu Aug 2 09:08:04 UTC 2018 x86_64 master1 (none))"
level=info ts=2018-12-29T08:51:26.126497022Z caller=main.go:246 fd_limits="(soft=1000000, hard=1000000)"
level=info ts=2018-12-29T08:51:26.126567171Z caller=main.go:247 vm_limits="(soft=unlimited, hard=unlimited)"
level=info ts=2018-12-29T08:51:26.127332045Z caller=main.go:561 msg="Starting TSDB ..."
level=info ts=2018-12-29T08:51:26.127414745Z caller=web.go:429 component=web msg="Start listening for connections" address=0.0.0.0:9090
level=info ts=2018-12-29T08:51:26.135164421Z caller=main.go:571 msg="TSDB started"
level=info ts=2018-12-29T08:51:26.135204665Z caller=main.go:631 msg="Loading configuration file" filename=prometheus.yml
level=info ts=2018-12-29T08:51:26.136207062Z caller=main.go:657 msg="Completed loading of configuration file" filename=prometheus.yml
level=info ts=2018-12-29T08:51:26.136232588Z caller=main.go:530 msg="Server is ready to receive web requests."
最后一行顯示Server is ready to receive web requests.
,也就是服務(wù)已經(jīng)準(zhǔn)備好了洛姑。我們?cè)L問(wèn)鏈接
http://10.19.70.139:9090
在首頁(yè)可以看到Prometheus自帶的UI界面如下
從界面中可以看到上沐,標(biāo)題欄Graph鏈接到圖形查詢界面,在輸入框中允許輸入查詢語(yǔ)句楞艾,點(diǎn)擊execute執(zhí)行参咙,insert metric at cursor下拉列表中列舉了當(dāng)前所有的存在的指標(biāo)可以不用輸入而直接選擇。
往下硫眯,默認(rèn)的console欄表示查詢結(jié)果以文字的形式顯示蕴侧,而Graph欄表示繪制圖表。
我們?cè)囈幌铝饺耄谳斎肟蛑休斎胂惹暗闹笜?biāo)名node_cpu_seconds_total
單機(jī)Execute净宵。
可以看到很多指標(biāo)被列舉了出來(lái)
為了更精確的查詢,我們輸入node_cpu_seconds_total{cpu="0",mode="idle"}
可以看到很精確的查詢結(jié)果被展示出來(lái)
node_cpu_seconds_total{cpu="0",instance="localhost:9100",job="node",mode="idle"} 1420025.1
為了方便我直接拷貝出來(lái)了,可以看到择葡,指標(biāo)新增了很多的標(biāo)簽
比如instance就表示IP地址和端口號(hào)紧武,是配置文件中輸入的
點(diǎn)擊Graph查看圖形
可以看到圖形一直處于增長(zhǎng)狀態(tài),這是合理的刁岸,因?yàn)樗莄ounter類型脏里,而且我們還沒(méi)有做任何運(yùn)算。
這個(gè)圖本身對(duì)開(kāi)發(fā)者或者用戶而言并沒(méi)有什么作用可言虹曙,但是想象一下,如果我們截取每一分鐘內(nèi)變化的數(shù)據(jù)番舆,將其除以60秒酝碳,是否可以得到每秒中CPU0空閑率。這么理解恨狈,如果在某段時(shí)間計(jì)算出來(lái)的值越高疏哗,則說(shuō)明CPU0的使用率越低,而數(shù)值越低禾怠,說(shuō)明CPU0利用率越高返奉,也就是越繁忙。
在搜索欄鍵入rate(node_cpu_seconds_total{cpu="0",mode="idle"}[1m])
這段的意思為選取1分鐘內(nèi)的數(shù)據(jù)的差值以60秒吗氏,rate函數(shù)就是用來(lái)做這個(gè)事情的芽偏。
得到以下圖形
可以看出,我這臺(tái)服務(wù)器的CPU0一直還是比較閑的
當(dāng)然也可以使用稍微復(fù)雜一點(diǎn)的公式計(jì)算出整個(gè)CPU的工作狀態(tài)
鍵入
(((count(count(node_cpu_seconds_total) by (cpu))) - avg(sum by (mode)(irate(node_cpu_seconds_total{mode='idle'}[5m])))) * 100) / count(count(node_cpu_seconds_total) by (cpu))
這段查詢語(yǔ)句表示計(jì)算出CPU所有核數(shù)的busy狀態(tài)總和在整個(gè)CPU時(shí)間的占比弦讽。從圖中可以看到CPU時(shí)而忙碌時(shí)而悠閑污尉。
在targets面板,我們可以查看所有配置往产,如果該配置能夠成功拉取數(shù)據(jù)被碗,則為狀態(tài)為綠色UP,否則為紅色DOWN仿村。如果配置了alert規(guī)則锐朴,還可以在Alert面板查看到他們。
我這里就不一一演示了蔼囊。
配置焚志、存儲(chǔ)、圖表與告警
現(xiàn)在我們已經(jīng)可以利用Prometheus進(jìn)行數(shù)據(jù)拉取压真,并且繪制圖形了娩嚼,但是對(duì)于生產(chǎn)環(huán)境中面臨的情況來(lái)說(shuō),完全不夠滴肿。我們要可配置岳悟、可存儲(chǔ)、隨時(shí)查看圖表、指定閾值告警等等贵少。
為了與本機(jī)環(huán)境隔離呵俏,我將使用docker進(jìn)行后續(xù)的演示,在這里我默認(rèn)你已有了docker的基本知識(shí)以及可以使用docker-compose進(jìn)行服務(wù)編排滔灶。
為了長(zhǎng)期存儲(chǔ)普碎,我們需要指定Prometheus的存儲(chǔ)目錄
為了繪圖與保留圖形以便二次查看,我們引入Grafana作為儀表板
對(duì)于告警录平,由于Prometheus自帶的告警配置相對(duì)復(fù)雜麻车,這里我使用了Grafana所提供的告警,它的告警很簡(jiǎn)單斗这,所見(jiàn)即所得动猬,拖動(dòng)滑塊就可以配置閾值。
關(guān)閉先前啟動(dòng)的prometheus
(測(cè)試的時(shí)候我換了虛擬機(jī)表箭,可能后續(xù)IP地址會(huì)有變化)
在/opt目錄下建立配置目錄赁咙,為方便操作我切換到管理員權(quán)限
sudo su
cd /opt
mkdir -p prometheus/config/
mkdir -p grafana/data
chmod 777 grafana/data
mkdir -p /data/prometheus
chmod 777 /data/prometheus
編輯docker-compose.yml
文件并鍵入
version: '2'
networks:
monitor:
driver: bridge
services:
prometheus:
image: prom/prometheus:latest
container_name: prometheus
hostname: prometheus
restart: always
volumes:
- /opt/prometheus/config:/etc/prometheus
- /data/prometheus:/prometheus
ports:
- "9090:9090"
expose:
- "8086"
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--log.level=info'
- '--web.listen-address=0.0.0.0:9090'
- '--storage.tsdb.path=/prometheus'
- '--storage.tsdb.retention=15d'
- '--query.max-concurrency=50'
networks:
- monitor
grafana:
image: grafana/grafana:latest
container_name: grafana
hostname: grafana
restart: always
volumes:
- /opt/grafana/data:/var/lib/grafana
ports:
- "3000:3000"
- "25:25"
networks:
- monitor
depends_on:
- prometheus
拷貝先前的配置文件prometheus.yml到/opt/prometheus/config目錄下并替換所有l(wèi)ocalhost為IP地址。
運(yùn)行docker-compose up
啟動(dòng)容器
此時(shí)可以看到docker-compose打印的日志部分如下
prometheus | level=info ts=2019-01-28T03:43:24.580746431Z caller=web.go:429 component=web msg="Start listening for connections" address=0.0.0.0:9090
grafana | t=2019-01-28T03:43:25+0000 lvl=info msg="HTTP Server Listen" logger=http.server address=0.0.0.0:3000 protocol=http subUrl= socket=
為了確保Prometheus可以順利拉取node_exporter上的數(shù)據(jù)免钻,我們?nèi)孕枰獧z查下它的UI界面彼水,訪問(wèn)http://192.168.112.129:9090/targets
,選擇targets。
如下圖极舔,status為UP凤覆,點(diǎn)擊前面的URL可以順利訪問(wèn)到詳細(xì)信息則表示OK。
此時(shí)訪問(wèn)http://10.19.70.139:3000/
就可以看到Grafana的登錄界面
初始密碼admin/admin
那么什么是Grafana
Grafana是一個(gè)跨平臺(tái)的開(kāi)源的度量分析和可視化工具姆怪,可以通過(guò)將采集的數(shù)據(jù)查詢?nèi)缓罂梢暬恼故九炎⒓皶r(shí)通知。它主要有以下六大特點(diǎn):
1稽揭、展示方式:快速靈活的客戶端圖表俺附,面板插件有許多不同方式的可視化指標(biāo)和日志,官方庫(kù)中具有豐富的儀表盤插件溪掀,比如熱圖事镣、折線圖、圖表等多種展示方式揪胃;
2璃哟、數(shù)據(jù)源:Graphite,InfluxDB喊递,OpenTSDB随闪,Prometheus,Elasticsearch骚勘,CloudWatch和KairosDB等铐伴;
3撮奏、通知提醒:以可視方式定義最重要指標(biāo)的警報(bào)規(guī)則,Grafana將不斷計(jì)算并發(fā)送通知当宴,在數(shù)據(jù)達(dá)到閾值時(shí)通過(guò)Slack畜吊、PagerDuty等獲得通知;
4户矢、混合展示:在同一圖表中混合使用不同的數(shù)據(jù)源玲献,可以基于每個(gè)查詢指定數(shù)據(jù)源,甚至自定義數(shù)據(jù)源梯浪;
5捌年、注釋:使用來(lái)自不同數(shù)據(jù)源的豐富事件注釋圖表,將鼠標(biāo)懸停在事件上會(huì)顯示完整的事件元數(shù)據(jù)和標(biāo)記驱证;
6延窜、過(guò)濾器:Ad-hoc過(guò)濾器允許動(dòng)態(tài)創(chuàng)建新的鍵/值過(guò)濾器,這些過(guò)濾器會(huì)自動(dòng)應(yīng)用于使用該數(shù)據(jù)源的所有查詢抹锄。
注:偷個(gè)懶,上面這段Grafana的介紹來(lái)自別人的博文可視化工具Grafana:簡(jiǎn)介及安裝荠藤,有興趣可以看看伙单。
進(jìn)入初始界面之后會(huì)看到這樣的家目錄
稍作解釋,中間的一欄
Install Grafana哈肖、Add data source吻育、Create your first dashboard等等是操作流程,表示已經(jīng)完成了哪些操作以及進(jìn)度淤井,從左往右布疼。
添加數(shù)據(jù)源
點(diǎn)擊 Add data source
,選擇Prometheus,在URL輸入框鍵入http://192.168.112.129:9090
币狠,點(diǎn)擊save & test
游两,如果出現(xiàn)下圖中的綠色提示,則表示配置有效漩绵,否則可能是地址或者端口等其他錯(cuò)誤贱案,需要自行修改。
點(diǎn)擊左側(cè)的Home回到家目錄
接下來(lái)是create一個(gè)Dashboard止吐,這里宝踪,如果需要自定義一些圖表,則可以手動(dòng)添加碍扔。然而我們的node_exporter是開(kāi)源的項(xiàng)目瘩燥,而且也有他的開(kāi)源的對(duì)應(yīng)的圖表,那么我們可以直接使用開(kāi)源的圖表以簡(jiǎn)化操作〔煌現(xiàn)在我們來(lái)看看如何搜索和添加開(kāi)源的圖表厉膀。(對(duì)于很多需要監(jiān)控的應(yīng)用比如mysql、kafka等等,都可以去網(wǎng)上尋找開(kāi)源的exporter和相應(yīng)的dashboard圖表,比如Prometheus的開(kāi)源地址站蝠,【Default port allocations】)
單機(jī)左上角的Home
選擇搜索功能
我們選擇數(shù)據(jù)源為Prometheus
并鍵入node_exporter
使用Enter
鍵開(kāi)始搜索汰具。
從搜索結(jié)果中選擇一條,比如第一條菱魔,單機(jī)
界面中可以看到關(guān)于這個(gè)圖表的簡(jiǎn)介留荔,點(diǎn)擊copy它的ID。
回到原先的Home界面澜倦,選擇import dashboard
在Grafana.com Dashboard欄粘貼先前拷貝的ID
它會(huì)自動(dòng)識(shí)別出dashboard的相關(guān)信息
我們需要選擇它的數(shù)據(jù)源聚蝶,這里是Prometheus,然后Import即可
在這個(gè)面板上藻治,Name可以修改為自己想要的碘勉,F(xiàn)older可以對(duì)該dashboard分類存放,其他也可以看到作者信息等桩卵。
添加完畢會(huì)自動(dòng)來(lái)到dashboard頁(yè)面【該dashboard需要node_exporter0.16以上版本验靡,如果不是可以更換node_exporter版本或者選擇其他dashboard】
在這里,我重新挑選了一個(gè)ID為5174
的dashboard這是我最喜歡的的node_exporter版本雏节。
import之后胜嗓,在主界面此時(shí)并沒(méi)有圖像顯示出來(lái)。點(diǎn)擊右上角的設(shè)置钩乍,選擇Variables
選擇第一行 $node ,替換node_boot_time_seconds
為up
辞州,此時(shí)可以在最下方看到配置的主機(jī)IP地址,也有可能是localhost,最后選擇Update
依葫蘆畫瓢寥粹,替換$port
,中的參數(shù)為label_values(up, instance)
变过。
這里注意一點(diǎn),$port的編輯界面中涝涤,Hide參數(shù)先選擇為空(如果默認(rèn)為variable的話)媚狰,否則port參數(shù)在主界面就看不到了棍丐。
點(diǎn)擊左側(cè)save之后仍劈,選擇回到dashboard主界面。
此時(shí)兴蒸,node_exporter的數(shù)據(jù)就以圖表的形式展示出來(lái)了衫生。
將系統(tǒng)指標(biāo)以圖形的形式展示出來(lái)裳瘪,這無(wú)疑是有趣且實(shí)用的。
現(xiàn)在依然有一些問(wèn)題
比如Prometheus的存儲(chǔ)數(shù)據(jù)庫(kù)默認(rèn)只保留15天的數(shù)據(jù)罪针,Grafana存儲(chǔ)的配置以及圖表都還在容器之中它自己生成的sqlit數(shù)據(jù)庫(kù)中長(zhǎng)期存儲(chǔ)并不友好彭羹。
為了更適應(yīng)老板的需求,我們可以添加時(shí)序數(shù)據(jù)庫(kù)InfluxDB作為后端存儲(chǔ)泪酱,可以修改grafana的存儲(chǔ)為mysql以便自己做一些自定義操作的時(shí)候方便一點(diǎn)派殷,添加pushgateway接收來(lái)自腳本推送的指標(biāo)还最。這時(shí)我們的yml文件可以是這個(gè)樣子的.
version: '2'
networks:
monitor:
driver: bridge
services:
prometheus:
image: prom/prometheus:latest
container_name: prometheus
hostname: prometheus
restart: always
volumes:
- /home/ubuntu/monitors/prometheus/config:/etc/prometheus
- /data/monitors/prometheus_data:/prometheus
ports:
- "9090:9090"
expose:
- "8086"
depends_on:
- influxdb
#environment:
#- storage.tsdb.retention=168h0m0s
#- storage.tsdb.retention=15d
#- log.level=debug
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--log.level=info'
- '--web.listen-address=0.0.0.0:9090'
- '--storage.tsdb.path=/prometheus'
- '--storage.tsdb.retention=15d'
- '--query.max-concurrency=50'
logging:
driver: "json-file"
options:
max-size: "1g"
networks:
- monitor
grafana:
image: grafana/grafana:latest
container_name: grafana
hostname: grafana
restart: always
volumes:
- /home/ubuntu/monitors/grafana/config:/etc/grafana
- /home/ubuntu/monitors/grafana/logs:/var/log/grafana
- /home/ubuntu/monitors/grafana/data:/var/lib/grafana
- /home/ubuntu/monitors/grafana/dashboards:/etc/grafana/provisioning/dashboards
ports:
- "3000:3000"
- "25:25"
- "465:465"
user: "104"
networks:
- monitor
depends_on:
- db
db:
image: mysql:5.7
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: root
hostname: mysql
restart: always
volumes:
- /home/ubuntu/monitors/mysql/config:/etc/mysql
- /home/ubuntu/monitors/mysql/data:/var/lib/mysql
- /home/ubuntu/monitors/mysql/log:/var/log/mysql
networks:
- monitor
pushgateway:
image: prom/pushgateway:latest
container_name: pushgateway
hostname: pushgateway
restart: always
ports:
- "9091:9091"
networks:
- monitor
influxdb:
image: influxdb:latest
container_name: influxdb
hostname: influxdb
restart: always
volumes:
- /home/ubuntu/monitors/influxdb/config:/etc/influxdb
- /data/monitors/influxdb/data:/root/.influxdb
#command: -config /etc/influxdb/influxdb.conf
ports:
- "8086:8086"
- "8083:8083"
expose:
- "8086"
environment:
- INFLUXDB_DB=prometheus
- INFLUXDB_ADMIN_ENABLED=true
- INFLUXDB_ADMIN_USER=admin
- INFLUXDB_ADMIN_PASSWORD=admin
- INFLUXDB_USER=prom
- INFLUXDB_USER_PASSWORD=prom
- INFLUXDB_CONFIG_PATH=/etc/influxdb/influxdb.conf
networks:
- monitor
logging:
driver: "json-file"
options:
max-size: "1g"
從文件描述中可以看出,我重新指定了Prometheus的存儲(chǔ)數(shù)據(jù)庫(kù)為InfluxDB毡惜,指定Grafana存儲(chǔ)圖表的數(shù)據(jù)庫(kù)為熟悉的MySQL拓轻,設(shè)置日志不超過(guò)1G等,當(dāng)然经伙,這些也需要Prometheus和Grafana的配合扶叉。
在Prometheus的配置文件中,添加遠(yuǎn)程讀寫帕膜。
scrape_configs:
- job_name: mx-discovery
file_sd_configs:
- files:
- '/etc/prometheus/fileconfig/mx-nodes.json'
remote_write:
- url: "http://influxdb:8086/api/v1/prom/write?db=prometheus&u=prom&p=prom"
remote_read:
- url: "http://influxdb:8086/api/v1/prom/read?db=prometheus&u=prom&p=prom"
file_sd_configs表示啟動(dòng)prometheus的文件發(fā)現(xiàn)功能枣氧。也就是說(shuō),可以將配置主機(jī)等信息寫入文件中垮刹,如果用戶修改了配置达吞,比如增加或刪除了某些主機(jī),那么Prometheus會(huì)自動(dòng)發(fā)現(xiàn)這些更改而不用每次reload甚至重啟荒典。
所以文件可以是這樣的
[
{
"targets": ["192.168.1.8:9100"],
"labels": {
"alias": "test1",
"job": "node"
}
},
{
"targets": ["192.168.1.9:9100"],
"labels": {
"alias": "test2",
"job": "node"
}
}
]
還有Grafana的配置(如果找不到配置酪劫,或者容器無(wú)法將配置映射出來(lái),那么直接進(jìn)入容器拷貝一份出來(lái)即可)
[database]
# You can configure the database connection by specifying type, host, name, user and password
# as separate properties or as on string using the url properties.
type = mysql
host = mysql:3306
name = grafana
user = grafana
password =grafana
url = mysql://grafana:grafana@mysql:3306/grafana
[session]
# Either "memory", "file", "redis", "mysql", "postgres", default is "file"
#provider = file
provider = mysql
provider_config = `grafana:grafana@tcp(mysql:3306)/grafana`
關(guān)于了解入門寺董,我就只介紹這么多了契耿,更多的可以參考官網(wǎng)或者其他一些更專業(yè)的博文。
附錄
Prometheus官方文檔英文
Grafana官方文檔英文
prometheus + grafana安裝部署(centos6.8)
Prometheus入門與實(shí)踐
全面學(xué)習(xí)Prometheus
Prometheus 非官方中文手冊(cè)
Prometheus實(shí)戰(zhàn)
Prometheus-book
大米哥-Prometheus普羅米修斯監(jiān)控-合集專題付費(fèi)視頻專題
Grafana的一些實(shí)用技巧
Prometheus:Up & Running這本書對(duì)Prometheus介紹的很系統(tǒng)螃征,可以用來(lái)學(xué)習(xí)提高,也可以用來(lái)當(dāng)工具書透敌,不過(guò)也是英文的盯滚,國(guó)內(nèi)是可以下載到的。(我放的這個(gè)地址注冊(cè)后可以免費(fèi)看幾天酗电,作為了解)魄藕。