Prometheus是CNCF的一個開源項目,Google BorgMon監控系統的開源版本,是一個系統和服務的監控系統。週期性採集metrics指標,匹配規則和展現結果,以及觸發某些條件的告警發送。html
Prometheus主要區別於其餘監控系統的特色是:java
Prometheus生態系統由多個組件組成,其中大部分是可選的組件。node
絕大部分Prometheus的組件都是用golang編寫,使得Prometheus 組件容易編譯和部署。(二進制沒有依賴)python
從架構圖中能夠看出,Prometheus Server 週期性的拉取從配置文件或者服務發現獲取到的目標數據,每一個目標須要經過HTTP接口暴露數據。Prometheus Server經過必定的規則彙總和記錄時序數據到本地數據庫。將符合檢測條件的告警數據推送給Altermanager,Altermanager經過配置的通知方式發送告警。Web UI 或者Grafana經過PromQL查詢Prometheus Server中的數據繪圖展現。git
Prometheus在記錄純數字的時序數據方面表現得很是好。既適用於機器的性能數據,也適用於服務的監控數據。對於微服務,Prometheus的多維度收集和查詢語言也是很是強大。github
Promethus的價值在於它的可靠性。Prometheus不適用於對統計或分析數據100%準確要求的場景。golang
下面我會經過Docker Compose的方式部署整個Prometheus監控系統和Grafana展現數據。若是對Docker Compose還不熟悉的朋友,能夠先查看我以前的介紹文章。web
Prometheus的docker-compose.yml基於github的開源倉庫修改。docker-compose.yml內容以下:docker
version: '3.1' volumes: prometheus_data: {} grafana_data: {} services: prometheus: image: prom/prometheus:v2.1.0 volumes: - ./prometheus/:/etc/prometheus/ - prometheus_data:/prometheus command: - '--config.file=/etc/prometheus/prometheus.yml' - '--storage.tsdb.path=/prometheus' - '--web.console.libraries=/usr/share/prometheus/console_libraries' - '--web.console.templates=/usr/share/prometheus/consoles' ports: - 9090:9090 restart: always node-exporter: image: prom/node-exporter volumes: - /proc:/host/proc:ro - /sys:/host/sys:ro - /:/rootfs:ro command: - '--path.procfs=/host/proc' - '--path.sysfs=/host/sys' - --collector.filesystem.ignored-mount-points - "^/(sys|proc|dev|host|etc|rootfs/var/lib/docker/containers|rootfs/var/lib/docker/overlay2|rootfs/run/docker/netns|rootfs/var/lib/docker/aufs)($$|/)" ports: - 9100:9100 restart: always alertmanager: image: prom/alertmanager volumes: - ./alertmanager/:/etc/alertmanager/ ports: - 9093:9093 restart: always command: - '--config.file=/etc/alertmanager/config.yml' - '--storage.path=/alertmanager' grafana: image: grafana/grafana user: "104" ports: - 3000:3000 depends_on: - prometheus volumes: - grafana_data:/var/lib/grafana - ./grafana/provisioning/:/etc/grafana/provisioning/ env_file: - ./grafana/config.monitoring restart: always
從上面的docker-compose.yml能夠看出,將經過Docker Compose部署Prometheus Server,Altermanager,Grafana,和node exporter。其中node exporter負責採集機器的基礎性能數據,例如CPU,MEM,DISK等等,經過暴露HTTP接口供Prometheus Server拉取數據作數據存儲和清洗。Grafana負責數據的展現。Prometheus經過配置文件靜態配置獲取node exporter的地址:數據庫
1 $ cat prometheus.yml 2 # my global config 3 global: 4 scrape_interval: 15s # By default, scrape targets every 15 seconds. 5 evaluation_interval: 15s # By default, scrape targets every 15 seconds. 6 # scrape_timeout is set to the global default (10s). 7 8 # Attach these labels to any time series or alerts when communicating with 9 # external systems (federation, remote storage, Alertmanager). 10 external_labels: 11 monitor: 'my-project' 12 13 # Load and evaluate rules in this file every 'evaluation_interval' seconds. 14 rule_files: 15 - 'alert.rules' 16 # - "first.rules" 17 # - "second.rules" 18 19 # alert 20 alerting: 21 alertmanagers: 22 - scheme: http 23 static_configs: 24 - targets: 25 - "alertmanager:9093" 26 27 # A scrape configuration containing exactly one endpoint to scrape: 28 # Here it's Prometheus itself. 29 scrape_configs: 30 # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. 31 32 - job_name: 'prometheus' 33 34 # Override the global default and scrape targets from this job every 5 seconds. 35 scrape_interval: 5s 36 37 static_configs: 38 - targets: ['localhost:9090'] 39 40 - job_name: 'node-exporter' 41 42 # Override the global default and scrape targets from this job every 5 seconds. 43 scrape_interval: 5s 44 static_configs: 45 - targets: ['node-exporter:9100']
其中40-45行是node-exporter的抓取地址和週期配置。由於Docker Compose會自動作服務地址解析,因此這裏能夠直接用node-exporter:9100做爲地址。
經過Prometheus 9090端口能夠查看到要採集的目標列表信息:
經過Grafana能夠查看到node exporter採集上來的數據展現,其中Grafana用的看板模板是https://grafana.com/dashboards/8919
文章開始分析了Prometheus開源監控系統的總體架構和特色,而後經過Docker Compose演示了整個系統的搭建。下一篇博客我將演示用Prometheus提供的Golang SDK從頭開始寫一個Expoter,敬請期待。