docker monitor — grafana+cAdvisor+prometheus

Sheng-Shan Chen
3 min readApr 21, 2021

--

目標為監控所有docker container容器,主要是因為我建的大部分服務都在docker上,需要一個監控系統來除錯。

Step 1.

cAdvisor (Container Advisor) collects, aggregates, processes and exports information about running containers. For installing cAdvisor with docker;

請注意,我在server上的docker位置為 /home/docker

預設位置在 /var/lib/docker ,選錯會造成cAdvisor取不到資料

sudo docker run \
--volume=/:/rootfs:ro \
--volume=/var/run:/var/run:ro \
--volume=/sys:/sys:ro \
--volume=/home/docker/:/home/docker:ro \
--volume=/dev/disk/:/dev/disk:ro \
--publish=8080:8080 \
--detach=true \
--name=cadvisor \
gcr.io/google-containers/cadvisor:latest

cAdvisor is now running on http://localhost:8080

Step 2.

Prometheus is an open-source system monitoring and alerting toolkit. Prometheus is very easy to setup. Firstly, we need to download the latest release of prometheus from https://prometheus.io/download/ .

首先要配置 promethus.yml,位置在 /etc/prometheus/prometheus.yml

配置為

# global config
global:
scrape_interval: 15s
evaluation_interval: 15s
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'cadvisor'
scrape_interval: 10s
metrics_path: '/metrics'
static_configs:
- targets: ['localhost:8080']
labels:
group: 'cadvisor'

這裡我跟原文操作不同的是我把prometheus也同樣採用container安裝,所以指令為

docker run -d -p 9090:9090  -v /etc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml     prom/prometheus --name=prometheus

啟用後,到http://localhost:9090 看看

理論上會看到兩個服務都是up的狀態,如果沒有請檢查container是否沒有開啟,或port衝突。

Step 3.

Grafana is an open source metric analytics & visualization suite. It is used for visualizing time series data for infrastructure and application analytics. Grafana installation is very laborless, and we will run it using the official Docker container.

$ docker run -d -p 3000:3000 grafana/grafana

Grafana is running on port 3000 so, we will use http://localhost:3000 for going main page of grafana.

http://localhost:3000

點選左方 「Configuration(齒輪)」> data source

新增一個新的來源

選擇 Prometheus,進行相關配置,先前安裝prometheus為 [http://{your_ip}:9090,不要填localhost,因為docker](http://{your_ip}:9090,不要填localhost,因為docker container內的localhost跟實體機的localhost位置不同。

選擇下方 save and test,系統會檢查是否能連線成功。

下一步,選左方「+」> import

這裡使用 https://grafana.com/grafana/dashboards/193 的模板

在裡面輸入 193 > Load

最後,

你就能在網頁上觀察看 docker 使用的效能與底下容器的狀態了。

參考文件:https://medium.com/@mertcan.simsek276/docker-monitoring-with-cadvisor-prometheus-and-grafana-adefe1202bf8

請我喝杯咖啡

https://buymeacoffee.com/stwater20

--

--