APM - Prometheus监控系统初探
文章目錄
- Wiki
- 時序數據庫 TSDB(Time Series Database)
- 概述
- 下載&安裝 Prometheus server
- 二進制文件的方式
- 【 查看版本信息 】
- 【運行 Prometheus server】
- Docker方式
- 拉取鏡像 啟動容器
- 查看prometheus 的配置信息
- 啟動容器 指定配置文件的位置
- Prometheus配置文件
- PromQL初探
Wiki
https://en.wikipedia.org/wiki/Prometheus_(software)
Prometheus 是一款基于時序數據庫的開源監控告警系統.
說起 Prometheus 則不得不提 SoundCloud, 在線音樂分享的平臺, 微服務架構出現了成百上千的服務,使用傳統的監控系統 StatsD 和 Graphite 存在大量的局限性,于是他們在 2012 年開始著手開發一套全新的監控系統。
Prometheus 的原作者是 Matt T. Proud,他也是在 2012 年加入 SoundCloud 的, 在加入 SoundCloud 之前,Matt 一直就職于 Google,他從 Google 的集群管理器 Borg 和它的監控系統 Borgmon 中獲取靈感,開發了開源的監控系統 Prometheus,和 Google 的很多項目一樣,使用的編程語言是 Go。
Prometheus 作為一個微服務架構監控系統的解決方案,它和容器也脫不開關系。早在 2006 年 8 月 9 日,Eric Schmidt 在搜索引擎大會上首次提出了云計算(Cloud Computing)的概念,在之后的十幾年里,云計算的發展勢如破竹。
在 2013 年,Pivotal 的 Matt Stine 又提出了云原生(Cloud Native)的概念,云原生由微服務架構、DevOps 和以容器為代表的敏捷基礎架構組成,幫助企業快速、持續、可靠、規模化地交付軟件。
為了統一云計算接口和相關標準,2015 年 7 月,隸屬于 Linux 基金會的 云原生計算基金會(CNCF,Cloud Native Computing Foundation) 應運而生。第一個加入 CNCF 的項目是 Google 的 Kubernetes,而 Prometheus 是第二個加入的(2016 年)。
目前 Prometheus 已經廣泛用于 Kubernetes 集群的監控系統中,對 Prometheus 的歷史感興趣的同學可以看看 SoundCloud 的工程師 Tobias Schmidt 在 2016 年的 PromCon 大會上的演講:The History of Prometheus at SoundCloud 。
時序數據庫 TSDB(Time Series Database)
Prometheus是監控系統,同時也是一個TSDB。 很多流行的監控系統都在使用時序數據庫來保存數據, 時序數據庫的特點主要有如下幾個方面:
常見的時序數據庫有以下幾個
- 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 的官方博客中可以找到一篇關于他們為什么需要新開發一個監控系統的文章 Prometheus: Monitoring at SoundCloud,在這篇文章中,他們介紹到,他們需要的監控系統必須滿足下面四個特性:
- 多維度數據模型
- 方便的部署和維護
- 靈活的數據采集
- 強大的查詢語言
實際上,多維度數據模型和強大的查詢語言這兩個特性,正是時序數據庫所要求的,所以 Prometheus 不僅僅是一個監控系統,同時也是一個時序數據庫。
為什么 Prometheus 不直接使用現有的時序數據庫作為后端存儲呢?這是因為 SoundCloud 不僅希望他們的監控系統有著時序數據庫的特點,而且還需要部署和維護非常方便。
縱觀比較流行的時序數據庫 要么組件太多,要么外部依賴繁重,比如:Druid 有 Historical、MiddleManager、Broker、Coordinator、Overlord、Router 一堆的組件,而且還依賴于 ZooKeeper、Deep storage(HDFS 或 S3 等),Metadata store(PostgreSQL 或 MySQL),部署和維護起來成本非常高。
而 Prometheus 采用去中心化架構,可以獨立部署,不依賴于外部的分布式存儲,我們可以在幾分鐘的時間里就可以搭建出一套監控系統。
此外,Prometheus 數據采集方式也非常靈活。要采集目標的監控數據,首先需要在目標處安裝數據采集組件,這被稱之為 Exporter,它會在目標處收集監控數據,并暴露出一個 HTTP 接口供 Prometheus 查詢,Prometheus 通過 Pull 的方式來采集數據,這和傳統的 Push 模式不同。
不過 Prometheus 也提供了一種方式來支持 Push 模式,我們可以將數據推送到 Push Gateway,Prometheus 通過 Pull 的方式從 Push Gateway 獲取數據。(如果消息存在的周期很短,pull的時候可能沒了,可以考慮這種方式)。
Prometheus主要是一個基于拉取的系統,但它也支持接收推送到網關的事件.
目前的 Exporter 已經可以采集絕大多數的第三方數據,比如 Docker、HAProxy、StatsD、JMX 等等,官網有一份 Exporter 的列表。
除了這四大特性,隨著 Prometheus 的不斷發展,開始支持越來越多的高級特性,比如:服務發現,更豐富的圖表展示,使用外部存儲,強大的告警規則和多樣的通知方式。
下圖是 Prometheus 的整體架構圖
從上圖可以看出,Prometheus 生態系統包含了幾個關鍵的組件:
- Prometheus server、
- Pushgateway、
- Alertmanager、
- Web UI 等,
但是大多數組件都不是必需的,其中最核心的組件當然是 Prometheus server,它負責收集和存儲指標數據,支持表達式查詢,和告警的生成。接下來我們就來安裝 Prometheus server。
下載&安裝 Prometheus server
二進制文件的方式
https://github.com/prometheus/prometheus/releases
按需選擇對應的操作系統 ,下載即可
【 查看版本信息 】
[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]#【運行 Prometheus server】
前臺運行哈,下面這種方式
[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
拉取鏡像 啟動容器
[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 命令
啟動容器 指定配置文件的位置
通過-v 指定配置文件,將配置文件存放在宿主機上,便于管理
[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通過 -v 參數將本地的配置文件掛載到 /etc/prometheus/ 位置, prometheus 在容器中默認加載的配置文件位置。
Prometheus配置文件
我們啟動的時候,可以通過參數 --config.file 來指定配置文件
- global 塊
Prometheus 的全局配置,比如 scrape_interval 表示 Prometheus 多久抓取一次數據,evaluation_interval 表示多久檢測一次告警規則;
- alerting 塊
Alertmanager 的配置
- rule_files 塊:
告警規則
- scrape_config 塊
這里定義了 Prometheus 要抓取的目標.
默認已經配置了一個名稱為 prometheus 的 job, Prometheus 在啟動的時候也會通過 HTTP 接口暴露自身的指標數據,Prometheus 自己監控自己.
可以訪問 http://localhost:9090/metrics 查看 Prometheus 暴露了哪些指標;
PromQL初探
Prometheus 提供了可視化的 Web UI 方便我們操作, 訪問 http://ip:9090/ 默認跳轉到 Graph 頁面
總結
以上是生活随笔為你收集整理的APM - Prometheus监控系统初探的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Docker Review - Dock
- 下一篇: Java - Java集合中的快速失败F