This commit is contained in:
DullJZ
2025-09-13 22:30:40 +08:00
parent 4268f6238d
commit f214b003fd
34 changed files with 2217 additions and 10 deletions

45
deploy/docker/Dockerfile Normal file
View File

@@ -0,0 +1,45 @@
# 多阶段构建用于减小镜像大小
FROM golang:1.24-alpine AS builder
# 安装构建依赖包括CGO支持的sqlite3
RUN apk add --no-cache gcc musl-dev sqlite-dev git ca-certificates tzdata
# 设置工作目录
WORKDIR /build
# 复制 Go 模块文件
COPY go.mod go.sum ./
RUN go mod download && go mod tidy
# 复制源码
COPY . .
# 构建应用启用CGO
RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build \
-ldflags='-w -s -X main.version=$(git describe --tags --always)' \
-o s3-balance \
cmd/s3-balance/main.go
# 运行时镜像
FROM alpine:latest
# 安装运行时依赖包括sqlite库
RUN apk --no-cache add ca-certificates tzdata sqlite-libs
# 设置时区
ENV TZ=Asia/Shanghai
WORKDIR /app
# 复制二进制文件
COPY --from=builder /build/s3-balance /app/s3-balance
# 创建配置和数据目录
RUN mkdir -p /app/config /app/data
# 暴露端口
EXPOSE 8080
# 运行应用
ENTRYPOINT ["/app/s3-balance"]
CMD ["-config", "/app/config/config.yaml"]

View File

@@ -0,0 +1,62 @@
# Docker 配置 - 启用监控
server:
host: "0.0.0.0"
port: 8080
read_timeout: 30s
write_timeout: 30s
idle_timeout: 60s
database:
type: "sqlite"
dsn: "data/s3-balance.db"
max_open_conns: 25
max_idle_conns: 5
conn_max_lifetime: 300
log_level: "info"
auto_migrate: true
# 示例存储桶配置 - 请根据实际情况修改
buckets:
- name: "minio-bucket"
endpoint: "http://minio:9000"
region: "us-east-1"
access_key_id: "minioadmin"
secret_access_key: "minioadmin"
max_size: "1GB"
weight: 10
enabled: true
use_ssl: false
path_style: true
virtual: false
- name: "user-data"
endpoint: ""
region: "us-east-1"
access_key_id: ""
secret_access_key: ""
max_size: "1GB"
weight: 10
enabled: true
use_ssl: true
path_style: false
virtual: true
balancer:
strategy: "least-space"
health_check_period: 30s
update_stats_period: 60s
retry_attempts: 3
retry_delay: 1s
# 监控指标 - 启用状态
metrics:
enabled: true
path: "/metrics"
port: 9090
s3api:
access_key: "AKIAIOSFODNN7EXAMPLE"
secret_key: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
virtual_host: false
proxy_mode: false
auth_required: false

View File

@@ -0,0 +1,90 @@
version: '3.8'
services:
# S3 Balance 服务
s3-balance:
build:
context: ../..
dockerfile: deploy/docker/Dockerfile
container_name: s3-balance
ports:
- "8080:8080"
volumes:
- ../docker/config.docker.yaml:/app/config/config.yaml
- ../docker/data:/app/data
environment:
- TZ=Asia/Shanghai
networks:
- monitoring
restart: unless-stopped
command: ["/app/s3-balance", "-config", "/app/config/config.yaml"]
# Prometheus 监控
prometheus:
image: prom/prometheus:latest
container_name: prometheus
ports:
- "9090:9090"
volumes:
- ../monitoring/prometheus.yml:/etc/prometheus/prometheus.yml
- ../monitoring/s3_balance_alerts.yml:/etc/prometheus/s3_balance_alerts.yml
- prometheus_data:/prometheus
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.path=/prometheus'
- '--web.console.libraries=/etc/prometheus/console_libraries'
- '--web.console.templates=/etc/prometheus/consoles'
- '--storage.tsdb.retention.time=200h'
- '--web.enable-lifecycle'
networks:
- monitoring
restart: unless-stopped
# Grafana 可视化
grafana:
image: grafana/grafana:latest
container_name: grafana
ports:
- "3000:3000"
volumes:
- grafana_data:/var/lib/grafana
- ../monitoring/grafana/provisioning:/etc/grafana/provisioning
- ../monitoring/grafana/dashboards:/var/lib/grafana/dashboards
environment:
- GF_SECURITY_ADMIN_USER=admin
- GF_SECURITY_ADMIN_PASSWORD=admin123
- GF_USERS_ALLOW_SIGN_UP=false
- GF_INSTALL_PLUGINS=grafana-clock-panel,grafana-simple-json-datasource
networks:
- monitoring
restart: unless-stopped
depends_on:
- prometheus
# Node Exporter (可选,监控宿主机)
node-exporter:
image: prom/node-exporter:latest
container_name: node-exporter
ports:
- "9100:9100"
volumes:
- /proc:/host/proc:ro
- /sys:/host/sys:ro
- /:/rootfs:ro
command:
- '--path.procfs=/host/proc'
- '--path.sysfs=/host/sys'
- '--collector.filesystem.mount-points-exclude=^/(sys|proc|dev|host|etc)($$|/)'
networks:
- monitoring
restart: unless-stopped
volumes:
prometheus_data:
driver: local
grafana_data:
driver: local
networks:
monitoring:
driver: bridge

View File

@@ -0,0 +1,72 @@
#!/bin/bash
# S3 Balance + 监控栈 一键启动(修复版)
set -e
echo "🚀 正在启动 S3 Balance + 监控栈修复CGO版本..."
# 检查 Docker 和 Docker Compose
if ! command -v docker &> /dev/null; then
echo "❌ Docker 未安装,请先安装 Docker"
exit 1
fi
if ! command -v docker-compose &> /dev/null; then
echo "❌ Docker Compose 未安装,请先安装 Docker Compose"
exit 1
fi
# 创建必要目录
echo "📁 创建必要目录..."
mkdir -p data grafana/provisioning/datasources grafana/provisioning/dashboards
# 构建 S3 Balance 镜像(使用固定 Go 版本)
echo "🔨 构建 S3 Balance 镜像..."
docker build -t s3-balance:latest -f ../docker/Dockerfile ../..
# 停止已有容器(如果存在)
echo "🛑 清理已有容器..."
docker-compose down 2>/dev/null || true
# 启动服务
echo "🐳 启动 Docker 容器..."
docker-compose up -d
# 等待服务启动
echo "⏳ 等待服务启动..."
sleep 25
# 检查服务状态
echo "🔍 检查服务状态..."
if docker-compose ps | grep -q "Up"; then
echo ""
echo "✅ 服务启动完成!"
else
echo ""
echo "❌ 服务启动可能失败,检查日志:"
echo "docker-compose logs"
exit 1
fi
# 输出访问信息
echo ""
echo "🔗 访问地址:"
echo " 📊 Grafana 面板: http://localhost:3000 (用户名: admin, 密码: admin123)"
echo " 🔥 Prometheus: http://localhost:9090"
echo " 📈 指标端点: http://localhost:8080/metrics"
echo " 🐳 Node 指标: http://localhost:9100/metrics"
echo ""
echo "🔧 管理命令:"
echo " docker-compose logs -f s3-balance # 查看 S3 Balance 日志"
echo " docker-compose logs -f prometheus # 查看 Prometheus 日志"
echo " docker-compose logs -f grafana # 查看 Grafana 日志"
echo " docker-compose down # 停止所有服务"
echo " docker-compose restart s3-balance # 重启 S3 Balance"
echo ""
echo "📊 指标查询示例:"
echo " - 存储桶健康: s3_balance_bucket_healthy"
echo " - QPS: rate(s3_balance_s3_operations_total[1m])"
echo " - 延迟: histogram_quantile(0.95, s3_balance_s3_operation_duration_seconds_bucket)"
echo ""
echo "🎉 享受完整的 S3 Balance 监控体验!"