Prometheus 特點
Prometheus 具有以下特點:
1、多維數據模型:由度量名稱和鍵值對標識的時間序列數據
2、PromSQL:一種靈活的查詢語言,可以利用多維數據完成復雜的查詢
3、不依賴分布式存儲,單個服務器節點可直接工作
4、基於HTTP的pull方式采集時間序列數據
5、推送時間序列數據通過PushGateway組件支持
6、通過服務發現或靜態配置發現目標
7、多種圖形模式及儀表盤支持(grafana)
8、適用於以機器為中心的監控以及高度動態面向服務架構的監控
一:prometheus server 的部署
在 peometheus的官網下載prometheus的源碼文件:https://prometheus.io/download/ ,linux 下載之后是一個tar包,直接解壓縮,就會出現一個prometheus的二進制文件,這樣就安裝完成了,無需其他安裝部署步驟。
二:配置中心詳解
編輯prometheus的安裝目錄中的prometheus.yml文件
[root@webserver2 prometheus]# pwd
/alidata/prometheus/prometheus
[root@webserver2 prometheus]# ll prometheus.yml
-rw-r--r-- 1 root root 2872 Oct 16 14:11 prometheus.yml
配置文件的模塊介紹:
global:全局配置
alerting:告警配置
rule_files:告警規則
scrape_configs:配置數據源,稱為target,每個target用job_name命名
prometheus server 配置文件如下:
[root@webserver2 prometheus]# cat 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.
alerting:
alertmanagers:
- static_configs:
- targets: ['127.0.0.1:9093']
rule_files:
- "/alidata/prometheus/prometheus/rules/disk_rule.yml"
- "/alidata/prometheus/prometheus/rules/cpu_rule.yml"
- "/alidata/prometheus/prometheus/rules/memory_rule.yml"
- "/alidata/prometheus/prometheus/rules/node_up.yml"
- "/alidata/prometheus/prometheus/rules/system_load.yml"
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'pushgateway'
scrape_interval: 30s
honor_labels: true #加上此配置exporter節點上傳數據中的一些標簽將不會被pushgateway節點的相同標簽覆蓋
static_configs:
- targets: ['34.193.83.103:9091']
labels:
instance: pushgateway
# php組服務
- job_name: 'php_node_exporter'
static_configs:
- targets: ['172.16.100.126:9098']
labels:
name: webserver01
- targets: ['172.16.100.127:9098']
labels:
name: webserver02
#go組服務
- job_name: 'go_node_exporter'
static_configs:
- targets: ['172.16.254.173:9098']
labels:
name: go1
- targets: ['172.16.254.172:9098']
labels:
name: go2
#japan_node_exporter
- job_name: 'japan_node_exporter'
static_configs:
#node_ecporter
- job_name: 'node_exporter'
static_configs:
- targets: ['127.0.0.1:9100']
labels:
name: webserver2
- targets: ['172.16.149.48:9100']
labels:
name: sonar
#admin_node_exporter
- job_name: 'admin_node_exporter'
static_configs:
- targets: ['172.16.100.120:9098']
labels:
name: H2
默認配置補充:
#有一些默認值,不配置就內容就選擇使用默認值,是可以進行更改的,以下是單個job的默認值
# 任務名
job_name: <job_name>
# 抓取間隔,默認為對應全局配置
[ scrape_interval: <duration> | default = <global_config.scrape_interval> ]
# 抓取超時時間,默認為對應全局配置
[ scrape_timeout: <duration> | default = <global_config.scrape_timeout> ]
# 協議,默認為 http,可選 https
[ scheme: <scheme> | default = http ]
# 抓取地址的路徑,默認為 /metrics
[ metrics_path: <path> | default = /metrics ]
# 抓取地址的參數
params:
[ <string>: [<string>, ...] ]
# 是否尊重抓取回來的標簽,默認為 false
[ honor_labels: <boolean> | default = false ]
# 靜態目標配置
static_configs:
[ - <static_config> ... ]
# 單次抓取的采樣值個數限制,默認為 0,表示沒有限制
[ sample_limit: <int> | default = 0 ]
三:啟動
服務器啟動的默認端口是:9090 可以通過--web.listen-address="0.0.0.0:9090" 進行更改
# 切換到prometheus的安裝目錄
[root@webserver2 ~]# cd /alidata/prometheus/prometheus/
# 使用nohup 進行后台啟動
[root@webserver2 prometheus]# nohup ./prometheus --config.file=./prometheus.yml --storage.tsdb.retention.time=365d &
#正常的情況下,可以看到以下日志內容:
level=info ts=2019-10-18T08:45:45.084Z caller=main.go:654 msg="Starting TSDB ..."
level=info ts=2019-10-18T08:45:45.084Z caller=web.go:448 component=web msg="Start listening for connections" address=0.0.0.0:9090
level=info ts=2019-10-18T08:45:45.204Z caller=head.go:509 component=tsdb msg="replaying WAL, this may take awhile"
level=info ts=2019-10-18T08:45:45.258Z caller=head.go:533 component=tsdb msg="WAL checkpoint loaded"
level=info ts=2019-10-18T08:45:50.020Z caller=main.go:669 fs_type=EXT4_SUPER_MAGIC level=info ts=2019-10-18T08:45:50.020Z caller=main.go:670 msg="TSDB started"
level=info ts=2019-10-18T08:45:50.020Z caller=main.go:740 msg="Loading configuration file" filename=./prometheus.yml
level=info ts=2019-10-18T08:45:50.208Z caller=main.go:768 msg="Completed loading of configuration file" filename=./prometheus.yml
level=info ts=2019-10-18T08:45:50.208Z caller=main.go:623 msg="Server is ready to receive web requests."
啟動的常用參數
--storage.tsdb.path="data/"
#修改本地數據存儲的路徑,默認存儲目錄是prometheus的當前目錄
--storage.tsdb.retention.time=15d
# 修改prometheus的數據保留時間,默認保留15天數據
--web.listen-address="0.0.0.0:9090"
# 修改啟動prometheus的監聽端口,默認為9090
在prometheus的安裝目錄中執行 ./prometheus -h 查看所支持的啟動參數命令
四:web UI介紹
服務啟動完成之后,可通過http://localhost:9090訪問Prometheus的UI界面,這個是prometheus的自帶的UI界面,可用作數據查詢,在當前界面使用promsql的語法進行數據查詢,promsql的查詢方式請參考文章《Prometheus-PromSQL 語法介紹和使用方法》
PromSQl的查詢舉例:
查詢cpu的使用率
100 - (avg(irate(node_cpu_seconds_total{instance=~"$node",mode="idle"}[5m])) * 100)
查詢內存使用率:
100 - (node_memory_MemFree_bytes{instance=~"$node"} + node_memory_Cached_bytes{instance=~"$node"} + node_memory_Buffers_bytes{instance=~"$node"}) / (node_memory_MemTotal_bytes{instance=~"$node"})* 100
常用的界面操作介紹:
通過9090端口進入界面
Status -> targets 進入界面查看被監控節點的運行狀態
Status -> rules 進入界面查看報警規則
Status -> configuration 進入界面查看prometheus的配置中心 prometheus.yml 文件中的內容