Docker學習(12):Docker監控方案之Prometheus
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,完整列表可參照
這裡將使用
(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中存放的監控資料了