14、Docker监控方案(Prometheus+cAdvisor+Grafana)

作者:@skyflask
转载本文请注明出处:https://www.cnblogs.com/skyflask/p/11148702.html


目录

一、prometheus
1、Prometheus介绍
3、Prometheus架构图
4、监控对象
5、Prometheus 部署
6、Prometheus 配置
二、cAdvisor部署
三、grafana部署
1、grafana安装
2、grafana添加数据源
3、导入模板

上一篇文章我们已经学习了比较流行的cAdvisor+InfluxDB+Grafana组合进行Docker监控。这节课来学习Prometheus+cAdvisor+Grafana组合。

cAdvisor是专门用来采集数据的工具,也是google公司的一款开源产品,Grafana则是前端展示,支持多种数据源,定制非常灵活。而prometheus则作为数据源。

整体架构图如下:

 

一、prometheus

1、Prometheus介绍

Prometheus(普罗米修斯)是一个最初在SoundCloud上构建的监控系统。自2012年成为社区开源项目,拥有非常活跃的开发人员和用户社区。为强调开源及独立维护,Prometheus于2016年加入云原生云计算

基金会(CNCF),成为继Kubernetes之后的第二个托管项目。

官网:https://prometheus.io
github地址:https://github.com/prometheus

 

2、Prometheus 特点
• 多维数据模型:由度量名称和键值对标识的时间序列数据
• PromSQL:一种灵活的查询语言,可以利用多维数据完成复杂的查询
• 不依赖分布式存储,单个服务器节点可直接工作
• 基于HTTP的pull方式采集时间序列数据
• 推送时间序列数据通过PushGateway组件支持
• 通过服务发现或静态配置发现目标
• 多种图形模式及仪表盘支持(grafana)

 

3、Prometheus架构图

• Prometheus Server:收集指标和存储时间序列数据,并提供查询接口
• ClientLibrary:客户端库
• Push Gateway:短期存储指标数据。主要用于临时性的任务
• Exporters:采集已有的第三方服务监控指标并暴露metrics
• Alertmanager:告警
• Web UI:简单的Web控制台

 

4、监控对象

实例:可以抓取的目标称为实例(Instances)
作业:具有相同目标的实例集合称为作业(Job)

1
2
3
4
5
6
7
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'node'
static_configs:
- targets: ['192.168.1.100:9090']

  

5、Prometheus 部署

二进制部署:https://prometheus.io/docs/prometheus/latest/getting_started/
Docker部署:https://prometheus.io/docs/prometheus/latest/installation/
访问Web:http://localhost:9090
配置Prometheus监控本身:
scrape_configs:
- job_name: 'prometheus'
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']

下面已docker部署为例:

1
docker run -d  -p 9090:9090 -v /root/prometheus.yml:/etc/prometheus/prometheus.yml  prom/prometheus

prometheus.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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']
  - job_name: 'cadvisor'
    static_configs:
    - targets: ['10.11.97.187:8081']

  

这时就可以通过9090端口进行访问prometheus的页面了:

 

prometheus网页比较简单,包括告警、图像、以及基本信息。

6、Prometheus 配置

全局配置

 

 scrape配置

 

二、cAdvisor部署

github地址:https://github.com/google/cadvisor

通过docker快速安装cadvisor,然后通过8081就可以访问了。

1
2
3
4
5
6
7
8
9
10
docker run \
  --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=8081:8080 \
  --detach=true \
  --name=cadvisor \
  google/cadvisor:latest

  

访问web页面:

 

 里面包含的内容非常丰富,包括docker主机上所有容器的资源监控和图表展示。

三、grafana部署

1、grafana安装

一句话安装:

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

  

安装完成后,即可访问:

 

2、grafana添加数据源

进入grafana后,点击设置,添加数据源,填写类型为prometheus以及prometheus的URL。

 

3、导入模板

grafana里面的template(模板)可以自己制作,也可以直接从grafana官网导入。

点击Import dashaboard,我们直接从grafana官网导入模板:

 输入ID号:

输入后回车,grafana会自动从官网查到到官网,并可以进行导入:

记得在prometheus里面把添加cadvisor作为采集数据源:

稍等片刻后,grafana里面就会有数据了:

 

 

posted @   skyflask  阅读(4517)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
点击右上角即可分享
微信分享提示