上一篇文章我們已經學習了比較流行的cAdvisor+InfluxDB+Grafana組合進行Docker監控。這節課來學習Prometheus+cAdvisor+Grafana組合。
cAdvisor是專門用來采集數據的工具,也是google公司的一款開源產品,Grafana則是前端展示,支持多種數據源,定制非常靈活。而prometheus則作為數據源。
整體架構圖如下:
一、prometheus
1、Prometheus介紹
Prometheus(普羅米修斯)是一個最初在SoundCloud上構建的監控系統。自2012年成為社區開源項目,擁有非常活躍的開發人員和用戶社區。為強調開源及獨立維護,Prometheus於2016年加入雲原生雲計算
基金會(CNCF),成為繼Kubernetes之后的第二個托管項目。
官網:https://prometheus.io
github地址:https://github.com/prometheus
2、Prometheus 特點
• 多維數據模型:由度量名稱和鍵值對標識的時間序列數據
• PromSQL:一種靈活的查詢語言,可以利用多維數據完成復雜的查詢
• 不依賴分布式存儲,單個服務器節點可直接工作
• 基於HTTP的pull方式采集時間序列數據
• 推送時間序列數據通過PushGateway組件支持
• 通過服務發現或靜態配置發現目標
• 多種圖形模式及儀表盤支持(grafana)
3、Prometheus架構圖
• Prometheus Server:收集指標和存儲時間序列數據,並提供查詢接口
• ClientLibrary:客戶端庫
• Push Gateway:短期存儲指標數據。主要用於臨時性的任務
• Exporters:采集已有的第三方服務監控指標並暴露metrics
• Alertmanager:告警
• Web UI:簡單的Web控制台
4、監控對象
實例:可以抓取的目標稱為實例(Instances)
作業:具有相同目標的實例集合稱為作業(Job)
scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] - job_name: 'node' static_configs: - targets: ['192.168.1.100:9090']
5、Prometheus 部署
二進制部署:https://prometheus.io/docs/prometheus/latest/getting_started/
Docker部署:https://prometheus.io/docs/prometheus/latest/installation/
訪問Web:http://localhost:9090
配置Prometheus監控本身:
scrape_configs:
- job_name: 'prometheus'
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
下面已docker部署為例:
docker run -d -p 9090:9090 -v /root/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
prometheus.yml
# 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: 'cadvisor' static_configs: - targets: ['10.11.97.187:8081']
這時就可以通過9090端口進行訪問prometheus的頁面了:
prometheus網頁比較簡單,包括告警、圖像、以及基本信息。
6、Prometheus 配置
全局配置
scrape配置
二、cAdvisor部署
github地址:https://github.com/google/cadvisor
通過docker快速安裝cadvisor,然后通過8081就可以訪問了。
docker run \ --volume=/:/rootfs:ro \ --volume=/var/run:/var/run:ro \ --volume=/sys:/sys:ro \ --volume=/var/lib/docker/:/var/lib/docker:ro \ --volume=/dev/disk/:/dev/disk:ro \ --publish=8081:8080 \ --detach=true \ --name=cadvisor \ google/cadvisor:latest
訪問web頁面:
里面包含的內容非常豐富,包括docker主機上所有容器的資源監控和圖表展示。
三、grafana部署
1、grafana安裝
一句話安裝:
docker run -d -p 3000:3000 grafana/grafana
安裝完成后,即可訪問:
2、grafana添加數據源
進入grafana后,點擊設置,添加數據源,填寫類型為prometheus以及prometheus的URL。
3、導入模板
grafana里面的template(模板)可以自己制作,也可以直接從grafana官網導入。
點擊Import dashaboard,我們直接從grafana官網導入模板:
輸入ID號:
輸入后回車,grafana會自動從官網查到到官網,並可以進行導入:
記得在prometheus里面把添加cadvisor作為采集數據源:
稍等片刻后,grafana里面就會有數據了: