fix(content): harden Docker practice guide

This commit is contained in:
yeasy
2026-06-16 21:23:21 -07:00
parent f4e684afeb
commit 9fdffa9d91
67 changed files with 343 additions and 278 deletions
+8 -6
View File
@@ -66,7 +66,7 @@ services:
- ./rules.yml:/etc/prometheus/rules.yml
- prometheus_data:/prometheus
ports:
- "9090:9090"
- "127.0.0.1:9090:9090"
command:
- --config.file=/etc/prometheus/prometheus.yml
- --storage.tsdb.path=/prometheus
@@ -77,9 +77,9 @@ services:
grafana:
image: grafana/grafana:13.0.1
ports:
- "3000:3000"
- "127.0.0.1:3000:3000"
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin
- GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_ADMIN_PASSWORD:?set GRAFANA_ADMIN_PASSWORD}
networks:
- monitoring
depends_on:
@@ -88,14 +88,14 @@ services:
node-exporter:
image: prom/node-exporter:v1.11.1
ports:
- "9100:9100"
- "127.0.0.1:9100:9100"
networks:
- monitoring
cadvisor:
image: ghcr.io/google/cadvisor:v0.56.2
ports:
- "8080:8080"
- "127.0.0.1:8080:8080"
volumes:
- /:/rootfs:ro
- /var/run:/var/run:ro
@@ -119,7 +119,9 @@ $ docker compose up -d
启动后访问以下地址
* Prometheus: `http://localhost:9090`
* Grafana`http://localhost:3000` (默认账号密码admin/admin首次登录后务必立即修改密码)
* Grafana`http://localhost:3000` (账号为 `admin`密码来自 `GRAFANA_ADMIN_PASSWORD`)
> 安全提示cAdvisor 需要读取宿主机 `/sys``/var/run`Docker 数据目录等路径才能采集容器指标即使是只读挂载也会暴露宿主机和容器元数据只在受控监控节点使用并把 PrometheusGrafanacAdvisor 端口绑定到本机或受保护网络
### 19.1.3 配置 Grafana 面板
-4
View File
@@ -42,8 +42,6 @@ services:
- ELASTICSEARCH_HOSTS=http://elasticsearch:9200
ports:
- "5601:5601"
links:
- elasticsearch
networks:
- logging
@@ -59,8 +57,6 @@ services:
ports:
- "24224:24224"
- "24224:24224/udp"
links:
- elasticsearch
volumes:
- ./fluentd/conf:/fluentd/etc
networks:
@@ -6,44 +6,18 @@
#### 核心性能指标体系
容器性能监控涉及以下关键指标
容器性能监控涉及 Docker CLIPrometheus/cAdvisor 指标和底层 cgroup 文件现代 Linux Kubernetes 新版本通常使用 cgroup v2旧系统或兼容环境仍可能看到 cgroup v1 名称
**CPU 相关指标**
- `cpu.usage_usec`容器 CPU 使用时间微秒
- `cpu.stat.nr_throttled`CPU 限流发生次数
- `cpu.stat.throttled_usec`CPU 限流总时间
- `cpu_percent`CPU 使用百分比
- `cpu_quota`CPU 配额设置微秒
**内存相关指标**
- `memory.usage_bytes`当前内存使用量
- `memory.max_usage_bytes`内存使用峰值
- `memory.limit_in_bytes`内存限制
- `memory.fail_cnt`OOMOut of Memory失败次数
- `memory.stat.cache`页面缓存占用
- `memory.stat.rss`实际内存占用RSS
- `memory.stat.swap`SWAP 使用量
**网络相关指标**
- `rx_bytes`接收字节数
- `tx_bytes`发送字节数
- `rx_packets`接收包数
- `tx_packets`发送包数
- `rx_errors`接收错误数
- `tx_errors`发送错误数
- `rx_dropped`接收丢包数
- `tx_dropped`发送丢包数
**I/O 相关指标**
- `io_service_bytes`I/O 操作字节数
- `io_service_time`I/O 操作耗时
- `io_queued`I/O 队列长度
- `fs_limit_bytes`文件系统限制
- `fs_usage_bytes`文件系统使用量
| 类型 | Docker / Prometheus 常见指标 | cgroup v2 文件 | cgroup v1 兼容名 |
|------|------------------------------|----------------|------------------|
| CPU 使用 | `CPU %``container_cpu_usage_seconds_total` | `cpu.stat` 中的 `usage_usec` | `cpuacct.usage` |
| CPU 限流 | `container_cpu_cfs_throttled_periods_total` | `cpu.stat` 中的 `nr_throttled``throttled_usec` | `cpu.stat.nr_throttled``cpu.stat.throttled_time` |
| 内存使用 | `MEM USAGE``container_memory_working_set_bytes` | `memory.current` | `memory.usage_in_bytes` |
| 内存限制 | `MEM USAGE / LIMIT` | `memory.max` | `memory.limit_in_bytes` |
| OOM 次数 | `container_oom_events_total` 或运行时事件 | `memory.events` 中的 `oom` / `oom_kill` | `memory.failcnt` |
| 网络收发 | `NET I/O``container_network_receive_bytes_total` / `transmit_bytes_total` | 网络命名空间接口计数 | `rx_bytes` / `tx_bytes` |
| I/O | `BLOCK I/O``container_fs_*` / `container_blkio_*` | `io.stat` | `blkio.throttle.io_service_bytes` |
| 文件系统 | `container_fs_usage_bytes` / `container_fs_limit_bytes` | 运行时或文件系统采集 | `fs_usage_bytes` / `fs_limit_bytes` |
### 19.3.2 使用 docker stats 实时监控
@@ -246,6 +220,16 @@ networks:
```
**Prometheus 配置文件prometheus.yml**
如果需要采集 Docker daemon 自身指标需要先在 Docker daemon 配置中开启 metrics
```json
{
"metrics-addr": "127.0.0.1:9323"
}
```
Prometheus 运行在容器里并使用 `host.docker.internal:9323` 抓取时daemon 必须监听容器可达的主机地址若改为 `0.0.0.0:9323`会把指标端口暴露给更大网络必须配合防火墙和可信网络边界
```yaml
global:
scrape_interval: 15s