docker-compose 搭建 Prometheus+Grafana监控系统
有关监控选型之前有写过一篇文章: 监控系统选型,一文轻松搞定!

监控对象
- Linux服务器
- Docker
- Redis
- MySQL
数据采集
1)、prometheus: 采集数据
2)、node-exporter: 收集操作系统和硬件信息的metrics
3)、cadvisor : 收集Docker的metrics
4)、redis-exporter: 收集Redis的metrics
5)、 mysql-exporter: 收集MySQL的metrics
报表
- grafana: 图表展示
一、环境搭建
1、MySQL用户授权
之前想用root账号尝试收集MySQL的metrics,但在启动docker的时候一直报错:
“Error pinging mysqld" err="Error 1049: Unknown database
后来去官方地址在发现,需要对用户进行授权才能收集监控数据信息。
gitHub开源项目有说明: https://github.com/prometheus/mysqld_exporter
所以这里重新创建用户并授权。
CREATE USER 'exporter'@'%' IDENTIFIED BY '123456';
GRANT PROCESS, REPLICATION CLIENT ON *.* TO 'exporter'@'%';
GRANT SELECT ON performance_schema.* TO 'exporter'@'%';
注意
:建议为用户设置最大连接限制,以避免在重负载下监控抓取使服务器过载。
2、配置docker-compose.yml
我在/etc/prometheus创建一个docker-compose.yml
文件,文件内容如下
version: '2'
networks:
monitor:
driver: bridge
services:
prometheus:
image: prom/prometheus
container_name: prometheus
hostname: prometheus
restart: always
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
ports:
- "9090:9090"
networks:
- monitor
alertmanager: #预警模块
image: prom/alertmanager
container_name: alertmanager
hostname: alertmanager
restart: always
ports:
- "9093:9093"
networks:
- monitor
grafana:
image: grafana/grafana
container_name: grafana
hostname: grafana
restart: always
ports:
- "3000:3000"
networks:
- monitor
node-exporter:
image: quay.io/prometheus/node-exporter
container_name: node-exporter
hostname: node-exporter
restart: always
ports:
- "9100:9100"
networks:
- monitor
cadvisor:
image: google/cadvisor:latest
container_name: cadvisor
hostname: cadvisor
restart: always
volumes:
- /:/rootfs:ro
- /var/run:/var/run:rw
- /sys:/sys:ro
- /var/lib/docker/:/var/lib/docker:ro
ports:
- "8899:8080"
networks:
- monitor
redis-exporter:
image: oliver006/redis_exporter
container_name: redis-exporter
hostname: redis-exporter
restart: always
ports:
- "9121:9121"
command:
- "--redis.addr=redis://127.0.0.1:6379"
networks:
- monitor
mysql_xporter:
image: prom/mysqld-exporter
container_name: mysql-exporter
hostname: mysql-exporter
restart: always
ports:
- "9104:9104"
environment:
DATA_SOURCE_NAME: 'exporter:123456@(127.0.0.1:3306)'
networks:
- monitor
因为这里prometheus容器的配置文件重新挂载了,挂载到/etc/prometheus/目录下,所以在该目录创建好prometheus.yml
文件。
3、配置prometheus.yml
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['ip:9090'] //ip为自己服务器IP
- job_name: 'cadvisor'
static_configs:
- targets: ['ip:8899']
- job_name: 'node'
static_configs:
- targets: ['ip:9100']
- job_name: 'redis_exporter'
static_configs:
- targets: ['ip:9121']
- job_name: 'mysql_exporter'
static_configs:
- targets: ['ip:9104']
二、docker-compose启动
在docker-compose.yml文件目录下通过 docker-compose up -d
启动容器。
我们可以通过 docker-compose ps
容器是否成功。

启动成功,就可以分别访问:
http://ip:9090
:prometheus的原生web-uihttp://ip:3000
:Grafana开源的监控可视化组件页面http://ip:9100
:收集服务器的metricshttp://ip:8899
:收集docker的metricshttp://ip:9100
:收集redis的metricshttp://ip:9104
:收集mysql的metrics
打开 http://ip:9090/targets ,如果State都是UP即代表Prometheus工作正常,如下图所示:

从图中可以看出所有State都是UP,说明已经搭建成功了,接下来就通过Grafana来展示监控数据。
三、Grafana配置监控
打开http://ip:3000, 使用默认账号密码admin/admin
登录

默认进来是创建数据库的页面,在如下图所示中,选择Prometheus

输入prometheus数据源的信息,主要是输入name和url

数据源配置好之后,我们就可以配置对应的监控信息了,常见的配置监控已经有对应的模板了,就不需要我们一个一个地去配置了。(如果不满足的话,那可以手动配置)
四、选择监控模版
grafana提供了许许多多精美的模版,我们可以在模版库中选择我们需要到模版导入到当前项目就可以了,使用非常方便。
模板地址
: https://grafana.com/grafana/dashboards/
1、Linux硬件信息监控
本次要导入的模板:https://grafana.com/grafana/dashboards/11074

选择好模版后,只需要导入到Grafana中就可以了。

导入成功后,选择数据源,为该模版命名

即可看到逼格非常高的系统主机节点监控信息了

2、docker监控
模版ID
:893

监控效果

3、Mysql监控
模版ID
:7362
监控效果

4、Redis监控
模版ID
:10534
监控效果

每种监控都有非常多的模版样式,我们可以去模版库中去找自己喜欢的就可以。这里监控就搭建完成了。
声明: 公众号如需转载该篇文章,发表文章的头部一定要 告知是转至公众号: 后端元宇宙。同时也可以问本人要markdown原稿和原图片。其它情况一律禁止转载!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
2018-06-12 java多线程(3)---synchronized、Lock