nahomseb/observability-showcase
GitHub: nahomseb/observability-showcase
一套生产就绪的 Docker 容器化可观测性技术栈,整合监控、日志、追踪与安全防护。
Stars: 0 | Forks: 0
# 可观测性技术栈
适用于容器化基础设施、经过生产验证的监控、日志记录和安全配置。运行 Grafana、Prometheus、Loki、Promtail 和 OpenTelemetry Collector,并为 nginx 提供安全加固。
## 架构
```
┌─────────────────────────────────────────┐
│ Grafana :3000 │
│ dashboards, alerting, log exploration │
└────────┬──────────────────┬─────────────┘
│ │
┌────────┴────────┐ ┌───────┴────────┐
│ Prometheus │ │ Loki │
│ :9090 │ │ :3100 │
│ metrics store │ │ log store │
└────────┬───────┘ └───────┬────────┘
│ │
┌──────────────┼──────────┐ │
│ │ │ │
┌─────┴─────┐ ┌─────┴────┐ ┌───┴───┐ ┌┴────────┐
│ node-exp │ │ cAdvisor │ │ OTel │ │Promtail │
│ host CPU │ │container │ │traces │ │log ship │
│ mem, disk │ │ metrics │ │ │ │ │
└───────────┘ └──────────┘ └───────┘ └─────────┘
```
## 快速开始
```
cp .env.example .env
# 编辑 .env — 设置 GRAFANA_PASSWORD
docker compose up -d
# Grafana: http://localhost:3000
# Prometheus: http://localhost:9090
```
## 包含内容
### 核心技术栈
| 组件 | 配置 | 用途 |
|-----------|--------|---------|
| **Prometheus** | `prometheus/prometheus.yml` | 指标抓取和时序数据存储 |
| **Loki** | `loki/loki.yml` | 日志聚合(保留 7 天) |
| **Promtail** | `loki/promtail.yml` | 从容器和系统日志传送日志 |
| **Grafana** | `grafana/` | 仪表盘、告警、数据源配置 |
### 可选组件
| 组件 | 配置 | 用途 |
|-----------|--------|---------|
| **OpenTelemetry Collector** | `otel/collector.yml` | 分布式追踪(支持 OTLP gRPC/HTTP) |
| **Node Exporter** | (在 docker-compose 中) | 主机级 CPU、内存、磁盘、网络指标 |
| **cAdvisor** | (在 docker-compose 中) | 单容器资源使用情况追踪 |
### 安全加固
| 配置 | 用途 |
|--------|---------|
| `nginx/default-security.conf` | 全捕获 Server 块 — 以 444 状态码丢弃未匹配的请求 |
| `nginx/security-locations.conf` | 拦截 .env/.git 探测、WordPress 扫描器、SQL 注入尝试、phpMyAdmin 爬虫 |
| `fail2ban/nginx-jail.conf` | 自动封禁频繁的机器人扫描(24 小时)和认证失败(1 小时) |
### 工具
| 脚本 | 用途 |
|--------|---------|
| `scripts/add-loki-to-service.py` | 自动为任意 docker-compose.yml 添加 Loki 日志驱动 |
### 仪表盘
| 仪表盘 | 用途 |
|-----------|---------|
| `grafana/dashboards/security-command-center.json` | 安全概览 — 探测计数、认证失败、机器人活动、封禁状态 |
## 添加你的服务
### 1. 添加 Prometheus 抓取目标
编辑 `prometheus/prometheus.yml` — 取消注释并调整示例目标:
```
scrape_configs:
- job_name: "my-api"
static_configs:
- targets: ["my-api:8000"]
metrics_path: "/metrics"
```
### 2. 将日志传送到 Loki
**方式 A** — Docker 日志驱动(单服务):
```
# 在你的服务的 docker-compose.yml 中
services:
my-api:
logging:
driver: loki
options:
loki-url: "http://localhost:3100/loki/api/v1/push"
loki-external-labels: "service=my-api,environment=prod"
```
**方式 B** — 使用自动化脚本:
```
python3 scripts/add-loki-to-service.py docker-compose.yml my-api:production:api
```
**方式 C** — Promtail 文件抓取(添加到 `loki/promtail.yml`):
```
scrape_configs:
- job_name: my-app
static_configs:
- targets: [localhost]
labels:
job: my-app
__path__: /var/log/my-app/*.log
```
### 3. 导入仪表盘
将 JSON 文件放入 `grafana/dashboards/` — Grafana 重启时会自动加载。
## 结构化日志标准
所有服务应输出包含以下字段的 JSON 日志:
```
{
"level": "info",
"service": "my-api",
"message": "Request processed",
"timestamp": "2026-03-03T12:00:00Z",
"duration_ms": 42
}
```
必填项:`level`, `service`, `message`, `timestamp`
## 配置说明
- **所有端口绑定到 127.0.0.1** — 不向互联网暴露。如需外部访问,请使用反向代理。
- **Loki 保留期**:7 天(可在 `loki/loki.yml` → `retention_period` 中配置)
- **Prometheus 保留期**:15 天(可在 docker-compose → `--storage.tsdb.retention.time` 中配置)
- **Grafana 密码**:通过环境变量设置,切勿硬编码
- **Fail2ban 封禁**:机器人扫描器 24 小时,认证失败 1 小时
## 项目结构
```
docker-compose.yml # Full stack (Prometheus + Loki + Grafana + optional exporters)
.env.example # Environment template
prometheus/
prometheus.yml # Scrape config with commented examples
loki/
loki.yml # Loki server config (retention, limits, compaction)
promtail.yml # Log shipping (Docker, syslog, nginx)
grafana/
provisioning/
prometheus.yml # Auto-provision Prometheus datasource
loki.yml # Auto-provision Loki datasource
dashboards/
security-command-center.json # Security monitoring dashboard
otel/
collector.yml # OpenTelemetry Collector (gRPC + HTTP → Prometheus)
nginx/
default-security.conf # Catch-all server block (drop unknown hosts)
security-locations.conf # Block attack paths (.env, wp-*, SQL injection)
fail2ban/
nginx-jail.conf # Auto-ban bot scanners and auth failures
scripts/
add-loki-to-service.py # Add Loki logging driver to any docker-compose.yml
```
## 技术栈
Grafana, Prometheus, Loki, Promtail, OpenTelemetry, Fail2ban, Nginx, Docker Compose
由 [D. Michael Piscitelli](https://github.com/herakles-dev) 构建 | [herakles.dev](https://herakles.dev)
标签:API集成, cAdvisor, CISA项目, Docker, GET参数, GitHub Advanced Security, Grafana, Loki, Nginx, NIDS, Node Exporter, OpenTelemetry, Promtail, 代理支持, 可观测性, 告警, 安全加固, 安全防御评估, 容器化, 密码管理, 开源, 指标, 日志, 版权保护, 生产环境, 用户代理, 监控, 监控栈, 自定义请求头, 运维, 链路追踪