環境說明html
咱們將經過 Prometheus 監控兩臺 Docker Host:192.168.56.102 和 192.168.56.103,監控 host 和容器兩個層次的數據。node
按照架構圖,咱們須要運行以下組件:docker
Prometheus Serverjson
Prometheus Server 自己也將以容器的方式運行在 host 192.168.56.103 上。瀏覽器
Exporter架構
Prometheus 有不少現成的 Exporter,完整列表請參考 https://prometheus.io/docs/instrumenting/exporters/學習
咱們將使用:測試
Grafana網站
顯示多維數據,Grafana 自己也將以容器方式運行在 host 192.168.56.103 上。this
運行 Node Exporter
在兩個 host 上執行以下命令:
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://192.168.56.102:9100/metrics 測試一下。
640?wx_fmt=png&wxfrom=5&wx_lazy=1
運行 cAdvisor
在兩個 host 上執行以下命令:
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
當cadvisor容器起不來時
輸入:
mount -o remount,rw '/sys/fs/cgroup'
ln -s /sys/fs/cgroup/cpu,cpuacct /sys/fs/cgroup/cpuacct,cpu
注意,這裏咱們使用了 --net=host,這樣 Prometheus Server 能夠直接與 cAdvisor 通訊。
cAdvisor 啓動後,將經過 8080 提供 host 的監控數據。在瀏覽器中經過 http://192.168.56.102:8080/metrics 測試一下。
640?wx_fmt=png&wxfrom=5&wx_lazy=1
訪問http://192.168.56.102:8080/containers/
運行 Prometheus Server
vi /root/prometheus.yml ——建立一個prometheus配置文件
# 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','192.168.56.103:8080','192.168.56.103:9100','192.168.56.103:8080','192.168.56.103:9100']
在 host 192.168.56.103 上執行以下命令:
docker run -d -p 9090:9090 \
-v /root/prometheus.yml:/etc/prometheus/prometheus.yml \
--name prometheus \
--net=host \
prom/prometheus
注意,這裏咱們使用了 --net=host,這樣 Prometheus Server 能夠直接與 Exporter 和 Grafana 通訊。
prometheus.yml 是 Prometheus Server 的配置文件。
640?wx_fmt=png&wxfrom=5&wx_lazy=1
最重要的配置是:
static_configs:
- targets: ['localhost:9090','localhost:8080','localhost:9100','192.168.56.102:8080','192.168.56.102:9100']
指定從哪些 exporter 抓取數據。這裏指定了兩臺 host 上的 Node Exporter 和 cAdvisor。
另外 localhost:9090 就是 Prometheus Server 本身,可見 Prometheus 自己也會收集本身的監控數據。一樣地,咱們也能夠經過 http://192.168.56.103:9090/metrics 測試一下。
640?wx_fmt=png&wxfrom=5&wx_lazy=1
在瀏覽器中打開 http://192.168.56.103:9090 ,點擊菜單 Status -> Targets。
640?wx_fmt=png&wxfrom=5&wx_lazy=1
以下圖所示:
640?wx_fmt=png&wxfrom=5&wx_lazy=1
全部 Target 的 State 都是 UP,說明 Prometheus Server 可以正常獲取監控數據。
運行 Grafana
在 host 192.168.56.103 上執行以下命令:
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://192.168.56.103:3000/
640?wx_fmt=png&wxfrom=5&wx_lazy=1
登陸後,Grafana 將引導咱們配置 Data Source。
640?wx_fmt=png&wxfrom=5&wx_lazy=1
Name 爲 Data Source 命名,例如 prometheus。
Type 選擇 Prometheus。
640?wx_fmt=png&wxfrom=5&wx_lazy=1
Url 輸入 Prometheus Server 的地址 http://192.168.56.103:9090
其餘保持默認值,點擊 Add。
若是一切順利,Grafana 應該已經可以訪問 Prometheus 中存放的監控數據了,那麼如何展現呢?
Grafana 是經過 Dashboard 展現數據的,在 Dashboard 中須要定義:
可見,要作出一個 Dashboard 也不是件容易的事情。幸運的是,咱們能夠藉助開源社區的力量,直接使用現成的 Dashboard。
訪問 https://grafana.com/dashboards?dataSource=prometheus&search=docker,將會看到不少用於監控 Docker 的 Dashboard。
640?wx_fmt=png&wxfrom=5&wx_lazy=1
咱們能夠下載這些現成的 Dashboard,而後 import 到咱們的 Grafana 中就能夠直接使用了。
好比下載 Docker and system monitoring,獲得一個 json 文件,而後點擊 Grafana 左上角菜單 Dashboards -> Import。
640?wx_fmt=png&wxfrom=5&wx_lazy=1
導入咱們下載的 json 文件。
640?wx_fmt=png&wxfrom=5&wx_lazy=1
Dashboard 將馬上展現出漂亮的圖表。
640?wx_fmt=png&wxfrom=5&wx_lazy=1
在這個 Dashboard 中,上部分是 host 的數據,咱們能夠經過 Node 切換不一樣的 host。
640?wx_fmt=png&wxfrom=5&wx_lazy=1
Dashboard 的下半部分展現的是全部的容器監控數據。Grafana 的 Dashboard 是可交互的,咱們能夠在圖表上只顯示指定的容器、選取指定的時間區間、從新組織和排列圖表、調整刷新頻率,功能很是強大。
好了,以上就完成了 Prometheus 監控系統的部署,更多功能你們能夠自行探索。到這裏咱們已經學習了多種 Docker 監控方案,是時候對它們作個比較了,下一節見。
https://cloud.tencent.com/info/2eadc0b20b9f26a9842391722a2bd179.html 能夠藉助這個網站進行參考