APM - Prometheus监控系统初探
文章目錄
- Wiki
- 時(shí)序數(shù)據(jù)庫(kù) TSDB(Time Series Database)
- 概述
- 下載&安裝 Prometheus server
- 二進(jìn)制文件的方式
- 【 查看版本信息 】
- 【運(yùn)行 Prometheus server】
- Docker方式
- 拉取鏡像 啟動(dòng)容器
- 查看prometheus 的配置信息
- 啟動(dòng)容器 指定配置文件的位置
- Prometheus配置文件
- PromQL初探
Wiki
https://en.wikipedia.org/wiki/Prometheus_(software)
Prometheus 是一款基于時(shí)序數(shù)據(jù)庫(kù)的開源監(jiān)控告警系統(tǒng).
說(shuō)起 Prometheus 則不得不提 SoundCloud, 在線音樂(lè)分享的平臺(tái), 微服務(wù)架構(gòu)出現(xiàn)了成百上千的服務(wù),使用傳統(tǒng)的監(jiān)控系統(tǒng) StatsD 和 Graphite 存在大量的局限性,于是他們?cè)?2012 年開始著手開發(fā)一套全新的監(jiān)控系統(tǒng)。
Prometheus 的原作者是 Matt T. Proud,他也是在 2012 年加入 SoundCloud 的, 在加入 SoundCloud 之前,Matt 一直就職于 Google,他從 Google 的集群管理器 Borg 和它的監(jiān)控系統(tǒng) Borgmon 中獲取靈感,開發(fā)了開源的監(jiān)控系統(tǒng) Prometheus,和 Google 的很多項(xiàng)目一樣,使用的編程語(yǔ)言是 Go。
Prometheus 作為一個(gè)微服務(wù)架構(gòu)監(jiān)控系統(tǒng)的解決方案,它和容器也脫不開關(guān)系。早在 2006 年 8 月 9 日,Eric Schmidt 在搜索引擎大會(huì)上首次提出了云計(jì)算(Cloud Computing)的概念,在之后的十幾年里,云計(jì)算的發(fā)展勢(shì)如破竹。
在 2013 年,Pivotal 的 Matt Stine 又提出了云原生(Cloud Native)的概念,云原生由微服務(wù)架構(gòu)、DevOps 和以容器為代表的敏捷基礎(chǔ)架構(gòu)組成,幫助企業(yè)快速、持續(xù)、可靠、規(guī)?;亟桓盾浖?。
為了統(tǒng)一云計(jì)算接口和相關(guān)標(biāo)準(zhǔn),2015 年 7 月,隸屬于 Linux 基金會(huì)的 云原生計(jì)算基金會(huì)(CNCF,Cloud Native Computing Foundation) 應(yīng)運(yùn)而生。第一個(gè)加入 CNCF 的項(xiàng)目是 Google 的 Kubernetes,而 Prometheus 是第二個(gè)加入的(2016 年)。
目前 Prometheus 已經(jīng)廣泛用于 Kubernetes 集群的監(jiān)控系統(tǒng)中,對(duì) Prometheus 的歷史感興趣的同學(xué)可以看看 SoundCloud 的工程師 Tobias Schmidt 在 2016 年的 PromCon 大會(huì)上的演講:The History of Prometheus at SoundCloud 。
時(shí)序數(shù)據(jù)庫(kù) TSDB(Time Series Database)
Prometheus是監(jiān)控系統(tǒng),同時(shí)也是一個(gè)TSDB。 很多流行的監(jiān)控系統(tǒng)都在使用時(shí)序數(shù)據(jù)庫(kù)來(lái)保存數(shù)據(jù), 時(shí)序數(shù)據(jù)庫(kù)的特點(diǎn)主要有如下幾個(gè)方面:
常見(jiàn)的時(shí)序數(shù)據(jù)庫(kù)有以下幾個(gè)
- InfluxDB:https://influxdata.com/
- Kdb+:http://kx.com/
- Graphite:http://graphiteapp.org/
- RRDtool:http://oss.oetiker.ch/rrdtool/
- OpenTSDB:http://opentsdb.net/
- Prometheus:https://prometheus.io/
- Druid:http://druid.io/
概述
在 SoundCloud 的官方博客中可以找到一篇關(guān)于他們?yōu)槭裁葱枰麻_發(fā)一個(gè)監(jiān)控系統(tǒng)的文章 Prometheus: Monitoring at SoundCloud,在這篇文章中,他們介紹到,他們需要的監(jiān)控系統(tǒng)必須滿足下面四個(gè)特性:
- 多維度數(shù)據(jù)模型
- 方便的部署和維護(hù)
- 靈活的數(shù)據(jù)采集
- 強(qiáng)大的查詢語(yǔ)言
實(shí)際上,多維度數(shù)據(jù)模型和強(qiáng)大的查詢語(yǔ)言這兩個(gè)特性,正是時(shí)序數(shù)據(jù)庫(kù)所要求的,所以 Prometheus 不僅僅是一個(gè)監(jiān)控系統(tǒng),同時(shí)也是一個(gè)時(shí)序數(shù)據(jù)庫(kù)。
為什么 Prometheus 不直接使用現(xiàn)有的時(shí)序數(shù)據(jù)庫(kù)作為后端存儲(chǔ)呢?這是因?yàn)?SoundCloud 不僅希望他們的監(jiān)控系統(tǒng)有著時(shí)序數(shù)據(jù)庫(kù)的特點(diǎn),而且還需要部署和維護(hù)非常方便。
縱觀比較流行的時(shí)序數(shù)據(jù)庫(kù) 要么組件太多,要么外部依賴繁重,比如:Druid 有 Historical、MiddleManager、Broker、Coordinator、Overlord、Router 一堆的組件,而且還依賴于 ZooKeeper、Deep storage(HDFS 或 S3 等),Metadata store(PostgreSQL 或 MySQL),部署和維護(hù)起來(lái)成本非常高。
而 Prometheus 采用去中心化架構(gòu),可以獨(dú)立部署,不依賴于外部的分布式存儲(chǔ),我們可以在幾分鐘的時(shí)間里就可以搭建出一套監(jiān)控系統(tǒng)。
此外,Prometheus 數(shù)據(jù)采集方式也非常靈活。要采集目標(biāo)的監(jiān)控?cái)?shù)據(jù),首先需要在目標(biāo)處安裝數(shù)據(jù)采集組件,這被稱之為 Exporter,它會(huì)在目標(biāo)處收集監(jiān)控?cái)?shù)據(jù),并暴露出一個(gè) HTTP 接口供 Prometheus 查詢,Prometheus 通過(guò) Pull 的方式來(lái)采集數(shù)據(jù),這和傳統(tǒng)的 Push 模式不同。
不過(guò) Prometheus 也提供了一種方式來(lái)支持 Push 模式,我們可以將數(shù)據(jù)推送到 Push Gateway,Prometheus 通過(guò) Pull 的方式從 Push Gateway 獲取數(shù)據(jù)。(如果消息存在的周期很短,pull的時(shí)候可能沒(méi)了,可以考慮這種方式)。
Prometheus主要是一個(gè)基于拉取的系統(tǒng),但它也支持接收推送到網(wǎng)關(guān)的事件.
目前的 Exporter 已經(jīng)可以采集絕大多數(shù)的第三方數(shù)據(jù),比如 Docker、HAProxy、StatsD、JMX 等等,官網(wǎng)有一份 Exporter 的列表。
除了這四大特性,隨著 Prometheus 的不斷發(fā)展,開始支持越來(lái)越多的高級(jí)特性,比如:服務(wù)發(fā)現(xiàn),更豐富的圖表展示,使用外部存儲(chǔ),強(qiáng)大的告警規(guī)則和多樣的通知方式。
下圖是 Prometheus 的整體架構(gòu)圖
從上圖可以看出,Prometheus 生態(tài)系統(tǒng)包含了幾個(gè)關(guān)鍵的組件:
- Prometheus server、
- Pushgateway、
- Alertmanager、
- Web UI 等,
但是大多數(shù)組件都不是必需的,其中最核心的組件當(dāng)然是 Prometheus server,它負(fù)責(zé)收集和存儲(chǔ)指標(biāo)數(shù)據(jù),支持表達(dá)式查詢,和告警的生成。接下來(lái)我們就來(lái)安裝 Prometheus server。
下載&安裝 Prometheus server
二進(jìn)制文件的方式
https://github.com/prometheus/prometheus/releases
按需選擇對(duì)應(yīng)的操作系統(tǒng) ,下載即可
【 查看版本信息 】
[root@VM-0-7-centos ~]# cd prometheus-2.30.3.linux-amd64/ [root@VM-0-7-centos prometheus-2.30.3.linux-amd64]# ll total 185580 drwxr-xr-x 2 3434 3434 4096 Oct 6 00:39 console_libraries drwxr-xr-x 2 3434 3434 4096 Oct 6 00:39 consoles -rw-r--r-- 1 3434 3434 11357 Oct 6 00:39 LICENSE -rw-r--r-- 1 3434 3434 3646 Oct 6 00:39 NOTICE -rwxr-xr-x 1 3434 3434 100357256 Oct 6 00:14 prometheus -rw-r--r-- 1 3434 3434 934 Oct 6 00:39 prometheus.yml -rwxr-xr-x 1 3434 3434 89643838 Oct 6 00:17 promtool [root@VM-0-7-centos prometheus-2.30.3.linux-amd64]# ./prometheus --version 查看版本信息 prometheus, version 2.30.3 (branch: HEAD, revision: f29caccc42557f6a8ec30ea9b3c8c089391bd5df)build user: root@5cff4265f0e3build date: 20211005-16:10:52go version: go1.17.1platform: linux/amd64 [root@VM-0-7-centos prometheus-2.30.3.linux-amd64]#【運(yùn)行 Prometheus server】
前臺(tái)運(yùn)行哈,下面這種方式
[root@VM-0-7-centos prometheus-2.30.3.linux-amd64]# ./prometheus --config.file=prometheus.yml level=info ts=2021-10-27T01:17:19.068Z caller=main.go:400 msg="No time or size retention was set so using the default time retention" duration=15d level=info ts=2021-10-27T01:17:19.068Z caller=main.go:438 msg="Starting Prometheus" version="(version=2.30.3, branch=HEAD, revision=f29caccc42557f6a8ec30ea9b3c8c089391bd5df)" level=info ts=2021-10-27T01:17:19.068Z caller=main.go:443 build_context="(go=go1.17.1, user=root@5cff4265f0e3, date=20211005-16:10:52)" level=info ts=2021-10-27T01:17:19.068Z caller=main.go:444 host_details="(Linux 3.10.0-1160.11.1.el7.x86_64 #1 SMP Fri Dec 18 16:34:56 UTC 2020 x86_64 VM-0-7-centos (none))" level=info ts=2021-10-27T01:17:19.068Z caller=main.go:445 fd_limits="(soft=100001, hard=100002)" level=info ts=2021-10-27T01:17:19.068Z caller=main.go:446 vm_limits="(soft=unlimited, hard=unlimited)" level=info ts=2021-10-27T01:17:19.071Z caller=web.go:541 component=web msg="Start listening for connections" address=0.0.0.0:9090 level=info ts=2021-10-27T01:17:19.071Z caller=main.go:822 msg="Starting TSDB ..." level=info ts=2021-10-27T01:17:19.074Z caller=tls_config.go:191 component=web msg="TLS is disabled." http2=false level=info ts=2021-10-27T01:17:19.075Z caller=head.go:479 component=tsdb msg="Replaying on-disk memory mappable chunks if any" level=info ts=2021-10-27T01:17:19.075Z caller=head.go:513 component=tsdb msg="On-disk memory mappable chunks replay completed" duration=15.248μs level=info ts=2021-10-27T01:17:19.075Z caller=head.go:519 component=tsdb msg="Replaying WAL, this may take a while" level=info ts=2021-10-27T01:17:19.077Z caller=head.go:590 component=tsdb msg="WAL segment loaded" segment=0 maxSegment=1 level=info ts=2021-10-27T01:17:19.077Z caller=head.go:590 component=tsdb msg="WAL segment loaded" segment=1 maxSegment=1 level=info ts=2021-10-27T01:17:19.077Z caller=head.go:596 component=tsdb msg="WAL replay completed" checkpoint_replay_duration=30.976μs wal_replay_duration=2.14283ms total_replay_duration=2.213897ms level=info ts=2021-10-27T01:17:19.078Z caller=main.go:849 fs_type=EXT4_SUPER_MAGIC level=info ts=2021-10-27T01:17:19.078Z caller=main.go:852 msg="TSDB started" level=info ts=2021-10-27T01:17:19.078Z caller=main.go:979 msg="Loading configuration file" filename=prometheus.yml level=info ts=2021-10-27T01:17:19.079Z caller=main.go:1016 msg="Completed loading of configuration file" filename=prometheus.yml totalDuration=698.954μs db_storage=797ns remote_storage=3.417μs web_handler=1.953μs query_engine=699ns scrape=348.231μs scrape_sd=31.434μs notify=22.91μs notify_sd=10.579μs rules=4.76μs level=info ts=2021-10-27T01:17:19.079Z caller=main.go:794 msg="Server is ready to receive web requests."Docker方式
https://hub.docker.com/u/prom
拉取鏡像 啟動(dòng)容器
[root@VM-0-7-centos ~]# docker run -d -p 9090:9090 prom/prometheus Unable to find image 'prom/prometheus:latest' locally latest: Pulling from prom/prometheus aa2a8d90b84c: Pull complete b45d31ee2d7f: Pull complete 7579d86a00c9: Pull complete 8583d0bc7e17: Pull complete b32caf1c5e65: Pull complete e53f205885a2: Pull complete 6366df248f46: Pull complete a63db3af7b6e: Pull complete 94cd9f02fa61: Pull complete 2511fa13a76c: Pull complete 50c2584d9f31: Pull complete 22749d939f03: Pull complete Digest: sha256:e9620d250b16ffe0eb9e3eac7dd76151848424c17d577632ae9ca61d1328687e Status: Downloaded newer image for prom/prometheus:latest 20649f85d688c26ae7c6f80e11de5d07337cfc7ffc4f9695e6803ee0f94f8a28 [root@VM-0-7-centos ~]#查看prometheus 的配置信息
查看prometheus 的配置信息,可以使用 docker inspect 命令
啟動(dòng)容器 指定配置文件的位置
通過(guò)-v 指定配置文件,將配置文件存放在宿主機(jī)上,便于管理
[root@VM-0-7-centos pm-config]# pwd /root/pm-config [root@VM-0-7-centos pm-config]# [root@VM-0-7-centos pm-config]# ll prometheus.yml -rw-r--r-- 1 root root 934 Oct 27 09:54 prometheus.yml[root@VM-0-7-centos pm-config]# docker run -d -p 9090:9090 -v /root/pm-config/:/etc/prometheus/ prom/prometheus通過(guò) -v 參數(shù)將本地的配置文件掛載到 /etc/prometheus/ 位置, prometheus 在容器中默認(rèn)加載的配置文件位置。
Prometheus配置文件
我們啟動(dòng)的時(shí)候,可以通過(guò)參數(shù) --config.file 來(lái)指定配置文件
- global 塊
Prometheus 的全局配置,比如 scrape_interval 表示 Prometheus 多久抓取一次數(shù)據(jù),evaluation_interval 表示多久檢測(cè)一次告警規(guī)則;
- alerting 塊
Alertmanager 的配置
- rule_files 塊:
告警規(guī)則
- scrape_config 塊
這里定義了 Prometheus 要抓取的目標(biāo).
默認(rèn)已經(jīng)配置了一個(gè)名稱為 prometheus 的 job, Prometheus 在啟動(dòng)的時(shí)候也會(huì)通過(guò) HTTP 接口暴露自身的指標(biāo)數(shù)據(jù),Prometheus 自己監(jiān)控自己.
可以訪問(wèn) http://localhost:9090/metrics 查看 Prometheus 暴露了哪些指標(biāo);
PromQL初探
Prometheus 提供了可視化的 Web UI 方便我們操作, 訪問(wèn) http://ip:9090/ 默認(rèn)跳轉(zhuǎn)到 Graph 頁(yè)面
總結(jié)
以上是生活随笔為你收集整理的APM - Prometheus监控系统初探的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Docker Review - Dock
- 下一篇: hadoop入门-在windows上编译