好的, 让我们来详细规划第 13 部分: 部署和监控。我们将这部分分为 3 天, 每天专注于特定的部署和监控任务。

第 13 部分:部署和监控 (3 天)

Day 1: 容器化和 CI/CD

  1. 容器化应用

    • 为后端创建 Dockerfile
    # Backend Dockerfile
    FROM node:14
     
    WORKDIR /usr/src/app
     
    COPY package*.json ./
     
    RUN npm install
     
    COPY . .
     
    RUN npm run build
     
    EXPOSE 3000
     
    CMD ["npm", "run", "start:prod"]
    • 为前端创建 Dockerfile
    # Frontend Dockerfile
    FROM node:14 as build
     
    WORKDIR /app
     
    COPY package*.json ./
     
    RUN npm install
     
    COPY . .
     
    RUN npm run build
     
    FROM nginx:alpine
     
    COPY --from=build /app/build /usr/share/nginx/html
     
    EXPOSE 80
     
    CMD ["nginx", "-g", "daemon off;"]
    • 创建 docker-compose. Yml
    version: '3'
    services:
      backend:
        build: ./backend
        ports:
          - "3000:3000"
        environment:
          - DATABASE_URL=postgres://user:password@db:5432/lightweighttodo
        depends_on:
          - db
      frontend:
        build: ./frontend
        ports:
          - "80:80"
      db:
        image: postgres:13
        environment:
          - POSTGRES_USER=user
          - POSTGRES_PASSWORD=password
          - POSTGRES_DB=lightweighttodo
      redis:
        image: redis:6
  2. 设置 CI/CD 管道

    • 创建 GitLab CI/CD 配置文件 (. Gitlab-ci. Yml)
    stages:
      - test
      - build
      - deploy
     
    test:
      stage: test
      image: node:14
      script:
        - cd backend
        - npm install
        - npm run test
        - cd ../frontend
        - npm install
        - npm run test
     
    build:
      stage: build
      image: docker:latest
      services:
        - docker:dind
      script:
        - docker build -t backend:$CI_COMMIT_SHA ./backend
        - docker build -t frontend:$CI_COMMIT_SHA ./frontend
        - docker push $CI_REGISTRY/backend:$CI_COMMIT_SHA
        - docker push $CI_REGISTRY/frontend:$CI_COMMIT_SHA
     
    deploy:
      stage: deploy
      image: alpine:latest
      script:
        - apk add --no-cache openssh-client
        - ssh user@your-server "docker-compose pull && docker-compose up -d"
  3. 实现自动化部署脚本

    • 创建 deploy. Sh 脚本
    #!/bin/bash
     
    # 拉取最新的镜像
    docker-compose pull
     
    # 停止并删除旧容器
    docker-compose down
     
    # 启动新容器
    docker-compose up -d
     
    # 清理未使用的镜像和卷
    docker image prune -af
    docker volume prune -f

Day 2: 配置生产环境

  1. 设置生产环境服务器

    • 选择云服务提供商 (如 AWS, DigitalOcean)
    • 配置服务器安全组和防火墙规则
    • 安装必要的软件 (Docker, Docker Compose, Nginx)
  2. 配置反向代理

    • 安装 Nginx
    • 配置 Nginx 作为反向代理
    server {
        listen 80;
        server_name yourdomain.com;
     
        location / {
            proxy_pass http://frontend;
        }
     
        location /api {
            proxy_pass http://backend:3000;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }
  3. 设置 SSL 证书

    • 使用 Let’s Encrypt 获取免费 SSL 证书
    sudo certbot --nginx -d yourdomain.com
  4. 配置环境变量

    • 创建. Env 文件
    DATABASE_URL=postgres://user:password@db:5432/lightweighttodo
    REDIS_URL=redis://redis:6379
    JWT_SECRET=your_jwt_secret
    
  5. 设置数据库备份

    • 创建自动备份脚本
    #!/bin/bash
     
    TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
    BACKUP_DIR="/path/to/backups"
     
    docker exec -t your-db-container pg_dump -c -U postgres lightweighttodo > $BACKUP_DIR/backup_$TIMESTAMP.sql
     
    # 保留最近30天的备份
    find $BACKUP_DIR -type f -mtime +30 -name '*.sql' -delete

Day 3: 监控和日志管理

  1. 设置应用程序监控

    • 安装和配置 Prometheus
    # prometheus.yml
    global:
      scrape_interval: 15s
     
    scrape_configs:
      - job_name: 'nodejs'
        static_configs:
          - targets: ['backend:3000']
    • 在后端应用中集成 Prometheus 客户端
    import { register, Counter } from 'prom-client';
     
    const httpRequestsTotal = new Counter({
      name: 'http_requests_total',
      help: 'Total number of HTTP requests',
      labelNames: ['method', 'route', 'status_code'],
    });
     
    // 在请求处理中间件中使用
    app.use((req, res, next) => {
      res.on('finish', () => {
        httpRequestsTotal.inc({
          method: req.method,
          route: req.route?.path,
          status_code: res.statusCode,
        });
      });
      next();
    });
     
    app.get('/metrics', async (req, res) => {
      res.set('Content-Type', register.contentType);
      res.end(await register.metrics());
    });
  2. 配置日志聚合

    • 设置 ELK 栈 (Elasticsearch, Logstash, Kibana)
    • 配置 Logstash 以收集 Docker 容器日志
    input {
      file {
        path => "/var/lib/docker/containers/*/*.log"
        start_position => "beginning"
      }
    }
    
    filter {
      json {
        source => "message"
      }
    }
    
    output {
      elasticsearch {
        hosts => ["elasticsearch:9200"]
      }
    }
    
  3. 实现健康检查

    • 在后端应用中添加健康检查端点
    import { Controller, Get } from '@nestjs/common';
     
    @Controller('health')
    export class HealthController {
      @Get()
      check() {
        return { status: 'ok' };
      }
    }
    • 配置 Docker 健康检查
    services:
      backend:
        # ...其他配置...
        healthcheck:
          test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
          interval: 30s
          timeout: 10s
          retries: 3
  4. 设置告警系统

    • 配置 Alertmanager
    # alertmanager.yml
    route:
      group_by: ['alertname']
      group_wait: 30s
      group_interval: 5m
      repeat_interval: 1h
      receiver: 'email-notifications'
     
    receivers:
    - name: 'email-notifications'
      email_configs:
      - to: 'your-email@example.com'
        from: 'alertmanager@example.com'
        smarthost: 'smtp.gmail.com:587'
        auth_username: 'your-email@gmail.com'
        auth_identity: 'your-email@gmail.com'
        auth_password: 'your-app-specific-password'
    • 定义告警规则
    # alert.rules
    groups:
    - name: example
      rules:
      - alert: HighRequestLatency
        expr: http_request_duration_seconds{quantile="0.5"} > 1
        for: 10m
        labels:
          severity: warning
        annotations:
          summary: "High request latency on {{ $labels.instance }}"
          description: "{{ $labels.instance }} has a median request latency above 1s (current value: {{ $value }}s)"
  5. 实现性能基准测试

    • 使用 Apache Benchmark 进行负载测试
    ab -n 1000 -c 100 http://yourdomain.com/api/todos
    • 创建性能测试脚本
    import http from 'k6/http';
    import { sleep } from 'k6';
     
    export default function() {
      http.get('http://yourdomain.com/api/todos');
      sleep(1);
    }

    运行测试:

    k6 run --vus 10 --duration 30s script.js

这个详细计划涵盖了第 13 部分的部署和监控, 包括应用容器化、CI/CD 配置、生产环境设置、监控系统实现和性能测试。通过这些步骤, LightweightTodo Pro 将具备完整的部署流程和监控能力, 确保应用在生产环境中的稳定运行和高效管理。这将大大提高应用的可靠性和可维护性, 为用户提供更好的服务质量。