Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

開源監控docker的三大神器 – grafana+cAdvisor+prometheus

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

cAdvisor:https://github.com/google/cadvisor
grafana:https://github.com/grafana/grafana
prometheus:https://github.com/prometheus/prometheus

Step 1. 在docker上安裝cAdvisor。

請注意,我在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 可以在 http://localhost:8080 看到

Step 2. Prometheus是一個開源系統監視和警報工具。 Prometheus非常容易設置。首先,我們需要從以下位置下載最新版本 https://prometheus.io/download/ 

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

# global config
global:
  scrape_interval:     15s
  evaluation_interval: 15srule_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是一個開源指標分析和可視化套件。它用於可視化時間序列數據,以用於基礎結構和應用程序分析。




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

Grafana 預設使用 3000 port ,所以去 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

Back To Top
error: 內容被保護 !!
Buy Me A Coffee
歡迎贊助 sectools.tw 讓這個網站更好~!