部署 FlixTube

已发行

本章内容概览:

  • 回顾技术栈
  • FlixTube 应用架构
  • 统一代码库开发
  • 生产部署清单
  • CI/CD 流水线

10.1 技术栈回顾

开发语言和运行时:

  • Node.js 18.17.1
  • JavaScript (ES6+)

容器化和编排:

  • Docker 24.0.5
  • Kubernetes
  • Azure Container Registry (ACR)

云基础设施:

  • Microsoft Azure
  • Azure Kubernetes Service (AKS)
  • Terraform

CI/CD:

  • GitHub Actions
  • Git

测试:

  • Jest (单元测试和集成测试)
  • Playwright (端到端测试)

数据存储:

  • MongoDB
  • Azure Blob Storage

消息队列:

  • RabbitMQ

10.2 FlixTube 应用架构

图 10.1 FlixTube 生产环境架构
图 10.1 FlixTube 生产环境架构

微服务列表:

  1. Web Backend (前端网关)

    • 处理来自 Web 用户的请求
    • 端口:3000
    • 依赖:video-streaming, video-upload
  2. Mobile Backend (移动端网关)

    • 处理来自移动应用的请求
    • 端口:3100
    • 依赖:video-streaming, video-upload
  3. Video Upload (视频上传)

    • 处理视频上传请求
    • 端口:4000
    • 依赖:video-storage, RabbitMQ
  4. Video Storage (视频存储)

    • 管理视频元数据
    • 端口:4001
    • 依赖:MongoDB
  5. Video Streaming (视频流)

    • 提供视频流服务
    • 端口:3000
    • 依赖:video-storage

数据流:

视频上传:

  1. 用户通过前端上传视频
  2. Web Backend 转发到 Video Upload 服务
  3. Video Upload 将视频存储到 Azure Blob Storage
  4. Video Upload 将元数据保存到 MongoDB
  5. Video Upload 发送消息到 RabbitMQ

视频播放:

  1. 用户请求播放视频
  2. Web Backend 转发到 Video Streaming 服务
  3. Video Streaming 从 MongoDB 获取视频信息
  4. Video Streaming 从 Azure Blob Storage 流式传输视频

10.3 统一代码库

统一代码库的优势:

  • 简化代码共享
  • 统一版本控制
  • 简化依赖管理
  • 跨服务重构更容易
  • 统一 CI/CD 流程

目录结构:

flixtube/
  video-streaming/
    src/
    Dockerfile
    package.json
  video-upload/
    src/
    Dockerfile
    package.json
  video-storage/
    src/
    Dockerfile
    package.json
  web-backend/
    src/
    Dockerfile
    package.json
  mobile-backend/
    src/
    Dockerfile
    package.json
  docker-compose.yml
  k8s/
    deployment.yaml
    service.yaml

10.4 生产部署

Terraform 创建基础设施:

resource "azurerm_resource_group" "main" {
  name     = "flixtube-prod"
  location = "East US"
}

resource "azurerm_kubernetes_cluster" "main" {
  name                = "flixtube-aks"
  location            = azurerm_resource_group.main.location
  resource_group_name = azurerm_resource_group.main.name
  dns_prefix          = "flixtube"

  default_node_pool {
    name       = "default"
    node_count = 3
    vm_size    = "Standard_DS3_v2"
  }
}

resource "azurerm_container_registry" "main" {
  name                = "flixtubeacr"
  resource_group_name = azurerm_resource_group.main.name
  location            = azurerm_resource_group.main.location
  sku                 = "Standard"
  admin_enabled        = true
}

Kubernetes 部署清单:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: video-streaming
spec:
  replicas: 3
  selector:
    matchLabels:
      app: video-streaming
  template:
    metadata:
      labels:
        app: video-streaming
    spec:
      containers:
      - name: video-streaming
        image: flixtubeacr.azurecr.io/video-streaming:latest
        ports:
        - containerPort: 3000
        resources:
          requests:
            memory: "128Mi"
            cpu: "100m"
          limits:
            memory: "256Mi"
            cpu: "200m"

10.5 CI/CD 流水线

图 10.2 完整的 CI/CD 流程
图 10.2 完整的 CI/CD 流程

步骤:

  1. 代码推送 → GitHub
  2. 触发构建 → GitHub Actions
  3. 运行测试 → Jest + Playwright
  4. 构建镜像 → Docker
  5. 推送镜像 → Azure Container Registry
  6. 部署到 Kubernetes → kubectl
  7. 健康检查 → Kubernetes Probes

10.6 监控和可观察性

健康检查:

app.get('/health', (req, res) => {
  res.status(200).json({ status: 'healthy' });
});

日志聚合:

kubectl logs -f deployment/video-streaming

总结

  • FlixTube 由 5 个微服务组成
  • 统一代码库简化代码共享和依赖管理
  • Terraform 创建和管理基础设施
  • Kubernetes 用于容器编排和部署
  • GitHub Actions 实现完整的 CI/CD 流水线
  • 健康检查确保服务的可用性
  • 生产部署需要考虑资源限制和高可用性

下一章学习监控和维护运行中的微服务应用。

创建于 2026/01/06 更新于 2026/01/06 851 字 阅读约 2 分钟

提交勘误/建议