docker安裝普羅米修斯+Granfan並監控容器 一、基本概念 1、之間的關係 prometheus與grafana之間是相輔相成的關係。作為完美的分散式監控系統的Prometheus,就想布加迪威龍一樣示例和動力強勁。在猛的車也少不了儀錶盤來觀察。於是優雅的可視化平臺Grafana出現 ...
docker安裝普羅米修斯+Granfan並監控容器
一、基本概念
1、之間的關係
prometheus與grafana之間是相輔相成的關係。作為完美的分散式監控系統的Prometheus,就想布加迪威龍一樣示例和動力強勁。在猛的車也少不了儀錶盤來觀察。於是優雅的可視化平臺Grafana出現了。
簡而言之Grafana作為可視化的平臺,平臺的數據從Prometheus中取到來進行儀錶盤的展示。而Prometheus這源源不斷的給Grafana提供數據的支持。
2、基本工作原理
- Exporter 監控工具,獲取數據
- Prometheus 普羅米修斯時序資料庫,用來存儲和查詢你的監控數據
- Grafana 儀錶盤
二、瞭解概念之- Prometheus
Prometheus是一個開源的系統監控和報警系統,由Google BorgMon監控系統的開源版本發展而來。它使用Go語言開發,能夠監控和告警各種系統,包括網路、存儲、伺服器和應用等。Prometheus通過採集大量監控數據並存儲到時間序列資料庫中,然後通過查詢語言PromQL進行數據查詢和可視化。它已經成為繼k8s之後第二大在CNCF托管的項目,被廣泛應用於各種數據中心環境的監控。
三、瞭解概念之—Grafana
Grafana是一個開源的度量分析和可視化套件,常被用於數據分析和監控。它支持多種不同的數據源,包括Graphite、Elasticsearch、InfluxDB、Prometheus等,並能夠將數據以各種圖形的方式展示出來,比如折線圖、柱狀圖、熱圖等。Grafana的儀錶盤可以用來展示各種信息,比如系統性能、網路延遲、用戶行為等。Grafana還支持自定義查詢和可視化的功能,可以根據不同的需求來定製數據展示。
四、環境準備
系統 | IP | 配置 | 角色 |
---|---|---|---|
cnetos7.9 | 192.168.8.180 | 2c4g | NodeEXporter、cAdvisor、 Prometheus Server、Grafana |
cnetos7.9 | 192.168.8.181 | 2c4g | NodeEXporter、cAdvisor |
將包分別存放至兩台機器
google-cadvisor.tar.gz grafana.tar.gz node-exporter.tar.gz prometheus.tar.gz
組件說明
Prometheus Server:
普羅米修斯的主伺服器,埠號9090
NodeEXporter:
負責收集Host硬體信息和操作系統信息,埠號9100
cAdvisor:
負責收集Host上運行的容器信息,埠號占用8080
Grafana:
負責展示普羅米修斯監控界面,埠號3000
altermanager:
等待接收prometheus發過來的告警信息,altermanager再發送給定義的收件人
1.安裝node-EXporter
作用: 收集硬體和系統信息
#導入鏡像
[root@localhost docker]# docker load -i node-exporter.tar.gz
1e604deea57d: Loading layer [==================================================>] 1.458MB/1.458MB
6b83872188a9: Loading layer [==================================================>] 2.455MB/2.455MB
4f3f7dd00054: Loading layer [==================================================>] 20.5MB/20.5MB
Loaded image: prom/node-exporter:latest
#運行容器
[root@localhost docker]# 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)($|/)"
WARNING: Published ports are discarded when using host network mode
3f3abcc55d5ab28922a268d6fe60a1f9382e71808367ca1c15d2c2eb89ae7bc8
[root@localhost docker]#
#查看運行狀態
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f3abcc55d5a prom/node-exporter "/bin/node_exporter …" 44 seconds ago Up 40 seconds upbeat_agnesi
[root@localhost docker]#
驗證收集效果http://192.168.8.180:9100/metrics
2、安裝cAdvisor
作用: 收集節點容器信息
#導入鏡像
[root@localhost docker]# docker load -i google-cadvisor.tar.gz
cd7100a72410: Loading layer [==================================================>] 4.403MB/4.403MB
9ea477e6d99e: Loading layer [==================================================>] 33.09MB/33.09MB
66b3c2e84199: Loading layer [==================================================>] 32.88MB/32.88MB
Loaded image: google/cadvisor:latest
[root@localhost docker]#
#運行容器
[root@localhost docker]# docker run -v /:/rootfs:ro -v /var/run:/var/run/:rw -v /sys:/sys:ro -v /var/lib/docker:/var/lib/docker:ro -p 8080:8080 --detach=true --name=cadvisor --net=host google/cadvisor
WARNING: Published ports are discarded when using host network mode
477a383ea1943384f96a70dc1a9cd8d101565a5e3cca861cff6b6c0ed990efc5
[root@localhost docker]#
#查看運行狀態
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
477a383ea194 google/cadvisor "/usr/bin/cadvisor -…" 32 seconds ago Up 31 seconds cadvisor
3f3abcc55d5a prom/node-exporter "/bin/node_exporter …" 5 minutes ago Up 5 minutes upbeat_agnesi
[root@localhost docker]#
http://192.168.8.180:8080/containers/
驗證收集效果傳遞
3、部署prometheus-server服務
先啟動一個prometheus服務,目的是複製他的配置文件,修改配置文件,prometheus掛載這個文件
[root@localhost docker]# docker load -i prometheus.tar.gz
405a8b5041f1: Loading layer [==================================================>] 140.1MB/140.1MB
6dfd7aef3fcd: Loading layer [==================================================>] 131.8MB/131.8MB
8459ad25bfbd: Loading layer [==================================================>] 3.584kB/3.584kB
6cd41730d304: Loading layer [==================================================>] 13.82kB/13.82kB
dc12aa49f027: Loading layer [==================================================>] 28.16kB/28.16kB
f61f35ce8414: Loading layer [==================================================>] 13.31kB/13.31kB
89911f5a9a27: Loading layer [==================================================>] 5.632kB/5.632kB
e3df5cefd22c: Loading layer [==================================================>] 139.8kB/139.8kB
64d73d1a1b52: Loading layer [==================================================>] 1.536kB/1.536kB
b4d8bcc5b63b: Loading layer [==================================================>] 6.144kB/6.144kB
Loaded image: prom/prometheus:latest
[root@localhost docker]#
[root@localhost docker]# mkdir -pv /prometheus
mkdir: 已創建目錄 "/prometheus"
[root@localhost docker]#
#運行為容器,將容器的/etc/prometheus/prometheus.yml配置文件拷貝到宿主機
[root@localhost docker]# docker run -d --name test -P prom/prometheus
e449972ad85a035b62ae3aaca013c7f59d5000e7701df331b4bde060fcb37aa9
[root@localhost docker]#
#將容器內的/etc/prometheus/prometheus.yml文件拷貝到宿主機的/prometheus
[root@localhost docker]# docker cp test:/etc/prometheus/prometheus.yml /prometheus
Successfully copied 2.56kB to /prometheus
[root@localhost docker]# ll -l /prometheus/
總用量 4
-rw-r--r--. 1 root root 934 8月 27 19:11 prometheus.yml
[root@localhost docker]#
#編輯prometheus配置文件,在static_configs下麵修改為
#以下添加的ip都將會被監控起來
[root@localhost docker]# more /prometheus/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: ["192.168.8.180:9090","192.168.8.180:8080","localhost:9100","192.168.8.181:8080","192.168.8.181:9100"]
[root@localhost docker]#
4、刪除test容器並重新運行prometheus
[root@localhost docker]# docker rm -f test
test
[root@localhost docker]# docker run -d --name prometheus --net host -p 9090:9090 -v /prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
WARNING: Published ports are discarded when using host network mode
9274dfcd700dae2b9e4b23fa2212ffcc34a4cdc2ae767d6d0bfacc83763cf8ac
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9274dfcd700d prom/prometheus "/bin/prometheus --c…" 4 seconds ago Up 3 seconds prometheus
477a383ea194 google/cadvisor "/usr/bin/cadvisor -…" 15 minutes ago Up 15 minutes cadvisor
3f3abcc55d5a prom/node-exporter "/bin/node_exporter …" 20 minutes ago Up 20 minutes upbeat_agnesi
[root@localhost docker]#
5、訪問測試
6、在prometheus伺服器上部署grafana
grafana主要概念
- 插件:擴展功能作用,完成不能完成的事
- 數據源:連接數據源,通過數據源提供數據 來出圖
- dashboard:展示面板,出什麼樣的圖
[root@localhost docker]# docker load -i grafana.tar.gz
d4fc045c9e3a: Loading layer [==================================================>] 7.667MB/7.667MB
89b7e78c1264: Loading layer [==================================================>] 2.56kB/2.56kB
63c0e4417684: Loading layer [==================================================>] 8.806MB/8.806MB
de6ac4ba26dc: Loading layer [==================================================>] 9.713MB/9.713MB
6523361408ca: Loading layer [==================================================>] 190kB/190kB
11f6d7670b43: Loading layer [==================================================>] 104.4kB/104.4kB
6271cd8eca5a: Loading layer [==================================================>] 244.4MB/244.4MB
4beb5a56ae2e: Loading layer [==================================================>] 215.2MB/215.2MB
9c67d537403a: Loading layer [==================================================>] 37.89kB/37.89kB
e7392c8d1dd0: Loading layer [==================================================>] 5.12kB/5.12kB
Loaded image: grafana/grafana:latest
[root@localhost docker]#
[root@localhost docker]# mkdir -pv /grafana
mkdir: 已創建目錄 "/grafana"
[root@localhost docker]# chmod 777 -R /grafana
[root@localhost docker]# docker run -d -p 3000:3000 --name grafana -v /grafana:/var/lib/grafana -e "GF_SECURITY_ADMIN_PASSWORD=admin" grafana/grafana
736af9f6c1f55a2dbf8a7d3bf7de4d17f9555a2f8340bce8ccc2b0984fd09b77
[root@localhost docker]#
"GF_SECURITY_ADMIN_PASSWORD=" #是要設置grafana的登錄密碼
1.
訪問測試
http://192.168.8.180:3000/login
預設用戶名密碼
用戶名:admin
密碼:admin