mirror of
https://github.com/yeasy/docker_practice.git
synced 2026-03-06 15:31:11 +00:00
Release v1.5.0: Restructure chapters and update for Docker v30.x
This commit is contained in:
16
10_ops/monitor/README.md
Normal file
16
10_ops/monitor/README.md
Normal file
@@ -0,0 +1,16 @@
|
||||
# 容器监控
|
||||
|
||||
容器化技术的普及使得应用部署变得更加灵活和高效,但也给监控带来了新的挑战。
|
||||
|
||||
在传统架构中,我们通常关注主机的 CPU、内存、磁盘 IO 等指标。而在容器环境下,除了主机层面的监控,我们更关注容器级别的资源使用情况、服务的运行状态以及编排系统的健康状况。
|
||||
|
||||
## 常见的监控方案
|
||||
|
||||
目前主流的容器监控方案包括:
|
||||
|
||||
* **cAdvisor**: Google 开源的容器资源监控工具,Docker 原生支持。
|
||||
* **Prometheus**: CNCF 毕业项目,云原生时代最流行的监控系统。
|
||||
* **Grafana**: 强大的可视化平台,常与 Prometheus 配合使用。
|
||||
* **ELK/EFK Stack**: 主要用于日志管理,但也能提供一定的监控能力。
|
||||
|
||||
本章将重点介绍如何使用 Prometheus 和 Grafana 搭建一套完整的容器监控系统。
|
||||
107
10_ops/monitor/prometheus.md
Normal file
107
10_ops/monitor/prometheus.md
Normal file
@@ -0,0 +1,107 @@
|
||||
# Prometheus + Grafana
|
||||
|
||||
[Prometheus](https://prometheus.io/) 是一个开源的系统监控和报警工具包。它受 Google Borgmon 的启发,由 SoundCloud 在 2012 年创建。
|
||||
|
||||
## 架构简介
|
||||
|
||||
Prometheus 的主要组件包括:
|
||||
|
||||
* **Prometheus Server**: 核心组件,负责收集和存储时间序列数据。
|
||||
* **Exporters**: 负责向 Prometheus 暴露监控数据(如 Node Exporter, cAdvisor)。
|
||||
* **Alertmanager**: 处理报警发送。
|
||||
* **Pushgateway**: 用于支持短生命周期的 Job 推送数据。
|
||||
|
||||
## 快速部署
|
||||
|
||||
我们可以使用 Docker Compose 快速部署一套 Prometheus + Grafana 监控环境。
|
||||
|
||||
### 1. 准备配置文件
|
||||
|
||||
创建 `prometheus.yml`:
|
||||
|
||||
```yaml
|
||||
global:
|
||||
scrape_interval: 15s
|
||||
|
||||
scrape_configs:
|
||||
- job_name: 'prometheus'
|
||||
static_configs:
|
||||
- targets: ['localhost:9090']
|
||||
|
||||
- job_name: 'node-exporter'
|
||||
static_configs:
|
||||
- targets: ['node-exporter:9100']
|
||||
|
||||
- job_name: 'cadvisor'
|
||||
static_configs:
|
||||
- targets: ['cadvisor:8080']
|
||||
```
|
||||
|
||||
### 2. 编写 Docker Compose 文件
|
||||
|
||||
创建 `docker-compose.yml`:
|
||||
|
||||
```yaml
|
||||
version: '3.8'
|
||||
|
||||
services:
|
||||
prometheus:
|
||||
image: prom/prometheus:latest
|
||||
volumes:
|
||||
- ./prometheus.yml:/etc/prometheus/prometheus.yml
|
||||
ports:
|
||||
- "9090:9090"
|
||||
networks:
|
||||
- monitoring
|
||||
|
||||
grafana:
|
||||
image: grafana/grafana:latest
|
||||
ports:
|
||||
- "3000:3000"
|
||||
environment:
|
||||
- GF_SECURITY_ADMIN_PASSWORD=admin
|
||||
networks:
|
||||
- monitoring
|
||||
depends_on:
|
||||
- prometheus
|
||||
|
||||
node-exporter:
|
||||
image: prom/node-exporter:latest
|
||||
ports:
|
||||
- "9100:9100"
|
||||
networks:
|
||||
- monitoring
|
||||
|
||||
cadvisor:
|
||||
image: gcr.io/cadvisor/cadvisor:latest
|
||||
ports:
|
||||
- "8080:8080"
|
||||
volumes:
|
||||
- /:/rootfs:ro
|
||||
- /var/run:/var/run:ro
|
||||
- /sys:/sys:ro
|
||||
- /var/lib/docker/:/var/lib/docker:ro
|
||||
networks:
|
||||
- monitoring
|
||||
|
||||
networks:
|
||||
monitoring:
|
||||
```
|
||||
|
||||
### 3. 启动服务
|
||||
|
||||
```bash
|
||||
$ docker-compose up -d
|
||||
```
|
||||
|
||||
启动后,访问以下地址:
|
||||
|
||||
* Prometheus: `http://localhost:9090`
|
||||
* Grafana: `http://localhost:3000` (默认账号密码: admin/admin)
|
||||
|
||||
## 配置 Grafana 面板
|
||||
|
||||
1. 在 Grafana 中添加 Prometheus 数据源,URL 填写 `http://prometheus:9090`。
|
||||
2. 导入现成的 Dashboard 模板,例如 [Node Exporter Full](https://grafana.com/grafana/dashboards/1860) (ID: 1860) 和 [Docker Container](https://grafana.com/grafana/dashboards/193) (ID: 193)。
|
||||
|
||||
这样,你就拥有了一个直观的容器监控大屏。
|
||||
Reference in New Issue
Block a user