網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
Prometheus+Grafana監(jiān)控Docker容器和Linux主機(jī)
- Prometheus概述
- Prometheus部署
- Prometheus配置文件
- prometheus.yml
- scrape_configs
- 使用Prometheus監(jiān)控docker容器
- 使用cAdvisor采集監(jiān)控?cái)?shù)據(jù)(被監(jiān)控端)
- 修改prometheus配置文件
- 使用Grafana可視化監(jiān)控?cái)?shù)據(jù)
- 使用Prometheus監(jiān)控Linux主機(jī)
- 安裝node exporter(被監(jiān)控端)
- 修改prometheus配置文件
- 使用Grafana可視化監(jiān)控?cái)?shù)據(jù)
Prometheus概述
Prometheus發(fā)布于2016年,比zabbix晚了4年。雖然其社區(qū)支持暫時(shí)不如后者完善,但是對(duì)容器的支持更好,不僅支持swarm原生群集,還支持kubernetes容器集群的監(jiān)控。
Prometheus的特點(diǎn)包括:
- 多維數(shù)據(jù)模型:由度量名稱和鍵值對(duì)標(biāo)識(shí)的時(shí)間序列數(shù)據(jù);
- PromQL:一種靈活的查詢語(yǔ)言,可以利用多維數(shù)據(jù)完成復(fù)雜查詢;
- 不依賴分布式存儲(chǔ),單個(gè)服務(wù)器節(jié)點(diǎn)可直接工作;
- 基于HTTP的pull方式采集時(shí)間序列數(shù)據(jù);
- 支持通過(guò)PushGateway組件推送時(shí)間序列數(shù)據(jù);
- 通過(guò)服務(wù)發(fā)現(xiàn)(service discovery)或靜態(tài)配置發(fā)現(xiàn)目標(biāo);
- 多種圖形模式及儀表盤支持(grafana)。
架構(gòu)
Prometheus+Grafana的架構(gòu)如下:
組件
主要組件 | 功能 |
---|---|
Prometheus Server | 收集指標(biāo)和存儲(chǔ)時(shí)間序列數(shù)據(jù),并提供查詢接口 |
ClientLibrary | 客戶端庫(kù) |
Push Gateway | 短期存儲(chǔ)指標(biāo)數(shù)據(jù),主要用于臨時(shí)性任務(wù) |
Exporters | 采集已有的第三方服務(wù)監(jiān)控指標(biāo)并暴露metrics |
Alertmanager | 處理告警 |
Web UI | 簡(jiǎn)單的Web控制臺(tái) |
實(shí)例和作業(yè)
實(shí)例(Instance)和作業(yè)(Job)是Prometheus中的兩個(gè)重要概念。實(shí)例是指可以抓取的目標(biāo)(即被監(jiān)控端,通常是單個(gè)的進(jìn)程),而作業(yè)則是指具有相同目標(biāo)的實(shí)例的集合。
Prometheus部署
Prometheus有四種常見(jiàn)的部署方法:
- 使用預(yù)編譯的二進(jìn)制文件(pre-compiled binaries)安裝;
- 通過(guò)源碼編譯(from source);
- 使用docker部署;
- 通過(guò)配置管理系統(tǒng)安裝:比如Ansible、Chef、Puppet和SaltStack。
下面主要介紹第三種方法(最簡(jiǎn)單最方便),其它方法參見(jiàn) https://prometheus.io/docs/prometheus/latest/installation/
。
使用docker部署
需要準(zhǔn)備配置文件prometheus.yml(見(jiàn)后面的內(nèi)容)。
$ docker run -d -p 9090:9090 prom/prometheus # 最簡(jiǎn)單的運(yùn)行命令
# 運(yùn)行并綁定掛載主機(jī)上的配置文件prometheus.yml
$ docker run -d --name=prometheus -p 9090:9090 \
-v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus
# 訪問(wèn)http://主機(jī)IP:9090
Prometheus配置文件
prometheus.yml
Prometheus的配置語(yǔ)言用YAML語(yǔ)言編寫,其格式一般如下:
# global關(guān)鍵字用于定義全局參數(shù)
global:
# 定義從被監(jiān)控目標(biāo)采集數(shù)據(jù)的頻率,默認(rèn)為1分鐘
[ scrape_interval: <duration> | default = 1m ]
# 定義數(shù)據(jù)采集請(qǐng)求的超時(shí)時(shí)間,默認(rèn)為10秒
[ scrape_timeout: <duration> | default = 10s ]
# 評(píng)估規(guī)則(告警檢測(cè))的頻率,默認(rèn)為1分鐘
[ evaluation_interval: <duration> | default = 1m ]
# 跟外部系統(tǒng)(federation, remote storage, Alertmanager)通信時(shí)向任意時(shí)間序列或告警添加的標(biāo)簽
external_labels:
[ <labelname>: <labelvalue> ... ]
# 記錄PromQL查詢語(yǔ)句的文件。重新加載配置會(huì)重新打開(kāi)此文件
[ query_log_file: <string> ]
# rule_files定義了告警規(guī)則文件列表
rule_files:
[ - <filepath_glob> ... ]
# scrape_configs定義了監(jiān)控?cái)?shù)據(jù)抓取的配置列表
scrape_configs:
[ - <scrape_config> ... ]
# alerting定義了與Alertmanager相關(guān)的告警配置
alerting:
alert_relabel_configs:
[ - <relabel_config> ... ]
alertmanagers:
[ - <alertmanager_config> ... ]
# remote_write和remote_read定義了遠(yuǎn)程讀寫的相關(guān)配置
remote_write:
[ - <remote_write> ... ]
remote_read:
[ - <remote_read> ... ]
scrape_configs
下面主要介紹scrape_configs的配置參數(shù),它定義了監(jiān)控目標(biāo)(target)的集合并描述了從監(jiān)控目標(biāo)采集數(shù)據(jù)的相關(guān)參數(shù)。監(jiān)控目標(biāo)可以通過(guò)static_configs
關(guān)鍵字配置,也可以通過(guò)使用服務(wù)發(fā)現(xiàn)機(jī)制來(lái)動(dòng)態(tài)地發(fā)現(xiàn)。使用relabel_configs
關(guān)鍵字可以在采集數(shù)據(jù)前對(duì)任意監(jiān)控目標(biāo)及其標(biāo)簽(label)進(jìn)行進(jìn)一步的修改。
# 數(shù)據(jù)采集作業(yè)的名稱
job_name: <job_name>
# 數(shù)據(jù)采集的頻率,默認(rèn)為對(duì)應(yīng)的全局配置
[ scrape_interval: <duration> | default = <global_config.scrape_interval> ]
# 數(shù)據(jù)采集的超時(shí)時(shí)間,默認(rèn)為對(duì)應(yīng)的全局配置
[ scrape_timeout: <duration> | default = <global_config.scrape_timeout> ]
# 從監(jiān)控目標(biāo)獲取指標(biāo)的HTTP資源路徑
[ metrics_path: <path> | default = /metrics ]
# 當(dāng)采集的監(jiān)控?cái)?shù)據(jù)的標(biāo)簽與服務(wù)器端配置的不一樣時(shí),如果honor_labels設(shè)置為false,則以服務(wù)器端為準(zhǔn),否則以采集數(shù)據(jù)的標(biāo)簽為準(zhǔn)
[ honor_labels: <boolean> | default = false ]
# honor_timestamps設(shè)置為true時(shí),使用采集的監(jiān)控?cái)?shù)據(jù)的時(shí)間戳
[ honor_timestamps: <boolean> | default = true ]
# 監(jiān)控?cái)?shù)據(jù)采集請(qǐng)求使用的協(xié)議,默認(rèn)為http
[ scheme: <scheme> | default = http ]
# 可選的HTTP URL參數(shù)
params:
[ <string>: [<string>, ...] ]
# 使用配置的用戶名和密碼設(shè)置每個(gè)數(shù)據(jù)采集請(qǐng)求的Authorization頭部標(biāo)簽。其中password與password_file配置互斥
basic_auth:
[ username: <string> ]
[ password: <secret> ]
[ password_file: <string> ]
# 使用bear token設(shè)置每個(gè)數(shù)據(jù)采集請(qǐng)求的Authorization頭部標(biāo)簽。與bearer_token_file配置互斥
[ bearer_token: <secret> ]
# 使用bear token設(shè)置每個(gè)數(shù)據(jù)采集請(qǐng)求的Authorization頭部標(biāo)簽。與bearer_token配置互斥
[ bearer_token_file: <filename> ]
# 數(shù)據(jù)采集請(qǐng)求的TLS配置(安全傳輸層協(xié)議)
tls_config:
[ <tls_config> ]
# 可選的代理URL.
[ proxy_url: <string> ]
# Azure服務(wù)發(fā)現(xiàn)的配置清單
azure_sd_configs:
[ - <azure_sd_config> ... ]
# Consul服務(wù)發(fā)現(xiàn)的配置清單
consul_sd_configs:
[ - <consul_sd_config> ... ]
# DigitalOcean服務(wù)發(fā)現(xiàn)的配置清單
digitalocean_sd_configs:
[ - <digitalocean_sd_config> ... ]
# Docker Swarm服務(wù)發(fā)現(xiàn)的配置清單
dockerswarm_sd_configs:
[ - <dockerswarm_sd_config> ... ]
# DNS服務(wù)發(fā)現(xiàn)的配置清單
dns_sd_configs:
[ - <dns_sd_config> ... ]
# EC2服務(wù)發(fā)現(xiàn)的配置清單
ec2_sd_configs:
[ - <ec2_sd_config> ... ]
# Eureka服務(wù)發(fā)現(xiàn)的配置清單
eureka_sd_configs:
[ - <eureka_sd_config> ... ]
# file服務(wù)發(fā)現(xiàn)的配置清單
file_sd_configs:
[ - <file_sd_config> ... ]
# GCE服務(wù)發(fā)現(xiàn)的配置清單
gce_sd_configs:
[ - <gce_sd_config> ... ]
# Hetzner服務(wù)發(fā)現(xiàn)的配置清單
hetzner_sd_configs:
[ - <hetzner_sd_config> ... ]
# Kubernetes服務(wù)發(fā)現(xiàn)的配置清單
kubernetes_sd_configs:
[ - <kubernetes_sd_config> ... ]
# Marathon服務(wù)發(fā)現(xiàn)的配置清單
marathon_sd_configs:
[ - <marathon_sd_config> ... ]
# AirBnB's Nerve服務(wù)發(fā)現(xiàn)的配置清單
nerve_sd_configs:
[ - <nerve_sd_config> ... ]
# OpenStack服務(wù)發(fā)現(xiàn)的配置清單
openstack_sd_configs:
[ - <openstack_sd_config> ... ]
# Zookeeper Serverset服務(wù)發(fā)現(xiàn)的配置清單
serverset_sd_configs:
[ - <serverset_sd_config> ... ]
# Triton服務(wù)發(fā)現(xiàn)的配置清單
triton_sd_configs:
[ - <triton_sd_config> ... ]
# 為當(dāng)前作業(yè)靜態(tài)配置的監(jiān)控目標(biāo)清單.
static_configs:
[ - <static_config> ... ]
# 監(jiān)控目標(biāo)relabel重新設(shè)定標(biāo)簽配置
relabel_configs:
[ - <relabel_config> ... ]
# 指標(biāo)relabel配置清單
metric_relabel_configs:
[ - <relabel_config> ... ]
# 監(jiān)控?cái)?shù)據(jù)采樣的數(shù)目限制,默認(rèn)為0表示無(wú)限制
[ sample_limit: <int> | default = 0 ]
# 數(shù)據(jù)采集的監(jiān)控目標(biāo)的個(gè)數(shù)限制,默認(rèn)為0表示無(wú)限制
[ target_limit: <int> | default = 0 ]
其他參數(shù)的配置參見(jiàn) https://prometheus.io/docs/prometheus/latest/configuration/configuration/
。
詳細(xì)配置可以參考示例:https://github.com/prometheus/prometheus/blob/release-2.24/documentation/examples/prometheus-dockerswarm.yml。
使用Prometheus監(jiān)控docker容器
常用的監(jiān)控指標(biāo)有內(nèi)存、CPU、磁盤、網(wǎng)絡(luò)。一般使用Google開(kāi)源的cAdvisor (Container Advisor)來(lái)收集正在運(yùn)行的容器資源信息和性能信息(參見(jiàn) https://github.com/google/cadvisor
)。
使用cAdvisor采集監(jiān)控?cái)?shù)據(jù)(被監(jiān)控端)
使用Docker部署cadvisor。
$ docker run -d --volume=/:/rootfs:ro \
--volume=/var/run:/var/run:ro --volume=/sys:/sys:ro \
--volume=/var/lib/docker/:/var/lib/docker:ro \
--volume=/dev/disk/:/dev/disk:ro \
--publish=8080:8080 --detach=true \
--name=cadvisor google/cadvisor:latest
# 訪問(wèn)http://主機(jī)IP:8080
cAdvisor不負(fù)責(zé)存儲(chǔ)采集到的監(jiān)控?cái)?shù)據(jù),因此只能查看實(shí)時(shí)數(shù)據(jù)。cAdvisor為Prometheus提供的數(shù)據(jù)采集接口為http://被監(jiān)控主機(jī)IP:8080/metrics
。
修改prometheus配置文件
在prometheus.yml的scrape_configs中添加監(jiān)控作業(yè)。
- job_name: "my_docker"
static_configs:
- targets: ['被監(jiān)控主機(jī)IP:8080']
然后重啟容器:docker restart prometheus
。可以在http://主機(jī)IP:9090/targets
頁(yè)面可以查看prometheus監(jiān)控的目標(biāo)。在http://主機(jī)IP:9090/graph
頁(yè)面可以查看監(jiān)控參數(shù)的簡(jiǎn)單圖形化展示。
使用Grafana可視化監(jiān)控?cái)?shù)據(jù)
Grafana則是一個(gè)開(kāi)源的度量分析和可視化系統(tǒng)(https://grafana.com/docs/)。它比Prometheus自帶的圖形化展示功能更強(qiáng)大。
Docker部署Grafana
$ docker run -d --name=grafana -p 3000:3000 grafana/grafana
# 訪問(wèn)http://主機(jī)IP:3000,默認(rèn)用戶名和密碼都是admin,登錄并修改密碼
添加數(shù)據(jù)源
在添加數(shù)據(jù)源中選擇Prometheus,在HTTP下的URL欄中粘貼http://Prometheus主機(jī)IP:9090
并保存。
創(chuàng)建儀表盤(dashboard)
在新建儀表盤(New dashboard)中點(diǎn)擊右側(cè)的導(dǎo)入儀表盤(Import dashboard),輸入并搜索193(對(duì)應(yīng)儀表盤名稱為docker monitoring),在顯示的儀表盤選項(xiàng)(Options)中選擇數(shù)據(jù)源為Prometheus,最后點(diǎn)擊導(dǎo)入即可。
在 https://grafana.com/grafana/dashboards
上可以發(fā)現(xiàn)更多官方和社區(qū)提供的儀表盤。
使用Prometheus監(jiān)控Linux主機(jī)
Prometheus監(jiān)控Linux主機(jī)需要在被監(jiān)控目標(biāo)上安裝Node Exporter。在目標(biāo)主機(jī)上執(zhí)行以下腳本來(lái)部署node exporter。
(參見(jiàn) https://prometheus.io/docs/guides/node-exporter/#monitoring-linux-host-metrics-with-the-node-exporter
)
安裝node exporter(被監(jiān)控端)
#!/bin/bash
wget https://github.com/prometheus/node_exporter/releases/download/v0.17.0/node_exporter-0.17.0.linux-amd64.tar.gz
tar zxf node_exporter-0.17.0.linux-amd64.tar.gz
mv node_exporter-0.17.0.linux-amd64 /usr/local/node_exporter
cat << EOF > /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=https://prometheus.io
[Service]
Restart=on-failure
ExecStart=/usr/local/node_exporter/node_exporter
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable node_exporter
systemctl restart node_exporter
安裝完成以后使用ps -ef | grep node_exporter
查看是否正常運(yùn)行。Node exporter默認(rèn)會(huì)使用9100端口,為Prometheus提供的數(shù)據(jù)采集接口為http://被監(jiān)控主機(jī)IP:9100/metrics
。
修改prometheus配置文件
在prometheus.yml的scrape_configs中添加監(jiān)控作業(yè)。
- job_name: "my_linux"
static_configs:
- targets: ['被監(jiān)控主機(jī)IP:9100']
然后重啟容器:docker restart prometheus
。可以在http://主機(jī)IP:9090/targets
頁(yè)面可以查看prometheus監(jiān)控的目標(biāo)。在http://主機(jī)IP:9090/graph
頁(yè)面可以查看監(jiān)控參數(shù)的簡(jiǎn)單圖形化展示。
使用Grafana可視化監(jiān)控?cái)?shù)據(jù)
Grafana提供的9276儀表盤可以很好地可視化Linux主機(jī)基礎(chǔ)監(jiān)控信息。參照上文中Grafana可視化容器監(jiān)控的儀表盤導(dǎo)入操作。
原文鏈接:https://blog.csdn.net/Sebastien23/article/details/113645177
相關(guān)推薦
- 2022-11-19 Android權(quán)限詢問(wèn)的實(shí)例詳解_Android
- 2022-12-21 Python中的取整、取余運(yùn)算方法_python
- 2022-07-23 C語(yǔ)言簡(jiǎn)明分析指針與引用的具體用法_C 語(yǔ)言
- 2022-12-15 Pycharm中配置Anaconda解釋器的完整步驟_python
- 2022-04-16 統(tǒng)計(jì)C語(yǔ)言二叉樹中葉子結(jié)點(diǎn)個(gè)數(shù)_C 語(yǔ)言
- 2023-04-26 C語(yǔ)言實(shí)現(xiàn)數(shù)組元素排序方法詳解_C 語(yǔ)言
- 2022-06-21 python繪制帶有色塊的折線圖_python
- 2022-12-10 Qt顯示QImage圖像在label上,并保持自適應(yīng)大小問(wèn)題_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支