cadvisor+promethues+grafana监控docker容器

一、cadvisor介绍

        CAdvisor是谷歌开发的用于分析运行中容器的资源占用和性能指标的开源工具。为容器用户提供了对其运行容器的资源使用和性能特征的理解。 它是一个运行守护程序,用于收集,

聚合,处理和导出有关正在运行的容器的信息。 具体而言,对于每个容器,它保留资源隔离参数,历史资源使用和完整历史资源使用的直方图。 此数据按容器和机器范围导出。

官方地址:

dockerhub: https://hub.docker.com/r/google/cadvisor/

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

二、docker宿主机上安装/启动cadvisor

1、准备

docker命令监控容器:
docker stats containerid

1.设置权限
mount -o remount,rw '/sys/fs/cgroup'
2.建立软连接
ln -s /sys/fs/cgroup/cpu,cpuacct /sys/fs/cgroup/cpuacct,cpu

注:可忽略

2、安装、启动

# 获取最新镜像(可以指定版本:google/cadvisor:v0.24.1
docker pull google/cadvisor
 
# 运行容器
docker run --volume=/:/rootfs:ro --volume=/var/run:/var/run:rw --volume=/sys:/sys:ro --volume=/var/lib/docker/:/var/lib/docker:ro --publish=8080:8080 --detach=true --privileged=true --name=cadvisor --restart=always google/cadvisor:latest
 
# 运行容器简写方式
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 --privileged=true --name=cadvisor --restart=always google/cadvisor:latest
 
# 查看容器日志
docker logs -f cadvisor

三、访问cAdvisor的WEB UI

访问地址:http://host:8080 ,下图为cAdvisor的web界面,数据实时刷新但是不能存储。

查看json格式 : http://host:8080/metrics。

 四、配置prometheus

1、编辑prometheus.yml文件

vi 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' static_configs: - targets: ['localhost:9090'] - job_name: 'cadvisor' static_configs: - targets: ['192.168.0.122:8080']

2、重启prometheus,浏览器访问prometheus检查是否配置成功

 访问地址:http://host/:9090

 五.Grafana添加监控模板

1、配置Prometheus数据源

2、下载模板模板地址:https://grafana.com/dashboards,并导入

 3、在grafana-dashboard-import,输入193、8321、10619模板,选择数据来源prometheus

 4、查看统计仪表盘

 

posted on 2023-10-31 17:15  uestc2007  阅读(110)  评论(0编辑  收藏  举报

导航