Docker常用的監控方案
Prometheus
Prometheus架構
Prometheus是一種很不錯的監控方案,它提供了監控數據搜集、存儲、處理、可視化和警告一套完整的解決方案,下面是Prometheus的架構
Prometheus Server
Prometheus Server負責從Exporter拉取和存儲監控數據,並提供一套靈活的查詢語言(PromQL)供用戶使用
Exporter
Exporter負責收集目標對象(host,container...)的性能數據,並通過HTTP接口提供Prometheus Server獲取
Alertmanager
用戶可以定義基於監控數據的告警規則,規則會觸發告警。一旦Alertmanager收到告警,會通過預定義的方式發出告警通知。支持的方式包括Email、PagerDuty、Webhook等
Prometheus的優勢
(1)通過維度對數據進行說明,附加更多的業務信息,進而滿足不同業務的需求。同時維度是可以動態添加的,比如再給數據加上一個user維度,就可以按用戶來統計容器內存使用量了
(2)Prometheus豐富的查詢語言能夠靈活、充分地挖掘數據的價值
部署Prometheus
環境說明
我們將通過Prometheus監控兩台Docker Host:10.211.55.17和10.211.55.21,監控host和容器兩個層次的數據,按照構架圖,我們需要運行如下組件
Prometheus Server
Prometheus Server本身也將以容器的方式運行在host 10.211.55.21上
Exporter
Prometheus有很多現成的Exporter,完整列表可參照https://prometheus.io/docs/instrumenting/exporters/
這里將使用
(1)Node Exporter,負責收集host硬件和操作系統數據。它將以容器方式運行在所有host上
(2)cAdvisor,負責收集容器數據。它將以容器的方式運行在所有host上
Grafana
顯示多維數據,Grafana本身也將以容器方式運行在host 10.211.55.21上
運行Node Exporter
在兩台主機上執行如下命令
sudo docker run -d -p 9100:9100 -v "/proc:/host/proc" -v "/sys:/host/sys" -v "/:/rootfs" --net=host prom/node-exporter --path.procfs /host/proc --path.sysfs /host/sys --collector.filesystem.ignored-mount-points "/(sys|proc|dev|host|etc)($|/)"
這里使用了--net=host,這樣Prometheus Server可以直接與Node Exporter通信。Node Exporter啟動后,將通過9100提供host的監控數據,在瀏覽器中通過http://10.211.55.17:9100/metrics測試一下
運行cAdvisor
在兩個主機上執行一下命令
sudo docker run --volume=/:/rootfs:ro --volume=/var/run:/var/run:rw --volume=/sys:/sys:ro --volume=/var/lib/docker:/var/lib/docker:ro --publish=8080:8080 --detach=true --name=cadvisor --net=host google/cadvisor:latest
這里使用了--net=host,這樣Prometheus Server可以直接與cAdvisor通信。cAdvisor啟動后,將通過8080提供host的監控數據,在瀏覽器中通過http://10.211.55.17:8080/metrics測試一下
運行Prometheus Server
先在主機10.211.55.21上編寫prometheus.yml文件,其具體內容如下
sudo vim 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','localhost:8080','localhost:9100','10.211.55.17:9100','10.211.55.17:8080']
編寫好配置文件后執行一下命令,以容器的方式來運行prometheus
sudo docker run -d -p 9090:9090 -v /home/chenjin/prometheus.yml:/etc/prometheus/prometheus.yml --name prometheus --net=host prom/prometheus
這里使用了--net=host,這樣Prometheus Server可以直接與Exporter和Grafana通信。上面的配置文件中最重要的是-targets里面的內容,指定從哪些exporter抓取數據。這里指定了兩台主機上的Node Exporter個cAdvisor,另外localhost:9090就是Prometheus Server自己,可見Prometheus本身也會收集自己的監控數據。可以通過http://10.211.55.21:9090/metrics測試一下
在瀏覽器中打開http://10.211.55.21:9090,點擊菜單Status -> Targets
如下圖所示
所有Target的State都是都是UP狀態,說明Prometheus Server能夠正常獲取監控數據
運行Grafana
在主機10.211.55.21上執行如下命令
sudo docker run -d -i -p 3000:3000 -e "GF_SERVER_ROOT_URL=http://grafana.server.name" -e "GF_SECURITY_ADMIN_PASSWORD=secret" --net=host grafana/grafana
這里使用了--net=host,這樣Grafana可以直接與Prometheus Server通信。-e "GF_SECURITY_ADMIN_PASSWORD=secret"指定了Grafana admin用戶和密碼secret
Grafana啟動后,在瀏覽器中打開http://10.211.55.21:3000
登錄后,Grafana將引導我們配置Data Source
Name為Date Source命令,例如prometheus
Type選擇Prometheus
Url輸入Prometheus Server的地址
其他保持默認,點擊下面的Save & Test
配置完成后,Grafana就能夠訪問Prometheus中存放的監控數據了