BodySense 云原生基础设施规划

BodySense 基于 DigitalOcean 的云原生基础设施规划:Droplet、Object Storage、Container Registry、Managed PostgreSQL、Managed Redis。已部署上线。

#type / howto #status / growing #tech / ops #tech / ai

[!info] related notes

BodySense 云原生基础设施规划

部署状态:✅ 已上线 — 2026-07-05 首次部署成功

  • 访问地址:https://bodydo.bakersean.top
  • API 健康:{"db":"ok","redis":"ok","status":"ok"}
  • 镜像仓库:DO Container Registry (body-sense-docker-repo)
  • 部署方式:GitHub Actions → DO Registry → SSH deploy

一句话定义

基于 DigitalOcean 的 BodySense 云原生基础设施方案:一台 Droplet 跑应用容器,托管数据库/缓存做状态外置,Object Storage 存文件,Container Registry 存镜像。


资源总览

服务月费用途
Droplet (4C/8G)$68跑所有应用容器(Web/API/AI/Caddy)
Spaces Object Storage$5图片、视频、日志、备份、RAG 文件、静态资源
Container Registry$5存 Docker 镜像,CI/CD 自动部署
Managed PostgreSQL$15.15生产级数据库,自动备份、高可用
Managed Valkey/Redis$15缓存、队列、会话、限流

总计: $108.15/月


架构映射

                        DigitalOcean 基础设施
┌─────────────────────────────────────────────────────────┐
│                                                         │
│  ┌─── Droplet (4C/8G) ──────────────────────────────┐  │
│  │                                                   │  │
│  │  [Caddy :80/:443]                                │  │
│  │      ├─ /        → [nginx (web)] → SPA           │  │
│  │      └─ /api/    → [Go API :8080]                │  │
│  │                      ↓ 内部调用                    │  │
│  │                  [Python AI :8100]                │  │
│  │                                                   │  │
│  │  [Watchtower] ← 轮询 Container Registry          │  │
│  └───────────────────────────────────────────────────┘  │
│                         │                               │
│         ┌───────────────┼───────────────┐               │
│         ↓               ↓               ↓               │
│  ┌─────────────┐ ┌─────────────┐ ┌─────────────┐       │
│  │ Managed     │ │ Managed     │ │ Spaces      │       │
│  │ PostgreSQL  │ │ Valkey/Redis│ │ Object      │       │
│  │ $15.15/月   │ │ $15/月      │ │ Storage     │       │
│  │             │ │             │ │ $5/月        │       │
│  └─────────────┘ └─────────────┘ └─────────────┘       │
│                                                         │
│  ┌─────────────────────┐                               │
│  │ Container Registry  │ ← CI/CD 推镜像               │
│  │ $0~$5/月            │ ← Watchtower 拉镜像          │
│  └─────────────────────┘                               │
└─────────────────────────────────────────────────────────┘

各服务详细方案

1. Droplet(应用服务器)

规格: 4 vCPU / 8 GB RAM / 160 GB SSD

运行的容器:

  • Caddy(反向代理 + 自动 HTTPS)
  • nginx(前端静态文件服务)
  • Go API(业务后端)
  • Python AI Service(AI 运行时)
  • Watchtower(自动更新)

配置要点:

  • 开启 Swap(2~4GB),防止内存压力
  • 配置 UFW 防火墙,只开放 22/80/443
  • 安装 Docker + Docker Compose
  • 配置日志轮转,防止磁盘打满
# 初始化脚本
sudo apt update && sudo apt upgrade -y
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile && sudo mkswap /swapfile && sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

# 安装 Docker
curl -fsSL https://get.docker.com | sh
sudo usermod -aG docker $USER

# 防火墙
sudo ufw allow 22 && sudo ufw allow 80 && sudo ufw allow 443
sudo ufw enable

2. Spaces Object Storage(文件存储)

用途:

  • 用户上传的图片/视频
  • RAG 知识库源文件
  • 日志归档
  • 数据库备份
  • 前端静态资源 CDN(可选)

接入方式: S3 兼容 API,直接用 AWS SDK

Endpoint: https://bodysense.sgp1.digitaloceanspaces.com
Region: sgp1(新加坡,离大陆近)
Access Key: DO_SPACES_KEY
Secret Key: DO_SPACES_SECRET
Bucket: bodysense-assets

Python AI Service 接入:

import boto3

s3 = boto3.client(
    "s3",
    endpoint_url="https://bodysense.sgp1.digitaloceanspaces.com",
    aws_access_key_id=os.environ["DO_SPACES_KEY"],
    aws_secret_access_key=os.environ["DO_SPACES_SECRET"],
)

# 上传文件
s3.upload_file("local.png", "bodysense-assets", "uploads/user123/photo.png")

# 生成预签名 URL(限时访问)
url = s3.generate_presigned_url(
    "get_object",
    Params={"Bucket": "bodysense-assets", "Key": "uploads/user123/photo.png"},
    ExpiresIn=3600,
)

Go API 接入:

import "github.com/aws/aws-sdk-go-v2/service/s3"

// 使用同样的 S3 兼容配置
cfg, _ := config.LoadDefaultConfig(ctx,
    config.WithRegion("sgp1"),
    config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(
        os.Getenv("DO_SPACES_KEY"),
        os.Getenv("DO_SPACES_SECRET"),
        "",
    )),
)
client := s3.NewFromConfig(cfg, func(o *s3.Options) {
    o.BaseEndpoint = aws.String("https://bodysense.sgp1.digitaloceanspaces.com")
})

3. Container Registry(镜像仓库)

用途: 存放三个服务的 Docker 镜像

镜像命名:

registry.digitalocean.com/bodysense/web:latest
registry.digitalocean.com/bodysense/api:latest
registry.digitalocean.com/bodysense/ai-service:latest

CI/CD 流程:

git push → GitHub Actions
  → 构建三个镜像
  → 推送到 DO Container Registry
  → Watchtower 在 Droplet 上检测到新镜像
  → 自动拉取并滚动重启

GitHub Actions Secret 配置:

DO_REGISTRY_NAME=bodysense
DO_REGISTRY_TOKEN=<DigitalOcean API Token>

免费额度: 1 个仓库免费,3 个仓库 $5/月

4. Managed PostgreSQL(托管数据库)

规格: $15.15/月(1C/1G/10GB SSD)

优势:

  • 自动每日备份
  • 自动故障转移(高可用)
  • 不占用 Droplet 资源
  • 内置 pgvector 扩展支持

连接方式:

Host: bodysense-db-do-user-xxxxx-0.db.ondigitalocean.com
Port: 25060
Database: bodysense
User: bodysense_admin
Password: <managed password>
SSL: require

Go API 连接字符串:

postgres://bodysense_admin:xxx@bodysense-db-do-user-xxxxx-0.db.ondigitalocean.com:25060/bodysense?sslmode=require

迁移策略:

  • 开发环境仍用 Docker PostgreSQL
  • 生产环境用 Managed PostgreSQL
  • golang-migrate 连接字符串通过环境变量切换

注意: 如果预算有限,也可以先在 Droplet 里跑 PostgreSQL 容器,后续再迁移。

5. Managed Valkey/Redis(托管缓存)

规格: $15/月起

用途:

  • 用户会话缓存(JWT 黑名单)
  • API 限流计数器
  • 任务队列
  • AI Service 的中间状态缓存

连接方式:

Host: bodysense-redis-do-user-xxxxx-0.db.ondigitalocean.com
Port: 25061
Password: <managed password>
TLS: enabled

注意: 如果预算有限,Droplet 8G 内存足够跑一个 Redis 容器,性能完全够用。

相关 MOC

创建于 2026/7/4 更新于 2026/7/15