{T}

伸缩性架构 学习笔记(第 2 部分)

3.2 传统部署 vs 虚拟机 vs 容器

对比分析

维度传统部署虚拟机容器
启动速度分钟级分钟级秒级
资源利用率
隔离性较好
性能损耗10-20%< 5%
镜像大小N/AGB 级MB 级
可移植性
运维复杂度

架构对比图

plaintext
┌─────────────────────────────────────────┐
│           传统部署架构                    │
├─────────────────────────────────────────┤
│  应用 A  │  应用 B  │  应用 C  │  应用 D  │
│         │         │         │         │
│         └─────────┴─────────┘          │
│                  操作系统               │
│              物理服务器硬件              │
└─────────────────────────────────────────┘
 
┌─────────────────────────────────────────┐
│           虚拟机部署架构                  │
├─────────────────────────────────────────┤
│  VM1     │  VM2     │  VM3     │  VM4    │
│ 应用A   │ 应用B   │ 应用C   │ 应用D   │
│ Guest OS │ Guest OS │ Guest OS │ Guest OS│
│ Hypervisor (虚拟化层)                    │
│              Host OS / 硬件              │
└─────────────────────────────────────────┘
 
┌─────────────────────────────────────────┐
│           容器化部署架构                  │
├─────────────────────────────────────────┤
│ 容器A   │ 容器B   │ 容器C   │ 容器D    │
│ App     │ App     │ App     │ App      │
│ Libs    │ Libs    │ Libs    │ Libs     │
│      Container Runtime (Docker)         │
│              Host OS / 硬件              │
└─────────────────────────────────────────┘

3.3 Docker 容器技术

核心概念

javascript
// Docker 核心概念
const dockerConcepts = {
  // 1. 镜像 (Image)
  image: {
    definition: '只读模板,包含运行应用所需的所有内容',
    features: ['分层存储', '可复用', '版本管理'],
    example: 'nginx:latest, node:16-alpine'
  },
  
  // 2. 容器 (Container)
  container: {
    definition: '镜像的运行实例,相互隔离',
    features: ['轻量级', '可移植', '隔离性'],
    lifecycle: ['创建', '启动', '停止', '删除']
  },
  
  // 3. 仓库 (Registry)
  registry: {
    definition: '存储和分发镜像的地方',
    types: ['Docker Hub (公共)', 'Harbor (私有)', '阿里云镜像仓库'],
    commands: ['docker push', 'docker pull']
  }
}

Dockerfile 示例

dockerfile
# 前端应用 Dockerfile 示例
# 阶段1: 构建阶段
FROM node:16-alpine AS builder
 
WORKDIR /app
 
# 复制依赖文件
COPY package*.json ./
 
# 安装依赖
RUN npm ci --only=production
 
# 复制源代码
COPY . .
 
# 构建应用
RUN npm run build
 
# 阶段2: 生产阶段
FROM nginx:alpine
 
# 复制构建产物到 Nginx
COPY --from=builder /app/dist /usr/share/nginx/html
 
# 复制 Nginx 配置
COPY nginx.conf /etc/nginx/conf.d/default.conf
 
# 暴露端口
EXPOSE 80
 
# 启动命令
CMD ["nginx", "-g", "daemon off;"]

Docker Compose 多容器编排

yaml
# docker-compose.yml 示例
version: '3.8'
 
services:
  # 前端服务
  frontend:
    build: ./frontend
    ports:
      - "80:80"
    depends_on:
      - backend
    networks:
      - app-network
    restart: always
  
  # 后端服务
  backend:
    build: ./backend
    ports:
      - "3000:3000"
    environment:
      - DATABASE_URL=postgresql://user:pass@db:5432/mydb
    depends_on:
      - db
      - redis
    networks:
      - app-network
    restart: always
  
  # 数据库
  db:
    image: postgres:13-alpine
    environment:
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=pass
      - POSTGRES_DB=mydb
    volumes:
      - postgres-data:/var/lib/postgresql/data
    networks:
      - app-network
    restart: always
  
  # 缓存
  redis:
    image: redis:6-alpine
    ports:
      - "6379:6379"
    networks:
      - app-network
    restart: always
 
networks:
  app-network:
    driver: bridge
 
volumes:
  postgres-data:

四、Kubernetes 容器编排